Click here to Skip to main content
16,004,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a question, how can i set a database value of type tinyint(0 or 1) when the values in my combobox are "true" and "false"?


For example, if i choose the "true" on combobox it sets a database value of 1 and if i choose "false" it sets the value to 0 according to a button with the sql query.


Any help would be nice! ;)
Posted
Updated 27-Jan-16 0:06am
v2
Comments
Andy Lanng 27-Jan-16 5:54am    
You could just switch it as runtime: dbParameter.Value = cbox.SelectedValue==1
The above is pseudo-code so don't expect it to be copy-pastable
Scribling Doodle 27-Jan-16 5:58am    
I understood how it works but i couldnt figure a way to setup my code correctly, could you give me a hint or so? Thanks in advance!
Sinisa Hajnal 27-Jan-16 6:05am    
You could create datatable with value / display_value columns and put it as datasource...you could put "false" first into the combobox and use combobox index as value...you could write class item with label and value and put them into collection that will be combobox datasource

My personal preference is to use checkbox for striclty true / false things because its easier to use :)
Scribling Doodle 27-Jan-16 6:08am    
In my case i would like to do like the example i gave, i setup all the comboboxes on my form and then when i press a button it would insert the values and that especific value into the database as a value of 0 if "false" and 1 if "true", only that. :)

1 solution

C#
string comboValue = "true";
bool v = bool.Parse(comboValue); // use TryParse instead
int yourDbValue = v ? 1 : 0;


If you do this a lot you can make an extension method on string to convert true\false\"1"\"0" to 1 or 0.
 
Share this answer
 
Comments
Scribling Doodle 27-Jan-16 6:03am    
I couldnt understand one thing, this piece only works for the value true? or it works for both values?
F-ES Sitecore 27-Jan-16 6:12am    
comboValue will be whatever comes back from your combobox, I just hard-coded it to "true" as an example, it'll work for "false" as well.
Scribling Doodle 27-Jan-16 9:32am    
Thanks for the information! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900