Hi, the exception "Object reference not set to an instance of an object", was thrown when I run my code.
If Not IsPostBack Then
Dim DBConn As OdbcConnection
Dim DBCommand As OdbcDataAdapter
Dim i As Integer
For i = 0 To CarNamesDL.Items.Count - 1
If CarNamesDL.Items(i).Selected Then
DBConn = New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=CarCompaniesDB;User=root;Password=soukpass;Option=3;")
DBCommand = New OdbcDataAdapter("SELECT CarID, Model From CarCompaniesDB where Make = '" & CarNamesDL.Items(i).Text & "'", DBConn)
Else : Exit For
End If
Next
DBCommand.Fill(DS, "CarCompaniesDB")
CarNamesDL.DataSource = DS.Tables("CarCompaniesDB").DefaultView
CarNamesDL.DataBind()
End If
This is the Source error:
Line 43: End If
Line 44: Next
Line 45: DBCommand.Fill(DS, "CarCompaniesDB")
Line 46:
Line 47: CarNames.DataSource = DS.Tables("CarCompaniesDB").DefaultView
I don't know what could be causing this. I don't think its because of notdeclaring a variable before its use 'cause I've checked. I'm not sureif its bad scoping, I've also made sure to Exit the For Loop
in the Else clause in case no match was found. Please take a look to see if my code is missing something or if it is incorrect, thank you in advance for yourhelp.
Hi,
Is your DS anywhere declared and new DataSet created and assigned to DS?
Even if your DS is declared somewhere else or not declared at all, you need to create new DataSet and assign it to DS variable.
DS = New DataSet()
-yuriy
please post complete post.
I dont see that you have initialize any where the "DS" that is DataSet.
Hi, thanks for your reply my code now sort of works but it still has a little problem. The following is my modified code:
If Not IsPostBack Then
Dim DBConn As OdbcConnection
Dim DBCommand As OdbcCommand
Dim i As Integer
Dim DS As New DataSet
For i = 0 To CarNamesDL.Items.Count - 1
If CarNamesDL.Items(i).Selected Then
DBConn = New OdbcConnection("Driver={MySQL ODBC 3.51Driver};Server=myServer;Database=CarCompaniesDB;User=myUser;Password=myPass;Option=3;")
DBCommand = New OdbcCommand("SELECT CarID, Model From CarCompaniesDBwhere Make = '" & CarNamesDL.Items(i).Text & "'", DBConn)
End If
Next
If Not IsNothing(DBConn) Then
DBConn.Open()
DBCommand.Connection = DBConn
Dim AD As New OdbcDataAdapter(DBCommand)
AD.Fill(DS, "CarCompaniesDB")
Me.CarNamesDL.DataTextField = "CarID"
Me.CarNamesDL.DataValueField = "Model"
CarNamesDL.DataBind()
End If
End If
Thisis the code for the button used to display the result, I don't quiteunderstand it since doing something different than what the MSDN saysits suppose to do.
Private Sub Bt_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_OK.Click
lblDataSelected.Text = "Selected Text: " &CarNamesDL.SelectedItem.Text & "<BR> Selected Value: " &CarNamesDL.SelectedValue & "<BR> Selected Index: " &CarNamesDL.SelectedIndex
End Sub
How do I get it to display make "Model" and not just the listitem when a user selects a listitem? Thank you for your help.
0 comments:
Post a Comment