odonovanpm
asked on
SQLDataReader
When I return a datareader from one of my classes I want to assign column values to individual text boxes. What code should I use? See code below:
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Dim drProduct As New admin
drProduct.ProductID = ddlProduct.SelectedItem.Va lue
If drProduct.GetProduct.Read( ) Then
Dim dr As SqlDataReader = drProduct.GetProduct()
ddlCategoryID.SelectedValu e = ddlCategory.SelectedItem.V alue
// I want to put in values from individual columns into text boxes but code below does not work?
txtProductCode.text = dr("ProductCode")
End If
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Dim drProduct As New admin
drProduct.ProductID = ddlProduct.SelectedItem.Va
If drProduct.GetProduct.Read(
Dim dr As SqlDataReader = drProduct.GetProduct()
ddlCategoryID.SelectedValu
// I want to put in values from individual columns into text boxes but code below does not work?
txtProductCode.text = dr("ProductCode")
End If
End Sub
Hi odonovanpm,
Well, you first have to "read" a row from the reader... try adding this line before the assignment:
If dr.Read then
txtProductCode.text = dr("ProductCode")
dr.Close
End if
If that doesn't do it, I'd have to see what your GetProduct procedure looks like.
Cheers!
Well, you first have to "read" a row from the reader... try adding this line before the assignment:
If dr.Read then
txtProductCode.text = dr("ProductCode")
dr.Close
End if
If that doesn't do it, I'd have to see what your GetProduct procedure looks like.
Cheers!
good catch, mdougan. I totally missed that the asker had not 'read' the Datareader.
AW
AW
ASKER
drProduct.GetProduct() returns an sqldatareader, I have used it as the datasource for a datagrid and it displays fine so I don't think there is a problem with the class... drProduct.GetProduct.Read( ) is the same as what you suggested dr.Read()???
For some strange reason it isn't displaying the field, I get an error saying there is no data present but when I look in the table there is??? Very strange.
For some strange reason it isn't displaying the field, I get an error saying there is no data present but when I look in the table there is??? Very strange.
Wait a minute... this code is really confusing to me:
If drProduct.GetProduct.Read( ) Then
Dim dr As SqlDataReader = drProduct.GetProduct()
First, what is drProduct? I was assuming that this was some local class you'd developed, and that the GetProduct was a function that was part of that class...
Since dr is your local data reader and since you're theoretically setting it to a newly created datareader through that assignment, it makes no sense for you to be doing the .Read statement before that.
Here is how you'd normally open a datareader and read the contents:
Dim oConn As SqlConnection
Dim oCmd As SqlCommand
'Get connection and set up for recordset (just a function I use to set conn)
If Not NETSetConnection(oConn) Then
Exit Function
End If
'Create a Command object.
oCmd = oConn.CreateCommand
oCmd.CommandText = "Select ProductID from Products Where Cust = 1"
oCmd.CommandType = CommandType.Text
oCmd.CommandTimeout = 600
oDatareader = oCmd.ExecuteReader(Command Behavior.C loseConnec tion)
if oDatareader.Read Then
txtProduct.Text = oDatareader("ProductID")
end if
oDatareader.Close
If drProduct.GetProduct.Read(
Dim dr As SqlDataReader = drProduct.GetProduct()
First, what is drProduct? I was assuming that this was some local class you'd developed, and that the GetProduct was a function that was part of that class...
Since dr is your local data reader and since you're theoretically setting it to a newly created datareader through that assignment, it makes no sense for you to be doing the .Read statement before that.
Here is how you'd normally open a datareader and read the contents:
Dim oConn As SqlConnection
Dim oCmd As SqlCommand
'Get connection and set up for recordset (just a function I use to set conn)
If Not NETSetConnection(oConn) Then
Exit Function
End If
'Create a Command object.
oCmd = oConn.CreateCommand
oCmd.CommandText = "Select ProductID from Products Where Cust = 1"
oCmd.CommandType = CommandType.Text
oCmd.CommandTimeout = 600
oDatareader = oCmd.ExecuteReader(Command
if oDatareader.Read Then
txtProduct.Text = oDatareader("ProductID")
end if
oDatareader.Close
ASKER
OK, sorry about that... It is a bit confusing the way I have presented it... drProduct is an instance of a local class I created, GetProduct is a method of the class, this method returns an sqldatareader by using the executereader method as you have in your code... There is no need for the 2nd line I put in "Dim dr as SqlDataReader = drProduct.GetProduct()"...
I just read something about this being an SQL problem, I don't know if this is true or not?
I just read something about this being an SQL problem, I don't know if this is true or not?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Opps, in that second example, your GetProduct function doesn't have to say GetProduct = oDataReader
Dim dr As SqlDataReader = drProduct.GetProduct()
If dr.Read() Then
ddlCategoryID.SelectedValu e = ddlCategory.SelectedItem.V alue
// I want to put in values from individual columns into text boxes but code below does not work?
txtProductCode.text = dr("ProductCode")
End If
'In your code "dr" has not use its .read property
If dr.Read() Then
ddlCategoryID.SelectedValu
// I want to put in values from individual columns into text boxes but code below does not work?
txtProductCode.text = dr("ProductCode")
End If
'In your code "dr" has not use its .read property
so you code should look like this:
If drProduct.GetProduct.Read(
Dim dr As SqlDataReader = drProduct.GetProduct()
ddlCategoryID.SelectedValu
// I want to put in values from individual columns into text boxes but code below does not work?
txtProductCode.text = dr.GetString(0) ' the ORDINAL position of the field
End If
or:
If drProduct.GetProduct.Read(
Dim dr As SqlDataReader = drProduct.GetProduct()
ddlCategoryID.SelectedValu
// I want to put in values from individual columns into text boxes but code below does not work?
txtProductCode.text = dr.GetString("ProductCode"
End If
AW