Showing posts with label checkboxlist. Show all posts
Showing posts with label checkboxlist. Show all posts

Tuesday, February 14, 2012

checkboxlist sql database

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 windrockform

Inherits System.Web.UI.Page

ProtectedSub submitButton_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles submitButton.Click

Dim dashdatasourceAsNew SqlDataSource

dashdatasource.ConnectionString = ConfigurationManager.ConnectionStrings("WindrockIssuesConnectionString").ToString

dashdatasource.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 Exception

Server.Transfer("problem.aspx")

EndTry

If rowsAffected <> 1Then

Server.Transfer("problem.aspx")

Else

Server.Transfer("confirm.aspx")

EndIf

EndSub

EndClass

I am new to asp.net and would appreciate the help.

Thanks,

I believe the key you are looking for is

CheckBox11.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.Count

6:

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.

CheckBoxList in Database

Hello all. When using a checkboxlist, is each selection under the checkboxlist suppose to have it's own column in the database?

Thank you,
MikeHi Michael,
You could have it in one column and use a string with a delimiter between each of the checked values.
P|||Cool, thank you. Am I suppose to use a filter to check for specific values?

Mike|||Mike
You would use the split() function to get the values.

chkBoxArr = Split(yourString, ",")

For iCounter = 0 to Ubound(chkBoxArr )
Response.Write chkBoxArr (iCounter) & "<br>"
Next

The split returns an array, so you need to use something like the above to get the values.

Hope this helps|||Check out this link, it has a good split() function for SQL 2000
http://weblogs.asp.net/pleloup/archive/2003/04/14/5569.aspx
Pete|||Thank you. I'll check out the link.

Mike :)

checkboxlist and SQL search using AND/OR on selected checkboxlist items.

I have a checkbox list like the one above. For example, Training OR Production – should include everyone with an Training OR everyone with a Production checked OR everyone with both Training and Production checked. If service AND technical support – just those two options will show – the customer can only have those 2 options selected in their account and nothing else. Is there an easy way to build the SQL query for this scenario? Any suggestions or tips?Thank you for any help

You can create a procedure that accepts a parameter ('AND' or 'OR), which perform a query based on the parameter:

create proc sp_testQuery @.logicOP varchar(3)='AND',@.id int,@.name varchar(20)
as
if @.logicOP='AND'
select * from t1 whereid=@.id and name=isnull(@.name,name)
else if @.logicOP='OR'
select * from t1 whereid=@.id orname=@.name
else raiserror('You must choose a logic operator ''AND'' or ''OR''',16,1)
go
sp_testQuery 'AND',1,null

Then what your code need to do is just call the stored procedure with providing all required parameters that come from your website.