I am not able to select any value from my dropdownlist,it always returns the first value. Based on the selection the corresponding record should be displayed in a text box. I have given my code here. I am totally frustrated. Pls check it out.
Page_load event
If Not Page.IsPostBack Then
sql = "select scode from supplier"
cmd = New SqlCommand(sql, con)
read = cmd.ExecuteReader
While (read.Read)
ddlscode.Items.Add(read.Item(0))
End While
ddlscode.DataBind()
End If
Private Sub ddlscode_SelectedIndexChanged(...)...
Dim sql As String
Dim i As Integer
Dim tempscode As String
con.Open()
sql = "select * from supplier "
cmd = New SqlCommand(sql, con)
adap = New SqlDataAdapter(cmd)
dt = New DataTable()
adap.Fill(dt)
For i = 0 To dt.Rows.Count - 1
If dt.Rows(i).Item("scode") = ddlscode.SelectedItem.Value Then
txtsaddr.Text = dt.Rows(i).Item("saddr")
End If
Next
read.Read()
read.Close()
con.Close()
End Subtry setting the View state of the control to true.
Set AutoPostback property equal to true.and write this code in page load
if(!page.IsPostback)
{
SqlDataReader rr;
rr=Com.ExecuteReader();
DropDownList1.DataSource=rr;
DropDownList1.DataBind();
}
Select the Value Selected value of DropdownList as
DropDownList1.SelectedItem.Text
I hope this will work but this is in c# convert into Vb
Not sure what's causing you problem, but it might be linked to your loop in the second function. A better way of handling it might be:
Private Sub ddlscode_SelectedIndexChanged(...)...
Dim sql As String
con.Open()
sql = "select saddr from supplier where scode = '" & ddlscode.SelectedItem.Value & "'"
Dim sqlComm As new System.Data.SqlClient.SqlCommand(sql,con);
txtsaddr.Text = sqlComm.ExecuteScalar()
con.Close()
End Sub
The SQL should only return 1 row (I've assumed the selected code is a text value) and the ExecuteScalar call returns the first column (in this case the 'saddr')
Hope this helps,
Nic
0 comments:
Post a Comment