Checkboxlist
-
Hi, I am storing the checkboxlist items as comma seperated values in a row. My problem is during updation of those values. When i bind the values to the checkboxlist i want to display all items but only those items have to be checked which are present in the row. For inserting i am using the followng code, Dim sqlstr As String Dim str As String = "" If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In CheckBoxList1.Items If item.Selected = True Then If str = "" Then str = item.Text Else str = str & "," & item.Text End If End If Next sqlstr = "insert into test(c1) values(@c)" Dim comm As New SqlCommand(sqlstr, dbconn) comm.Parameters.Add("@c", SqlDbType.Char, 10).Value = str Try comm.ExecuteNonQuery() Label1.Visible = True Label1.Text = "Success" Catch ex As Exception Label1.Visible = True Label1.Text = ex.ToString End Try
-
Hi, I am storing the checkboxlist items as comma seperated values in a row. My problem is during updation of those values. When i bind the values to the checkboxlist i want to display all items but only those items have to be checked which are present in the row. For inserting i am using the followng code, Dim sqlstr As String Dim str As String = "" If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If For Each item As ListItem In CheckBoxList1.Items If item.Selected = True Then If str = "" Then str = item.Text Else str = str & "," & item.Text End If End If Next sqlstr = "insert into test(c1) values(@c)" Dim comm As New SqlCommand(sqlstr, dbconn) comm.Parameters.Add("@c", SqlDbType.Char, 10).Value = str Try comm.ExecuteNonQuery() Label1.Visible = True Label1.Text = "Success" Catch ex As Exception Label1.Visible = True Label1.Text = ex.ToString End Try
n_gchaitra wrote:
My problem is during updation of those values.
I'm not sure what problem you are facing... Can you tell us more about the problem you are facing and what you wanna do? :)
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)
-
n_gchaitra wrote:
My problem is during updation of those values.
I'm not sure what problem you are facing... Can you tell us more about the problem you are facing and what you wanna do? :)
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)
Michael, I have around 20 items in which only checked items have to stored. It is working fine. When i want to update that obviously i have to see all those twenty items. Here i want to them to compare with the items present in the table and the matching rows have to get checked.
Chaitra N
-
Michael, I have around 20 items in which only checked items have to stored. It is working fine. When i want to update that obviously i have to see all those twenty items. Here i want to them to compare with the items present in the table and the matching rows have to get checked.
Chaitra N
Is that what you want?
Listbox.DataSource = Database.GetAllItems(); Listbox.DataBind(); string[] storedValues = Database.GetStoredItems().Split(","); foreach(string val in storedValues){ foreach ( ListItem itm in ListBox1.Items ){ if(itm.Value == val){ itm.Checked = true; } } }
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)
-
Is that what you want?
Listbox.DataSource = Database.GetAllItems(); Listbox.DataBind(); string[] storedValues = Database.GetStoredItems().Split(","); foreach(string val in storedValues){ foreach ( ListItem itm in ListBox1.Items ){ if(itm.Value == val){ itm.Checked = true; } } }
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)
Michael I am afraid i did not understood what is "Database" in Listbox.DataSource = Database.GetAllItems(); Listbox.DataBind(); string[] storedValues = Database.GetStoredItems().Split(","); The code i am using to bind the values are as follows. This works fine. I want only matching values to get checked. Can u help me please? Dim constr As String = ConfigurationSettings.AppSettings("conn") Dim dbconn As New SqlConnection(constr) Sub bind() Dim sqlstr As String sqlstr = "select software from list where sno='" & txtsno.text & "'" Dim comm As New SqlCommand(sqlstr, dbconn) Dim dr As SqlDataReader If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If dr = comm.ExecuteReader While dr.Read CheckBoxList1.Items.Add(dr.Item(0).ToString) End While dr.Close() End Sub
Chaitra N
-
Michael I am afraid i did not understood what is "Database" in Listbox.DataSource = Database.GetAllItems(); Listbox.DataBind(); string[] storedValues = Database.GetStoredItems().Split(","); The code i am using to bind the values are as follows. This works fine. I want only matching values to get checked. Can u help me please? Dim constr As String = ConfigurationSettings.AppSettings("conn") Dim dbconn As New SqlConnection(constr) Sub bind() Dim sqlstr As String sqlstr = "select software from list where sno='" & txtsno.text & "'" Dim comm As New SqlCommand(sqlstr, dbconn) Dim dr As SqlDataReader If dbconn.State <> ConnectionState.Open Then dbconn.Open() End If dr = comm.ExecuteReader While dr.Read CheckBoxList1.Items.Add(dr.Item(0).ToString) End While dr.Close() End Sub
Chaitra N
Database should be a helper class that return the dataset after retrieving the data from database.
n_gchaitra wrote:
I want only matching values to get checked.
What you meant is that there are the values (1,2,3) in database. There are 5 items (1,2,3,4,5) shown in ListBox. After retrieving the data (1,2,4) from database, you want the items (1,2,3) to be checked. Is it what you want? Let's
Database.GetAllItems();
will return all data (1,2,3,4,5) from database. this methodDatabase.GetStoredItems()
will return all saved value as a comma seperated value (eg: 1,2,3) eg:public class Database{ public static string GetStoredItems(){ //Get the data from Database //Return those data as comman seperated value return str; // 1,2,3 } public static DataSet GetAllItems()(){ //Get the data from Database //Return those data as DataSet return ds; // 1,2,3,4,5 } }
then, bind it to Listbox. (So, 1,2,3,4,5 will be shown in Listbox. )Listbox.DataSource = Database.GetAllItems(); Listbox.DataBind();
then, get the saved values from database. (In our case, we will get "1,2,3"). Split to string array. loop through the listbox and string array to find the match. If match found, we setitm.Checked = true;
string[] storedValues = Database.GetStoredItems().Split(","); foreach(string val in storedValues){ foreach ( ListItem itm in ListBox1.Items ){ if(itm.Value == val){ itm.Checked = true; } } }
Is the way I understand correct? Hopefully, you got my point...Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)
-
Database should be a helper class that return the dataset after retrieving the data from database.
n_gchaitra wrote:
I want only matching values to get checked.
What you meant is that there are the values (1,2,3) in database. There are 5 items (1,2,3,4,5) shown in ListBox. After retrieving the data (1,2,4) from database, you want the items (1,2,3) to be checked. Is it what you want? Let's
Database.GetAllItems();
will return all data (1,2,3,4,5) from database. this methodDatabase.GetStoredItems()
will return all saved value as a comma seperated value (eg: 1,2,3) eg:public class Database{ public static string GetStoredItems(){ //Get the data from Database //Return those data as comman seperated value return str; // 1,2,3 } public static DataSet GetAllItems()(){ //Get the data from Database //Return those data as DataSet return ds; // 1,2,3,4,5 } }
then, bind it to Listbox. (So, 1,2,3,4,5 will be shown in Listbox. )Listbox.DataSource = Database.GetAllItems(); Listbox.DataBind();
then, get the saved values from database. (In our case, we will get "1,2,3"). Split to string array. loop through the listbox and string array to find the match. If match found, we setitm.Checked = true;
string[] storedValues = Database.GetStoredItems().Split(","); foreach(string val in storedValues){ foreach ( ListItem itm in ListBox1.Items ){ if(itm.Value == val){ itm.Checked = true; } } }
Is the way I understand correct? Hopefully, you got my point...Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)
i am getting "System.Data.DataRowView " error. I tried to write the function in the same class but still getting the same error. Dim constr As String = ConfigurationSettings.AppSettings("conn") Dim dbconn As New SqlConnection(constr) Sub bind1() CheckBoxList1.DataSource = GetAllItems() CheckBoxList1.DataBind() Dim storedValues As String() = Database.GetStoredItems().Split(",") For Each val As String In storedValues For Each itm As ListItem In CheckBoxList1.Items If itm.Value = val Then itm.Selected = True End If Next Next End Sub Public Function GetAllItems() As DataSet Dim ds As New DataSet() Dim sqlstr As String = " select list from AList" Dim comm As New SqlDataAdapter(sqlstr, dbconn) comm.Fill(ds) 'Return those data as DataSet Return ds ' 1,2,3,4,5 End Function Public Function GetStoredItems() As String 'Get the data from Database ' Dim dbconn As New OleDb.OleDbConnection("provider=sqloledb;user id=sa;password=;trusted_connection=yes; initial catalog=wfphumanet;data source=localhost") Dim ds As New DataSet() Dim str As String = " select c1 from test where sl='" & TextBox1.text & "'" 'Return those data as comman seperated value Return str ' 1,2,3 End Function Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click bind1() End Sub
Chaitra N
-
i am getting "System.Data.DataRowView " error. I tried to write the function in the same class but still getting the same error. Dim constr As String = ConfigurationSettings.AppSettings("conn") Dim dbconn As New SqlConnection(constr) Sub bind1() CheckBoxList1.DataSource = GetAllItems() CheckBoxList1.DataBind() Dim storedValues As String() = Database.GetStoredItems().Split(",") For Each val As String In storedValues For Each itm As ListItem In CheckBoxList1.Items If itm.Value = val Then itm.Selected = True End If Next Next End Sub Public Function GetAllItems() As DataSet Dim ds As New DataSet() Dim sqlstr As String = " select list from AList" Dim comm As New SqlDataAdapter(sqlstr, dbconn) comm.Fill(ds) 'Return those data as DataSet Return ds ' 1,2,3,4,5 End Function Public Function GetStoredItems() As String 'Get the data from Database ' Dim dbconn As New OleDb.OleDbConnection("provider=sqloledb;user id=sa;password=;trusted_connection=yes; initial catalog=wfphumanet;data source=localhost") Dim ds As New DataSet() Dim str As String = " select c1 from test where sl='" & TextBox1.text & "'" 'Return those data as comman seperated value Return str ' 1,2,3 End Function Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click bind1() End Sub
Chaitra N
n_gchaitra wrote:
CheckBoxList1.DataSource = GetAllItems() CheckBoxList1.DataBind()
Change like that ~
CheckBoxList1.DataSource = GetAllItems()**.Tables(0)** **CheckBoxList1.DataValueField = "list" CheckBoxList1.DataTextField = "list"** CheckBoxList1.DataBind()
Full Code SampleProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load CheckBoxList1.DataSource = GetAllItems().Tables(0) CheckBoxList1.DataValueField = "Num" CheckBoxList1.DataTextField = "Num" CheckBoxList1.DataBind() Dim storedValues As String() = GetStoredItems().Split(",") For Each val As String In storedValues For Each itm As ListItem In CheckBoxList1.Items If itm.Value = val Then itm.Selected = True End If Next Next End Sub Public Function GetAllItems() As DataSet Dim ds As New DataSet() Dim dt As New DataTable() Dim dc As New DataColumn("Num") dt.Columns.Add(dc) For i As Integer = 1 To 5 Dim dr As DataRow = dt.NewRow() dr(0) = i dt.Rows.Add(dr) Next ds.Tables.Add(dt) Return ds ' 1,2,3,4,5 End Function Public Function GetStoredItems() As String Return "1,2,3" End Function
Hope it helps..Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)
-
n_gchaitra wrote:
CheckBoxList1.DataSource = GetAllItems() CheckBoxList1.DataBind()
Change like that ~
CheckBoxList1.DataSource = GetAllItems()**.Tables(0)** **CheckBoxList1.DataValueField = "list" CheckBoxList1.DataTextField = "list"** CheckBoxList1.DataBind()
Full Code SampleProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load CheckBoxList1.DataSource = GetAllItems().Tables(0) CheckBoxList1.DataValueField = "Num" CheckBoxList1.DataTextField = "Num" CheckBoxList1.DataBind() Dim storedValues As String() = GetStoredItems().Split(",") For Each val As String In storedValues For Each itm As ListItem In CheckBoxList1.Items If itm.Value = val Then itm.Selected = True End If Next Next End Sub Public Function GetAllItems() As DataSet Dim ds As New DataSet() Dim dt As New DataTable() Dim dc As New DataColumn("Num") dt.Columns.Add(dc) For i As Integer = 1 To 5 Dim dr As DataRow = dt.NewRow() dr(0) = i dt.Rows.Add(dr) Next ds.Tables.Add(dt) Return ds ' 1,2,3,4,5 End Function Public Function GetStoredItems() As String Return "1,2,3" End Function
Hope it helps..Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)
But Michael it returns only the values present in a table where i am storing all the values. See i put my requirement in another way. For the subscription of a website i enter some area of interest in checkboxes. Suppose if i want to modify them(addition or removal), i need to see all the options(area of interest) as well a which i had selected. All the values options will be present in one table and when i request the page it displays all the options and the options i had chosed previously have to be checked. Let say i am storing all the options in a table "Options" in the column "Interest" and when i select them i will store the values in the table "Web" which has the columns "Email" And "Area_Of_Int".
Chaitra N