|
|
 |
| ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

June 30th, 2006, 01:35 PM
|
|
Registered User
|
|
Join Date: May 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

July 5th, 2006, 10:00 PM
|
|
Wrox Author
Points: 12,827, Level: 49 |
|
|
Join Date: Oct 2005
Location: Akron, Ohio, USA.
Posts: 4,029
Thanks: 1
Thanked 42 Times in 42 Posts
|
|
Well the potential problem you run into here is is that a listbox can have multiple selections. Try something like this:
Dim item as ListItem
Dim i as integer
For each item in ListBox.Items
If item.selected then
prodID = item.selectedValue
i += 1
End If
Next
if i >= 2 then exit sub
or you could do this in the event of multiple selections
Dim item as ListItem
Dim strID as as string
For each item in ListBox.Items
If item.selected then
strID += item.selectedValue & ","
End If
Next
strID = strID.TrimEnd(",")
Then your SQL statement is this:
"SELECT PersonalID, Name FROM Profile WHERE PersonalID IN(" & strID &")"
HtH
"The one language all programmers understand is profanity."
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |