I have a form with text boxes and checkboxlists that a user will fill out and click submit. When the user clicks submit, it will update the sql database. In my sql database I have checkbox fields. My question is how can I use the selected items in a checkboxlist to update the sql database individual check boxes. Below is the code I have so far that works for a text box:
PartialClass windrockformInherits System.Web.UI.Page
ProtectedSub submitButton_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles submitButton.ClickDim dashdatasourceAsNew SqlDataSource
dashdatasource.ConnectionString = ConfigurationManager.ConnectionStrings("WindrockIssuesConnectionString").ToStringdashdatasource.InsertCommandType = SqlDataSourceCommandType.Text
dashdatasource.InsertCommand ="INSERT INTO IssueLog (initiator) VALUES (@.initiator)"
dashdatasource.InsertParameters.Add("initiator", initiatorTextBox.Text)Dim rowsAffectedAsInteger = 0
Try
rowsAffected = dashdatasource.Insert()
Catch exAs ExceptionServer.Transfer("problem.aspx")
EndTry
If rowsAffected <> 1Then
Server.Transfer("problem.aspx")Else
Server.Transfer("confirm.aspx")EndIf
EndSubEndClass
I am new to asp.net and would appreciate the help.
Thanks,
I believe the key you are looking for isCheckBox11.Checked =False
|||
What are checkbox datatypes in SQL server? Maybe you mean they are of BIT datatype?
I think you will probably have to loop through your checkbox list and check each box to see if its checked, and set a parameter accordingly.
|||Hi nkair19 ,
When the user clicks submit, it will update the sql database. In my sql database I have checkbox fields. My question is how can I use the selected items in a checkboxlist to update the sql database individual check boxes
I think maybe you mean you want to update certain data fields in your sql server database based on selected values in your checkboxlist. If I've understood you wrong, please feel free to tell me, thanks.
I think you can use an arraylist as a bridge between your checkboxlist and database filed. Like this:
ArrayList list=new ArrayList();
for (int i=0; i<checkboxlist1.Items.Count; i++)
{
if (checkboxlist1.Items[i].Selected)
{
list.add("the value you want to assign to the database field when checkbox item checked");
}
else
list.add("the value you want to assign to the database field when checkbox item not checked");
}
After this,in your sqlcommand text, you can refer to the corresponding arraylist value directly.
Hope my suggestion helps
|||Did I forget to mention that I am using vb, not C#?
|||I have this so far:
Dim listAsNew ArrayList()3:
4:Dim iAsInteger = 0
5:While i < pacheckboxlist.Items.Count6:
7:If pacheckboxlist.Items(i).SelectedThen
8:
9: list.Add("win6310_pa--is this where I add the check box name that I have in the SQL database?")10:Else
11: list.Add("the value you want to assign to the database field when checkbox item not checked--I don't want anything to happen if nothing is checked because I have other checkboxlist groups to choose from")12:
13:
14:EndIf
15: System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
16:EndWhile
After this,in your sqlcommand text, you can refer to the corresponding arraylist value directly. --confused here.
Thanks.
No comments:
Post a Comment