[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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.
0
asifsomi
Asked:
asifsomi
  • 3
  • 3
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
asifsomiAuthor Commented:
Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now