Except that there will be no field/column/item with the name "option1" ;-)
Look at the select statement.
I think you probably want
If dt.Rows(0)("report_option1
Roger
Main Topics
Browse All TopicsHi,
I am using VS2005 and coding in VB. I want to get the value of a datatable row, first row, column called option1. I dont know how to get it... :( Any ideas, code below of what im trying to do. The line where i have dt.tables is the line thats highlighted as incorrect code, unsure how to get the value for option1 from the datatable :(
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim dt As New DataTable
conn.ConnectionString = connectionstring
myCommand.Connection = conn
myCommand.CommandText = "SELECT report_name, report_option1, report_option2 from report_names WHERE report_category = '" & lstCategories.SelectedValu
lblOption.Text = lstCategories.SelectedValu
Try
conn.Open()
myCommand.ExecuteNonQuery(
myAdapter.SelectCommand = myCommand
myAdapter.Fill(dt)
lstReports.DataSource = dt
lstReports.DisplayMember = "report_name"
If dt.Tables(0).Rows(0).Item(
txtOption1.Enabled = False
Else
lblOption.Text = dt.Tables(0).Rows(0).Item(
txtOption1.Enabled = True
End If
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
Finally
conn.Dispose()
End Try
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Sorry, no. I only do stuff via EE ;-)
And, incidentally, what is the first of these lines supposed to be doing?
myCommand.ExecuteNonQuery(
myAdapter.SelectCommand = myCommand
myAdapter.Fill(dt)
Can I suggest you, temporarily, stick the last of those lines in some reporting object, such as
MsgBox(myAdapter.Fill(dt))
so you can see how many rows the adapter is actually returning. Just in case (as the message you report suggests) it might be less than you expect.
Roger
PS. In any event, I'm just off out, but if this needs any more follow up I imagine Bob or someone will be about. If not, I'll check in again when I come back in an hour or so.
Hey Roger,
Your code works, you are correct. However it appears my problem is to do with my query above that populates the datatable.
This is my MySQl query text ,
myCommand.CommandText = "SELECT report_name, report_option1, report_option2 from report_names WHERE report_category = '" & lstCategories.SelectedValu
Is .selectedvalue.tostring the option to use for a listbox. I have a listbox that has a bunch of categories. If i hardcode the above to be ..... WHERE report_category = 'Housekeeping'" it works, its as if the .selectedvalue.tostring doesnt get the text of the highlighted item in the listbox. In the combo box i had used cbocategories.selectedtext
Am I using the incorrect option here?
It depends on how the listbox is bound. SelectedValue should return the value from the datasource field that was set as .ValueMember in the datatable set as .DataSource. If you want to use the value that comes from the field that was set as .DisplayMember (that is, what is actually displayed in the list) you could use code like this
Dim drv As DataRowView = CType(lstCategories.Select
Dim Selection As String = drv(lstCategories.DisplayM
That's a bit more convoluted than might really be necessary but, assuming the listibox is bound to a datatable, it should work even if your settings require explicity casting without you having to change a word of it. And then you replace lstCategories.SelectedValu
Roger
Business Accounts
Answer for Membership
by: TheLearnedOnePosted on 2007-08-12 at 07:36:46ID: 19679225
Close. That was syntax for a DataSet, not a DataTable:
If dt.Rows(0)("option1") = "" Then
Bob