How to store data from Listbox
-
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
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
-
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.
I am using vb not c#. foreach(ListItem item in ListBox2.Items) will not work in vb
Chaitra N
-
I am using vb not c#. foreach(ListItem item in ListBox2.Items) will not work in vb
Chaitra N
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
-
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
-
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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