Avatar of smm6809
smm6809
 asked on

vb.net dataset errors connecting to SQL server and displaying fields on form.

New to SQL and VB.Net but have used Access and VBA for years. I have a couple of questions regarding the code below. I get an error saying there is no row at position 0. I don't even know what that means, let alone what to do about it. Also, I have 3 comboboxes on the form I am trying to populate. They fill automatically when the form loads, but I want them to select the item that represents the specific record in the database. For example: I have 10 stores in the dropdown, but the record I am trying to retrieve is associated with the ID 5. It's DisplayMember is the name of the store, but its ValueMember is an ID. I want the combobox to select the the ID associated with the record in SQL and then display the DisplayMember correctly. Can I do this? Here is the code:
Public Sub psFillDataset()
        Dim conn As New SqlConnection(sqlComm)
        Try
            Dim sSQL As String
            sSQL = "Select * from tvwLeads where numLeadID=" & numSelected & " order by numLeadID"
            MsgBox(numSelected)
            Dim dbadp As New SqlDataAdapter(sSQL, sqlComm)
            Dim dTable As New DataTable
            dbadp.Fill(dTable)
            dbadp.Dispose()
            Me.txtSource.DataSource = dTable
            Me.txtType.Text = ("strType").ToString()
            Me.txtClient.Text = ("numClient").ToString()
            Me.txtLast.Text = dTable.Rows(0)("strLast").ToString()
            Me.txtFirst.Text = dTable.Rows(0)("strFirst").ToString()
            Me.tmTO.Text = dTable.Rows(0)("tmTO").ToString()
            Me.dtTO.Text = dTable.Rows(0)("dtTO").ToString()
            Me.tmCT.Text = dTable.Rows(0)("tmCTC").ToString()
            Me.dtCT.Text = dTable.Rows(0)("dtCTC").ToString()
            Me.chkBad.Text = dTable.Rows(0)("ysnBad").ToString()
            Me.txtManager.Text = dTable.Rows(0)("strManager").ToString()
            Me.cboSales.Text = dTable.Rows(0)("strName").ToString()
            Me.txtNotes.Text = dTable.Rows(0)("strNotes").ToString()
            Me.txtEmail.Text = dTable.Rows(0)("strResponse").ToString()
            Me.txtID.Text = dTable.Rows(0)("numLeadID").ToString()
            Me.txtSource.DisplayMember = "strSource"
            Me.cboVehicle.DisplayMember = "strLeadType"
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
Visual Basic.NETMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
smm6809

8/22/2022 - Mon
Éric Moreau

>> I get an error saying there is no row at position 0

that means that your select query returns no row and you are trying to update the first row (index = 0)
smm6809

ASKER
Thanks, I was using the wrong table. More importantly to me is what I'm trying to do with the comboboxes. Possible?
ASKER CERTIFIED SOLUTION
Éric Moreau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
smm6809

ASKER
Thank you so much! One last question. These two fields are not displaying the value in the textbox. They are displaying strType and numClient. However, the information is in the database and the field names are correctly spelled. Any thoughts?

Me.txtType.Text = ("strType").ToString()
 Me.txtClient.Text = ("numClient").ToString()
Your help has saved me hundreds of hours of internet surfing.
fblack61
Éric Moreau

because you simply set those values!

your code surely needs to be:

Me.txtType.Text = dTable.Rows(0)("strType").ToString()
 Me.txtClient.Text = dTable.Rows(0)("numClient").ToString()

Open in new window

smm6809

ASKER
Haha so I did! How do I choose the best solution from you when they all helped? And thank you!
smm6809

ASKER
Some of my comboboxes still not populating, but they're just filled in with items, not connected to the database. Do I still handle them the same with selected value? If so, they aren't working. Also, I have 2 checkboxes that arent populating. The database shows them as 0s and 1s. Are they handled differently?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Éric Moreau

how and where do you feed those combos?

as for checkboxes, you need special code:
Me.chkBad.Checked = (dTable.Rows(0)("ysnBad").ToString() = "1")

Open in new window

smm6809

ASKER
Thanks. This is how I fill the comboboxes on the ones that aren't populating:
cboVehicle.Items.Clear()
            cboVehicle.DisplayMember = "Text"
            cboVehicle.Items.Add(New With {.Text = "NEW"})
            cboVehicle.Items.Add(New With {.Text = "USED"})

            cboContact.Items.Clear()
            cboContact.DisplayMember = "Text"
            cboContact.Items.Add(New With {.Text = "LE"})
            cboContact.Items.Add(New With {.Text = "L/M & LE"})
            cboContact.Items.Add(New With {.Text = "Not Determined"})
            cboContact.Items.Add(New With {.Text = "YES"})
            cboContact.Items.Add(New With {.Text = "NO"})

Then I use Me.cboContact.SelectedValue = dTable.Rows(0)("strContact").ToString()
Me.cboVehicle.SelectedValue = dTable.Rows(0)("strVehicle").ToString()
to put them back into the form.
Éric Moreau

in this case, you can simplify the syntax to:
cboVehicle.Items.Clear()
cboVehicle.Items.Add("NEW")
cboVehicle.Items.Add("USED")

Open in new window


then you will be able to use:
Me.cboVehicle.Text = dTable.Rows(0)("strLeadType").tostring

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
smm6809

ASKER
Perfect, but checkboxes still not working. If I'm understanding you correctly, I place that code in the same spot as all the other code we worked on and if it is a 1 in the database, it should show as checked on the form. If so, it isn't showing as such. Also, if there is a null value will I get the message that there is no row at position 0? I am getting it for certain records that I know have some data but not all of the fields. How do you handle that?
Éric Moreau

NULLs are a special value and you need to process them specially! You won't get an exception saying that there is no row at position 0 because this is really when there are no rows in your datatable.

To check for NULLs, you should:
if dTable.Rows(0)("strType") isnot nothing then
     Me.txtType.Text = dTable.Rows(0)("strType").ToString()
end if

Open in new window


For checkboxes, you can try:
if dTable.Rows(0)("ysnBad") is nothing orelse dTable.Rows(0)("ysnBad").ToString() = "0") then
   Me.chkBad.Checked = false
else
   Me.chkBad.Checked = true
end if

Open in new window

smm6809

ASKER
That code looks exactly right but now it checks the boxes are always checked. Ugh, I want to go back to VBA. Do you know a good book that gets into this level of coding?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Éric Moreau

what do you get if you do :
messagebox.show(dTable.Rows(0)("ysnBad").ToString()) 

Open in new window

chances are that you won't see 0 and 1 like you told me before!
smm6809

ASKER
Got error before running: argument not specified for parameter "Prompt" of "Publci Function MsgBox(Prompt as Object,[Buttons as MsgboxStyle=ApplicationModal],Title As Object=Nothing]) as MsgBoxResult
Éric Moreau

can you copy here your code? because mine is not using msgbox!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
smm6809

ASKER
Re-pasted what you sent. I get true, so I fixed the code. I appreciate your time today.