use bit datatype of sql server for the checkboxes,
Checkbox has two states either true or false
if checked means true and unchecked meanse false
after converting your datatype from nvarchar to bit, you just need to assign the value from database to checkbox, it will automatically check and uncheck depending on saved state
checkbox1.Checked = 'YourDatabaseFiled'
Hope it will solve your issue
Thanks
Please mark as answer if this post answered or solved your problem.
if you have taken int column
if (reader.Read())
{
NameTextBox.Text = reader["StudentName"].ToString();
LastName.Text = reader["LastName"].ToString();
CheckBox1.Checked = Convert.ToInt16(reader["columnname"]) == 0 ? true : false;
}
here i assumed that, if column value is 0 then checkbox will be checked
you can change it easily
If you feel helped. Please mark this post as Answer
I Think it is the best way to store the checkbox state as bit in the database and when you required to fetch the values just assigned it to the checkbox directly like, may be you required type casting if you are working with reader but for the DataTable,
you do not required any casting just directly assign with the Column Heading as you are assigning from reader
using (SqlConnection conn = new SqlConnection(@"Data Source=(local);Initial Catalog=gomahad;Integrated Security=True"))
{
var cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"SELECT * FROM Inquery WHERE StudentName = '" + DraftNameTextBox.Text + "' and FatherHusbandName = '" + DraftLastNameTextBox.Text + "'";
cmd.Parameters.AddWithValue("@StudentName", DraftNameTextBox.Text);
cmd.Parameters.AddWithValue("@FatherHusbandName", DraftLastNameTextBox.Text);
cmd.Connection = conn;
conn.Open();
var reader = cmd.ExecuteReader();
if (reader.Read())
{
NameTextBox.Text = reader["StudentName"].ToString();
LastName.Text = reader["LastName"].ToString();
CheckBox1.Checked = Convert.ToBolean(reader["CheckboxStateColumn"].ToString());
Please mark as answer if this post answered or solved your problem.
MahadPK
Participant
778 Points
225 Posts
CheckBox To Database Sql05
Apr 13, 2012 09:12 PM|LINK
GoodDay Guys,
I've a little prob. i've foget that how to connect and display Checkboxes on GridView.
* I want to connect Checkbox to the database (So, what Datatype Should i use? I Set the nvarchar Please tell me if im wrong.)
Im Using This Line for connecting to the Database: cmdboxsql.Parameters.AddWithValue("@Age", Age.Checked.ToString());* But This Line is Giving me "True" "False"
And on GridView When im showing it is simply Showing Text like "True and False" :/
Please Teach me this point I didnt Worked on Checkboxes so,far Hurry please.
Rab Nawaz Kh...
Participant
955 Points
209 Posts
Re: CheckBox To Database Sql05
Apr 13, 2012 10:20 PM|LINK
use bit datatype of sql server for the checkboxes,
Checkbox has two states either true or false
if checked means true and unchecked meanse false
after converting your datatype from nvarchar to bit, you just need to assign the value from database to checkbox, it will automatically check and uncheck depending on saved state
checkbox1.Checked = 'YourDatabaseFiled'
Hope it will solve your issue
Thanks
MahadPK
Participant
778 Points
225 Posts
Re: CheckBox To Database Sql05
Apr 14, 2012 05:31 AM|LINK
I have Changed DataType to "Bit" still its giving 'True' and 'False' and if im changing to datatype "Int" its giving '1' or '0'
and i wanna display Gridview will all Checked Results and i also wanna display Result on that Form by which data was Submitted.
I will have to use SqlCommand , ExecuteReader();
then on CheckBox how would i Put the Value from DataBase?
using (SqlConnection conn = new SqlConnection(@"Data Source=(local);Initial Catalog=gomahad;Integrated Security=True")) { var cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = @"SELECT * FROM Inquery WHERE StudentName = '" + DraftNameTextBox.Text + "' and FatherHusbandName = '" + DraftLastNameTextBox.Text + "'"; cmd.Parameters.AddWithValue("@StudentName", DraftNameTextBox.Text); cmd.Parameters.AddWithValue("@FatherHusbandName", DraftLastNameTextBox.Text); cmd.Connection = conn; conn.Open(); var reader = cmd.ExecuteReader(); if (reader.Read()) { NameTextBox.Text = reader["StudentName"].ToString(); LastName.Text = reader["LastName"].ToString(); CheckBox ? ?? ????? I want Checkbox Value from database.ramiramilu
All-Star
95463 Points
14106 Posts
Re: CheckBox To Database Sql05
Apr 14, 2012 10:27 AM|LINK
put up a checkbox in itemtemplate and then bind it with the bit field from your datatable...http://www.consultsarath.com/contents/threads/TS000014-checkbox-field-in-gridview.aspx
Thanks,
JumpStart
tdmca
Contributor
2396 Points
661 Posts
Re: CheckBox To Database Sql05
Apr 14, 2012 10:33 AM|LINK
if you have taken int column if (reader.Read()) { NameTextBox.Text = reader["StudentName"].ToString(); LastName.Text = reader["LastName"].ToString(); CheckBox1.Checked = Convert.ToInt16(reader["columnname"]) == 0 ? true : false; } here i assumed that, if column value is 0 then checkbox will be checked you can change it easilybasheerkal
Star
10672 Points
2426 Posts
Re: CheckBox To Database Sql05
Apr 14, 2012 10:38 AM|LINK
Use a checkBox in ItemTemPlate and Bind Its Checked Property to That bit field
. . . . . Checked= '<%# Bind("YourYesNoField")%>'
(Talk less..Work more)
Rab Nawaz Kh...
Participant
955 Points
209 Posts
Re: CheckBox To Database Sql05
Apr 14, 2012 12:24 PM|LINK
I Think it is the best way to store the checkbox state as bit in the database and when you required to fetch the values just assigned it to the checkbox directly like, may be you required type casting if you are working with reader but for the DataTable, you do not required any casting just directly assign with the Column Heading as you are assigning from reader
using (SqlConnection conn = new SqlConnection(@"Data Source=(local);Initial Catalog=gomahad;Integrated Security=True")) { var cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = @"SELECT * FROM Inquery WHERE StudentName = '" + DraftNameTextBox.Text + "' and FatherHusbandName = '" + DraftLastNameTextBox.Text + "'"; cmd.Parameters.AddWithValue("@StudentName", DraftNameTextBox.Text); cmd.Parameters.AddWithValue("@FatherHusbandName", DraftLastNameTextBox.Text); cmd.Connection = conn; conn.Open(); var reader = cmd.ExecuteReader(); if (reader.Read()) { NameTextBox.Text = reader["StudentName"].ToString(); LastName.Text = reader["LastName"].ToString(); CheckBox1.Checked = Convert.ToBolean(reader["CheckboxStateColumn"].ToString());