p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
I forgot my password Register Now
Register | FAQ | Members List | Calendar | 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 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.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 30th, 2006, 01: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.




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old July 5th, 2006, 10:00 PM
Wrox Author
Points: 12,827, Level: 49
Points: 12,827, Level: 49 Points: 12,827, Level: 49 Points: 12,827, Level: 49
Activity: 15%
Activity: 15% Activity: 15% Activity: 15%
 
Join Date: Oct 2005
Location: Akron, Ohio, USA.
Posts: 4,029
Thanks: 1
Thanked 42 Times in 42 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."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative raghavendra_das ASP.NET 2.0 Professional 0 July 24th, 2008 09:16 AM
Replace the cells having negative value yogeshyl Excel VBA 1 May 6th, 2007 11:45 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 06:27 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc