Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to store data from Listbox

How to store data from Listbox

Scheduled Pinned Locked Moved ASP.NET
databasegraphicsdesignhelptutorial
15 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N n_gchaitra

    Hi, I have two listboxes, first listbox is populating the data from the database and then with the help of move button ,i am moving the selscted items from listbox 1 to listbox2 but, I am not able to find any property to he data in listbox2. I want to save them to a table "A_Software" to column "soft". I used the following code. But data it is getting stored is "System.Web.UI.WebControls.ListItemCollection" Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click Dim str As String str = "insert into a_software (soft)values('" & ListBox2.Items.ToString() & "')" Dim comm As New SqlCommand(str, dbconn) If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try End Sub

    Chaitra N

    K Offline
    K Offline
    koolprasad2003
    wrote on last edited by
    #5

    Hello Chaitra.. have a try with following code.. I think u have to save the all items of ListBox2 to database ...is i am correct??? if yes then, dim szItem as new ListBoxItem for each szItem in Listbox2.Items //Now fire your insert query here str = "insert into a_software (soft)values('" & szItem.text & "')" Next hope it will helps.... regards, koolprasad2003:)

    If the message is useful for U then please Rate This message... Be a good listener...Because Opprtunity knoughts softly...N-Joy

    T N 3 Replies Last reply
    0
    • D daniel__c

      You are getting an error because the ListBox2.Items.ToString() function just returns the text System.Web.UI.WebControls.ListItemCollection - because that is what you are referencing....it's just a collection. You would need to loop through each of the ListItems and get out the text value of each and run that as a seperate insert...i.e. foreach(ListItem item in ListBox2.Items){ str = "insert into a_software (soft)values('" & item.Text & "')" //then run the insert } that would be a way to do it using your current code, or build up a collection of inserts and run them all at once (not sure if you can do that with ExecuteNonQuery() though) - running everything at once would definitely be the better option.

      N Offline
      N Offline
      n_gchaitra
      wrote on last edited by
      #6

      I am using vb not c#. foreach(ListItem item in ListBox2.Items) will not work in vb

      Chaitra N

      H 1 Reply Last reply
      0
      • N n_gchaitra

        I am using vb not c#. foreach(ListItem item in ListBox2.Items) will not work in vb

        Chaitra N

        H Offline
        H Offline
        helelark123
        wrote on last edited by
        #7

        In VB.NET --------- For Each item As ListItem In ListBox2.Items 'then run the insert str = "insert into a_software (soft)values('" And item.Text And "')" Next You can convert C# code to VB.NET at: http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx[^]

        Shay Noy

        N 2 Replies Last reply
        0
        • N n_gchaitra

          Hi, I have two listboxes, first listbox is populating the data from the database and then with the help of move button ,i am moving the selscted items from listbox 1 to listbox2 but, I am not able to find any property to he data in listbox2. I want to save them to a table "A_Software" to column "soft". I used the following code. But data it is getting stored is "System.Web.UI.WebControls.ListItemCollection" Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click Dim str As String str = "insert into a_software (soft)values('" & ListBox2.Items.ToString() & "')" Dim comm As New SqlCommand(str, dbconn) If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try End Sub

          Chaitra N

          T Offline
          T Offline
          T EDY
          wrote on last edited by
          #8

          n_gchaitra wrote:

          str = "insert into a_software (soft)values('" & ListBox2.Items.ToString() & "')"

          if u want to avoid SQL injection attack..u have to write u'r query into stored procedure Regards, Tomi

          1 Reply Last reply
          0
          • K koolprasad2003

            Hello Chaitra.. have a try with following code.. I think u have to save the all items of ListBox2 to database ...is i am correct??? if yes then, dim szItem as new ListBoxItem for each szItem in Listbox2.Items //Now fire your insert query here str = "insert into a_software (soft)values('" & szItem.text & "')" Next hope it will helps.... regards, koolprasad2003:)

            If the message is useful for U then please Rate This message... Be a good listener...Because Opprtunity knoughts softly...N-Joy

            T Offline
            T Offline
            T EDY
            wrote on last edited by
            #9

            koolprasad2003 wrote:

            str = "insert into a_software (soft)values('" & szItem.text & "')"

            writing a query in your code, make u wide open to SQL Injection attack..please tell me that is only for example explanation:confused: Kind Regards, Tomi

            1 Reply Last reply
            0
            • H helelark123

              In VB.NET --------- For Each item As ListItem In ListBox2.Items 'then run the insert str = "insert into a_software (soft)values('" And item.Text And "')" Next You can convert C# code to VB.NET at: http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx[^]

              Shay Noy

              N Offline
              N Offline
              n_gchaitra
              wrote on last edited by
              #10

              Following is the code i have written, But it is giving the error, ExecuteNonQuery: CommandText property has not been initialized Partial Class _Default Inherits System.Web.UI.Page Dim constr As String = ConfigurationSettings.AppSettings("conn") Dim dbconn As New SqlConnection(constr) Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Button1.Attributes.Add("onclick", "return fnMoveItems('ListBox1','ListBox2')") Button2.Attributes.Add("onclick", "return fnMoveItems('ListBox2','ListBox1')") End Sub Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click Dim str As String If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In ListBox2.Items str = "insert into a_software (soft)values('" & item.Text & "')" Next Dim comm As New SqlCommand(str, dbconn) Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try End Sub End Class

              Chaitra N

              1 Reply Last reply
              0
              • K koolprasad2003

                Hello Chaitra.. have a try with following code.. I think u have to save the all items of ListBox2 to database ...is i am correct??? if yes then, dim szItem as new ListBoxItem for each szItem in Listbox2.Items //Now fire your insert query here str = "insert into a_software (soft)values('" & szItem.text & "')" Next hope it will helps.... regards, koolprasad2003:)

                If the message is useful for U then please Rate This message... Be a good listener...Because Opprtunity knoughts softly...N-Joy

                N Offline
                N Offline
                n_gchaitra
                wrote on last edited by
                #11

                Following is the code i have written, But it is giving the error, ExecuteNonQuery: CommandText property has not been initialized Partial Class _Default Inherits System.Web.UI.Page Dim constr As String = ConfigurationSettings.AppSettings("conn") Dim dbconn As New SqlConnection(constr) Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Button1.Attributes.Add("onclick", "return fnMoveItems('ListBox1','ListBox2')") Button2.Attributes.Add("onclick", "return fnMoveItems('ListBox2','ListBox1')") End Sub Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click Dim str As String If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In ListBox2.Items str = "insert into a_software (soft)values('" & item.Text & "')" Next Dim comm As New SqlCommand(str, dbconn) Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try End Sub End Class

                Chaitra N

                K 1 Reply Last reply
                0
                • N n_gchaitra

                  Following is the code i have written, But it is giving the error, ExecuteNonQuery: CommandText property has not been initialized Partial Class _Default Inherits System.Web.UI.Page Dim constr As String = ConfigurationSettings.AppSettings("conn") Dim dbconn As New SqlConnection(constr) Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Button1.Attributes.Add("onclick", "return fnMoveItems('ListBox1','ListBox2')") Button2.Attributes.Add("onclick", "return fnMoveItems('ListBox2','ListBox1')") End Sub Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click Dim str As String If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In ListBox2.Items str = "insert into a_software (soft)values('" & item.Text & "')" Next Dim comm As New SqlCommand(str, dbconn) Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try End Sub End Class

                  Chaitra N

                  K Offline
                  K Offline
                  koolprasad2003
                  wrote on last edited by
                  #12

                  u have to take following line in for each..Next loop

                  n_gchaitra wrote:

                  comm.ExecuteNonQuery()

                  and the following statement move above the connection.Open()

                  n_gchaitra wrote:

                  Dim comm As New SqlCommand(str, dbconn)

                  make the changes and First check that connection is successfully open or not regards, koolprasad2003:) Be a good listener...Because Opprtunity knoughts softly...N-Joy

                  N 1 Reply Last reply
                  0
                  • K koolprasad2003

                    u have to take following line in for each..Next loop

                    n_gchaitra wrote:

                    comm.ExecuteNonQuery()

                    and the following statement move above the connection.Open()

                    n_gchaitra wrote:

                    Dim comm As New SqlCommand(str, dbconn)

                    make the changes and First check that connection is successfully open or not regards, koolprasad2003:) Be a good listener...Because Opprtunity knoughts softly...N-Joy

                    N Offline
                    N Offline
                    n_gchaitra
                    wrote on last edited by
                    #13

                    How is it possible, use comm.executenonquey() before declaring comm? I get the following message near the word str i.e Dim comm As New SqlCommand(str, dbconn) var 'str' is used before it has been assigned a value. A null reference exception could result at runtime

                    Chaitra N

                    1 Reply Last reply
                    0
                    • H helelark123

                      In VB.NET --------- For Each item As ListItem In ListBox2.Items 'then run the insert str = "insert into a_software (soft)values('" And item.Text And "')" Next You can convert C# code to VB.NET at: http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx[^]

                      Shay Noy

                      N Offline
                      N Offline
                      n_gchaitra
                      wrote on last edited by
                      #14

                      Hi, This function is working, But the each value is getting stored in different row of the table. I want them to get stored in a single row. Dim str As String If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In lstselectedemployees.Items str = "insert into a (soft)values('" & item.Text & "')" Dim comm As New SqlCommand(str, dbconn) Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try Next If I use the following code then, only the last item of the list box is getting stored, Dim str As String If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In lstselectedemployees.Items str = "insert into a (soft)values('" & item.Text & "')" next Dim comm As New SqlCommand(str, dbconn) Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try

                      Chaitra N

                      1 Reply Last reply
                      0
                      • K koolprasad2003

                        Hello Chaitra.. have a try with following code.. I think u have to save the all items of ListBox2 to database ...is i am correct??? if yes then, dim szItem as new ListBoxItem for each szItem in Listbox2.Items //Now fire your insert query here str = "insert into a_software (soft)values('" & szItem.text & "')" Next hope it will helps.... regards, koolprasad2003:)

                        If the message is useful for U then please Rate This message... Be a good listener...Because Opprtunity knoughts softly...N-Joy

                        N Offline
                        N Offline
                        n_gchaitra
                        wrote on last edited by
                        #15

                        Hi, This function is working, But the each value is getting stored in different row of the table. I want them to get stored in a single row. Dim str As String If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In lstselectedemployees.Items str = "insert into a (soft)values('" & item.Text & "')" Dim comm As New SqlCommand(str, dbconn) Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try Next If I use the following code then, only the last item of the list box is getting stored, Dim str As String If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In lstselectedemployees.Items str = "insert into a (soft)values('" & item.Text & "')" next Dim comm As New SqlCommand(str, dbconn) Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try

                        Chaitra N

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups