DataSet, Object Reference error

K

kawade

Hi All,
Here's what I'm trying to do. I want to use a DataSet with 2
tables(Clients, Contacts) to bind to DropDownLists. When a Client is
selected I execute a subroutine to create a DataView of Contacts
filtered with the ClientID that was selected from the 1st
DropDownList. I'm getting the error "Object reference not set to an
instance of an object" when I try this. Would someone help me
understand what I'm doing wrong?

Dim myDataSet as New DataSet()
Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim dbConn As SqlConnection = new
SqlConnection(ConfigurationSettings.AppSettings().Item("ProdDB"))

Dim mySQL1 as string ="Exec asp_GetClients "
& _
"; " & _
"Exec asp_GetContacts "
Dim myDataAdapter as New SqlDataAdapter(mySQL1,
dbConn)
myDataAdapter.TableMappings.Add("Table",
"Clients")
myDataAdapter.TableMappings.Add("Table1",
"Contacts")
myDataAdapter.Fill(myDataSet)
Clients.DataSource =
myDataSet.Tables("Clients")
Clients.DataBind()
Clients.Items.Insert(0, "Select
Company...")
Clients.SelectedIndex = 0
End If
End Sub


This is the subroutine that is run after a client is selected from the
first dropdownlist. This is that point where the error occurs.
[code:1:d0c95ced9c]
Sub New_Client_Selected(sender As Object, e As EventArgs)
Dim ContactsView as DataView =
myDataSet.Tables("Contacts").DefaultView
ContactsView.RowFilter = "ClientID='" &
Clients.SelectedItem.ToString() & "'"
Contacts.DataSource = ContactsView
Contacts.DataBind()
End Sub
[/code:1:d0c95ced9c]

Is a DataSet not what I should be using? I can do what I want with
DataReader but I have to make multiple db calls that way. I was
hoping to avoid that because this is not the only instance of what
I'm trying to do in this app. Thanks in advance for the help!
-kawade
 
J

Joyjit Mukherjee

Hi,

I think what really happening is the second ddlist is getting a null value
from the first ddlist as selecteditem property returns the lowest indexed
item from the list. Try Clients.SelectedItem.Value.ToString() in your second
subroutine. Hope that would help.

Regards
Joyjit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top