Select Index Even Or DropDownListt Not Work

Below is my code on the click event of a button, this code work well.

--------------------------Code start here-----------on button click----------
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            ConnectWithDatabase() ' a module that contains connection information
            DataAdapter = New SqlDataAdapter("select  * from customersupplier", CONN)
            DataAdapter.Fill(ds, "customer")
            cmbcustomer.DataSource = ds.Tables(0)
            cmbcustomer.DataValueField = "name"
            cmbcustomer.DataBind()
        Catch ex As Exception
            lbl.Text = ex.Message
        Finally
            Disconnectwithdatabase()
        End Try
    End Sub
------------------------End of code--------------button click---------

below is code on selected index event of dropdown list
-------------------------start here---------this code not work---
 Private Sub cmbcustomer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbcustomer.SelectedIndexChanged
        Try
            ConnectWithDatabase()
            DataAdapter = New SqlDataAdapter("select  * from customersupplier where id=" &       ds.Tables(0).Rows(cmbcustomer.SelectedIndex).Item("id"), CONN)
            DataAdapter.Fill(ds, "detail")
            dg.DataSource = ds.Tables(1)
            dg.DataBind()
        Catch ex As Exception
            lbl.Text = ex.Message
        Finally
            Disconnectwithdatabase()
        End Try
    End Sub
-----------end of code--------------

what i want.

when a customer is selected from drop down list i want to display his history in datagrid control (control name is dg). but the selected event is not work, i place a watch on this block and observe that when i select the customer from dropdownlist its control is not trasfer to the select index event.

plz help me immediatly.
asifsomiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ihenryCommented:
Have you set the cmbcustomer DropDownList Autopostback property to true? like the following,

<asp:DropDownList id="cmbcustomer" AutoPostBack="True" runat="server">
           <asp:ListItem Text= "1st item" />
           <asp:ListItem Text= "2nd item" />
           <asp:ListItem Text= "3rd item" />
</asp:DropDownList>
0
asifsomiAuthor Commented:
thanks ,

now event is calling succesfully but the values in ds.tables(0) is lost. see my code on selected event of drop down list, i get the information on the basis of id which is store in ds.table(0), so when i select the item from list (after setting autopostback=true) the event call but i get the exception
"Cant Find Table(0)"

i declare the Ds on the top of the form.

plz help me
0
vinhthuy_nguyenCommented:
Hi ,
I think the problem is in this :
select  * from customersupplier where id=" &       ds.Tables(0).Rows(cmbcustomer.SelectedIndex).Item("id"),
Did you mean that select where id = the selected value in cmbcustomer ?
in cmbcustomer, I think you should give the datatextfield to "name" and dataaluefield to id :
DataTextfield = "name"  //your customer name
Datavaluefield = "id"      //your customer id
and your query will be :
select  * from customersupplier where id=" & cmbcustomer.Selectedvalue

Pls check.Hope this helps
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ihenryCommented:

The reason being is that, any variable that's declared on the top of form does not retain its value between postback. So you have two options.

First, in the cmbcustomer_SelectedIndexChanged event, requery customersupplier information from database and fill resultset to the dataset.

Or, in the Button1_Click event. Store the ds variable in a storage like session or viewstate and reuse it in the cmbcustomer_SelectedIndexChanged event.
0
asifsomiAuthor Commented:
thanks
plz tell me how to store and use value in session and viewstate.
0
ihenryCommented:
First remove ds variable declaration (you should have it somewhere) and do the following

In the page_load event,
......
......
' add this line
Dim ds As New DataSet
DataAdapter.Fill(ds, "customer")
' and add this line
Session("customerData") = ds
cmbcustomer.DataSource = ds.Tables(0)
cmbcustomer.DataValueField = "name"
cmbcustomer.DataBind()
......
......

and in the cmbcustomer_SelectedIndexChanged event
' also this line
Dim ds As DataSet = CType( Session( "customerData" ), DataSet )
ConnectWithDatabase()
DataAdapter = New SqlDataAdapter("select  * from customersupplier where id=" &       ds.Tables(0).Rows(cmbcustomer.SelectedIndex).Item("id"), CONN)
DataAdapter.Fill(ds, "detail")
dg.DataSource = ds.Tables(1)
dg.DataBind()
......
......
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
asifsomiAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.