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
  1. Home
  2. Web Development
  3. ASP.NET
  4. append the listitems as a comma seperated string

append the listitems as a comma seperated string

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabasegraphicstutorial
13 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 Offline
    N Offline
    n_gchaitra
    wrote on last edited by
    #1

    how to append the listitems as a comma seperated string and insert into the database in a single row using asp.net 2.0 I am using vb. I am copying a content of listbox1 to listbox2. I want to store the listbox2 data into database. I am using the following code, 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

    J N realJSOPR 3 Replies Last reply
    0
    • N n_gchaitra

      how to append the listitems as a comma seperated string and insert into the database in a single row using asp.net 2.0 I am using vb. I am copying a content of listbox1 to listbox2. I want to store the listbox2 data into database. I am using the following code, 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

      J Offline
      J Offline
      jc net
      wrote on last edited by
      #2

      Dim str As String If dbconn.State ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In lstselectedemployees.Items STR = STR & "," & item.Text Next str2="insert into a (soft)values('" & STR & "')" Dim comm As New SqlCommand(str2, dbconn) Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try

      N 2 Replies Last reply
      0
      • J jc net

        Dim str As String If dbconn.State ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In lstselectedemployees.Items STR = STR & "," & item.Text Next str2="insert into a (soft)values('" & STR & "')" Dim comm As New SqlCommand(str2, dbconn) Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try

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

        Thank you JC. This is working, But it store a ',' in the very beginning. How to stop this. Example, if the items are, A B C It stores ,A,B,C

        Chaitra N

        M B J 3 Replies Last reply
        0
        • N n_gchaitra

          Thank you JC. This is working, But it store a ',' in the very beginning. How to stop this. Example, if the items are, A B C It stores ,A,B,C

          Chaitra N

          M Offline
          M Offline
          Malcolm Smart
          wrote on last edited by
          #4

          Oh come on. He's done the hard bit for you. Try doing at least some of it yourself.

          "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

          1 Reply Last reply
          0
          • N n_gchaitra

            Thank you JC. This is working, But it store a ',' in the very beginning. How to stop this. Example, if the items are, A B C It stores ,A,B,C

            Chaitra N

            B Offline
            B Offline
            BasharatAli
            wrote on last edited by
            #5

            **Dim str As String = ""** If dbconn.State ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In lstselectedemployees.Items **if str = "" then str = item.Text else STR = STR & "," & item.Text end if** Next str2="insert into a (soft)values('" & STR & "')" Dim comm As New SqlCommand(str2, dbconn) Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try Try Bold text

            BasharatAli Internee METESYS Lahore Pakistan

            N 1 Reply Last reply
            0
            • N n_gchaitra

              how to append the listitems as a comma seperated string and insert into the database in a single row using asp.net 2.0 I am using vb. I am copying a content of listbox1 to listbox2. I want to store the listbox2 data into database. I am using the following code, 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

              N Offline
              N Offline
              Nand Kumar Das
              wrote on last edited by
              #6

              First u should convert list items into one string seperated by comm(,) and stored it in a string type variable with concatenation. and then stored the variable into datbase field. Use this code for converting global decalartion dim i as int dim st as string for i=0 to i<=ListBox1.item.count-1 if i<>ListBox1.item.count-1 then st +=ListBox1.item(i) & "," else st +=ListBox1.item(i) End if Next BY Nand Nand

              N 1 Reply Last reply
              0
              • N n_gchaitra

                Thank you JC. This is working, But it store a ',' in the very beginning. How to stop this. Example, if the items are, A B C It stores ,A,B,C

                Chaitra N

                J Offline
                J Offline
                jc net
                wrote on last edited by
                #7

                Well, if you want me to do all the work, it might help if you rated my reply ;) hint: use the mid-function

                N 1 Reply Last reply
                0
                • N n_gchaitra

                  how to append the listitems as a comma seperated string and insert into the database in a single row using asp.net 2.0 I am using vb. I am copying a content of listbox1 to listbox2. I want to store the listbox2 data into database. I am using the following code, 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

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  Learn how to properly post code snippets here at CP.

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  J 1 Reply Last reply
                  0
                  • B BasharatAli

                    **Dim str As String = ""** If dbconn.State ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In lstselectedemployees.Items **if str = "" then str = item.Text else STR = STR & "," & item.Text end if** Next str2="insert into a (soft)values('" & STR & "')" Dim comm As New SqlCommand(str2, dbconn) Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try Try Bold text

                    BasharatAli Internee METESYS Lahore Pakistan

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

                    Thank u. It is working.

                    Chaitra N

                    1 Reply Last reply
                    0
                    • J jc net

                      Well, if you want me to do all the work, it might help if you rated my reply ;) hint: use the mid-function

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

                      Thank u JC. I gotta know the code. Your code really helped me.

                      Chaitra N

                      1 Reply Last reply
                      0
                      • realJSOPR realJSOP

                        Learn how to properly post code snippets here at CP.

                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                        -----
                        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                        J Offline
                        J Offline
                        jc net
                        wrote on last edited by
                        #11

                        I would like to learn that, any links to where it is explained?

                        1 Reply Last reply
                        0
                        • J jc net

                          Dim str As String If dbconn.State ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In lstselectedemployees.Items STR = STR & "," & item.Text Next str2="insert into a (soft)values('" & STR & "')" Dim comm As New SqlCommand(str2, dbconn) Try comm.ExecuteNonQuery() Label1.ForeColor = Drawing.Color.Green Label1.Text = "Success" Catch ex As Exception Label1.Text = ex.Message End Try

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

                          I have one more doubt. This is regarding bindng of listbox. Let us assume I store A B C D in from listbox2. It stores as A,B,C,D . When i bind the data to listbox it comes as the same. But i want to display each value in different row. How to do this? I tried split function but no use. Here is the code snippet i have used to bind the data to listbox, lstselectedemployees is the listbox. Dim sql As String = "Select * from A_desktops where assetcode='" & TextBox1.Text & "'" Dim da As New SqlDataAdapter(sql, dbconn) Dim ds As New DataSet() da.Fill(ds, "a_desktops") lstselectedemployees.DataSource = ds.Tables("a_desktops").DefaultView lstselectedemployees.SelectedIndex = 0 lstselectedemployees.DataTextField = "swinstalled" Page.DataBind()

                          Chaitra N

                          1 Reply Last reply
                          0
                          • N Nand Kumar Das

                            First u should convert list items into one string seperated by comm(,) and stored it in a string type variable with concatenation. and then stored the variable into datbase field. Use this code for converting global decalartion dim i as int dim st as string for i=0 to i<=ListBox1.item.count-1 if i<>ListBox1.item.count-1 then st +=ListBox1.item(i) & "," else st +=ListBox1.item(i) End if Next BY Nand Nand

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

                            I have one more doubt. This is regarding bindng of listbox. Let us assume I store A B C D in from listbox2. It stores as A,B,C,D . When i bind the data to listbox it comes as the same. But i want to display each value in different row. How to do this? I tried split function but no use. Here is the code snippet i have used to bind the data to listbox, lstselectedemployees is the listbox. Dim sql As String = "Select * from A_desktops where assetcode='" & TextBox1.Text & "'" Dim da As New SqlDataAdapter(sql, dbconn) Dim ds As New DataSet() da.Fill(ds, "a_desktops") lstselectedemployees.DataSource = ds.Tables("a_desktops").DefaultView lstselectedemployees.SelectedIndex = 0 lstselectedemployees.DataTextField = "swinstalled" Page.DataBind()

                            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