Index 0 is not non-negative!
Hello,
I created a webform.aspx with MS visual studio.net 2003.It has a listbox and a textbox.My page loads the listbox with list of name from a table(profile) from the database.when i select a name anomg the one loaded in the listbox, i want it to insert the personalID of the selected name from the same table(Profile) in the database.
After putting the code behind the page and text my web page, it will load the data from the profile table into the listbox successfully but when i select a name from the listbox, it won't insert the corresponding personalID into the textbox.Instead i get an error message that says [B]"Index 0 is not non-negative and below total row".
Please i need some one to help me correct my code.
Here is the code behinde the page:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Me.IsPostBack Then
Dim SqlConnection1 As New System.Data.SqlClient.SqlConnection
SqlConnection1.ConnectionString = "data source=xuzia;initial catalog=Pinfol;user id=sa"
Dim cmd As New SqlClient.SqlCommand
cmd.CommandText = "SELECT PersonalID, Name FROM Profile"
cmd.CommandType = CommandType.Text
cmd.Connection = SqlConnection1
SqlConnection1.Open()
Dim DS As New DataSet
Dim DA As New SqlClient.SqlDataAdapter
DA.SelectCommand = cmd
DA.Fill(DS, "Profile")
ListBox1.DataSource = DS
ListBox1.DataMember = "Profile"
ListBox1.DataTextField = "Name"
ListBox1.DataValueField = "PersonalID"
ListBox1.DataBind()
SqlConnection1.Close()
End If
End Sub.
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim prodID As Integer
prodID = ListBox1.SelectedIndex
Dim SqlConnection1 As New System.Data.SqlClient.SqlConnection
SqlConnection1.ConnectionString = "data source=xuzia;initial catalog=pinfo;user id=sa"
Dim SqlSelectCommand1 As New System.Data.SqlClient.SqlCommand
Dim cmd As New SqlClient.SqlCommand
cmd.CommandText = "SELECT PersonalID, Name FROM Profile WHERE PersonalID = " & prdID
cmd.CommandType = CommandType.Text
cmd.Connection = SqlConnection1
SqlConnection1.Open()
Dim DS As New DataSet
Dim DA As New SqlClient.SqlDataAdapter
DA.SelectCommand = cmd
DA.Fill(DS, "Profile")
txtID.Text = DataBinder.Eval(DS, "Tables[Profile].DefaultView.[0].PersonalID")
txtID.DataBind()
End Sub
I will appreciate all help.
Ktsis.
|