Reputation: 135
I am new to .net, when i run the program, it reports the error"无法将类型为System.Int32的对象强制转换为类型System.String"
Imports System.Data Imports System.Data.SqlClient Imports System.Data.Sql Partial Class _Default Inherits System.Web.UI.Page
Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
Dim constr As String
constr = ConfigurationManager.ConnectionStrings("libConstr").ConnectionString Dim conn As SqlConnection
conn = New SqlConnection(constr)
conn.Open()
Dim com As SqlCommand
com = New SqlCommand()
com.Connection = conn
com.CommandText = "select * from library"
Dim dr As SqlDataReader
dr = com.ExecuteReader()
Dim i As Integer
i = 0
Do While dr.Read()
If tbuser.Text = dr.GetString(i) Then //here report the error:无法将类型为System.Int32的对象强制转换为类型System.String(System.Int32 cound not be cast to the type of System.String)
If tbid.Text = dr.GetString(i + 1) Then
Response.Redirect("library.aspx")
End If
i = i + 1
End If
Loop
End Sub
What was wrong, can you help me. Thank you very much.
Upvotes: 0
Views: 1771
Reputation: 151
Interesting answers, for sure, and none are wrong (with the exception of CodeInChaos's second reply to Kon, although I suspect that was just a mis-statement). However, what can you learn from these answers?
The GetString(foo)
method expects an int32 type as its parameter (foo for me, i for you), which represents the ordinal position of the zero-based ordinal position of the column sought. Similarly, the remaining Get*type*(foo)
methods are specific to the expected data type, with the exception of GetValue(foo)
.
As YetAnotherUser and Justin Niessner point out explicitly, "No conversions are performed", and the "...method ... for reading string values ... is throwing an exception...". This is the data access layer, and performance is at a premium. You are expected to know the datatype of the column sought. If you don't, then use the method that returns an object, GetValue(foo)
, as suggested by Justin.
Your i
parameter is defined as needed, and is not the issue.
The type of the receiving object.property, tbuser.Text
, is also not the issue, but it will be if you change the GetString(foo)
method to GetInt32(foo)
or GetValue(foo)
, at which point you need *value*.ToString()
(or Convert.ToString(*value*)
) for the value from Column[i].
Know your data store's datatype for each column, or use the less-performant 'GetValue(foo)' method and cast the result to the receiving type.
Upvotes: 0
Reputation: 9356
You need to check for nulls before calling this method.
See MSDN:
No conversions are performed; therefore, the data retrieved must already be a string.
Call IsDBNull to check for null values before calling this method.
Upvotes: 0
Reputation: 245459
You are using a method that is specifically for reading string values from the result set. In this case, the value you are reading is an integer so the method is throwing an exception. If you want to read the value properly but store it as a string, I would try:
If tbuser.Text = dr.GetValue(i).ToString() Then
If tbid.Text = dr.GetValue(i + 1).ToString() Then
Response.Redirect("library.aspx")
End If
End If
Upvotes: 0
Reputation: 3931
It seems like your database stores the underlying field as a integer, not a string. Thus you need to call dr.GetInt32(i), and then convert that resulting integer to a string.
Upvotes: 0
Reputation: 27441
It seems that the value at location i of the data reader is an int, but you're trying to retrieve it as if it was a string. Try: dr.GetInt32(i)
Not sure what some of the gibberish is. :)
If you're trying to assign it to a string, then do this: Convert.ToString(dr.GetInt32(i))
Upvotes: 2