Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 30th, 2006, 12:35 PM
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.




  #2 (permalink)  
Old July 5th, 2006, 09:00 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace the cells having negative value yogeshyl Excel VBA 4 April 29th, 2016 01:43 PM
Index was out of range. Must be non-negative raghavendra_das ASP.NET 2.0 Professional 0 July 24th, 2008 08:16 AM
Making a number negative Corey Access 1 February 2nd, 2007 11:48 AM
C#, Negative Infinity, and Oracle 10g lost C# 0 January 17th, 2006 06:45 PM
negative number returned when subtracting gavmc Classic ASP Basics 2 November 29th, 2005 06:34 AM



All times are GMT -4. The time now is 11:46 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.