Ok... I have a value in my Sql table called "Disconnected" is a bit type....0 for not being disconnected 1 for being disconnected, Now i have a checkbox in my windows form that will submit information on a product....if the Disconnected checkbox is checked how do i send that value to the sql db...I know the checkbox is a bool value so if its checked it returns true if not it returns 0 for false.......I know i can type cast a bool to a bit, so should i do that within a property or something anyhelp or any links on how to get this done will be most helpful!!!

Data binding and Checkbox values
sebahattingokce
That worked! Thanks...I am using the stored procedure but i wanted to get this way to work first...thanks
if(Convert.ToBoolean(IsDe) == true)
{
cbox.CheckState = CheckState.Checked;
}
else {
cbox.CheckState = CheckState.Unchecked;
}
LouArnold
donbox5
Could u explain how I would do an Update the "somevalue" that i give it...could i do something like
if(checkbox.checkstate == CheckState.Checked)
Convert.ToBoolean(IsDe) ==true
else if
CheckBox.Checkstate = CheckState.UnChecked
Convert.ToBoolean(IsDe) == false;
}
then based on the value thats change in this decision structure...I would then call my update querey passing IsDe as the parameter is this the proper way of doing that
pessi
discontinuedItemParam.Value = SomeValue;
Would this "somevalue" be the checkbox.CheckedState or do i use true or false
K.Kong
Ok so your saying that
If i have a form with some text fields and i pull up the information about a pacific product....and I check the discontinued button...All I have to do is check the box How do i know the value is sent I have a bit column in my database called "IsDiscontinued"...Dont i need like a bool value called "IsDiscontinued" and somehow bind it like i would do my textfield to the data items how does my app know that this checkbox is check(true) so it should send the "1" value to my db to indicate thats its true this particular product is discontinued Check out my code....
bool IsDe;
string pname = "";
string qname = "";
string fullitem;
public string prepareStr(string str)
{
str = str.Replace("'","''");
return str;
}
/** if(str.Trim() == "")
{
return null;
}
else
{
return "'" + str.Trim() + "'";
}
} */
private void button2_Click(object sender, System.EventArgs e)
{
try {
string connect = System.Configuration.ConfigurationSettings.AppSettings["connstring"];
SqlConnection sql = new SqlConnection(connect);
string str = "Select * From Products Where ProductName = '" + prepareStr(this.txt.Text) + "'";
SqlCommand cmd = new SqlCommand(str, sql);
sql.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
MessageBox.Show(str);
while(dr.Read())
{
IsDe = dr["Discontinued"];
pname = dr["ProductName"].ToString();
qname = dr["QuantityPerUnit"].ToString();
fullitem = pname + " " + qname;
this.textBox3.Text = pname;
}
if(IsDe == true)
{
cbox.CheckState = CheckState.Checked;
}
else {
cbox.Checked.Equals(false);
}
checkedListBox1.Items.Add(fullitem);
}
catch(Exception da)
{
MessageBox.Show(da.Message.ToString());
}
}
See and i get this erorr........
Cannot implicitly convert type 'object' to 'bool'
Stefan Toller
You should not have to do anything special to bind a bit column to a checkbox. Here is a simple example. I have a bindingnavigator, textbox, and checkbox on a form
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
Dim strConn As String
Dim da As SqlDataAdapter
Dim conn As SqlConnection
strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;"
conn = New SqlConnection(strConn)
da = New SqlDataAdapter("Select * from Products", conn)
da.Fill(dt)
dt.Columns("CategoryID").DefaultValue = 6
Dim bs As New BindingSource
bs.DataSource = dt
BindingNavigator1.BindingSource = bs
txtProductName.DataBindings.Add("Text", bs, "ProductName")
chkDiscontinued.DataBindings.Add("Checked", bs, "Discontinued")
End Sub
End Class
JFoushee
well basically yes, mine was just an example where you need to complete it and change the variable names appropriately and so on
Kuei-yang Lo
it should be:
if (Convert.ToBoolean(IsDe) == true)
{ ... }
ok, well since you are using a DataReader, what I suggested won't work. You will have to in this case, manually create your update/insert SQL Statement and execute it - adding values to update/insert as parameters.
Small Example:
SqlCommand theUpdateCommand = new SqlCommand("Update tablename SET discontinued = @discontinuedItem WHERE [ID] = @IDParam", new SqlConnection(connectionString));
SqlParameter theID = new SqlParameter("@IDParam", SqlDbType.Int);
theID.Value = recordID;
theSqlCommand.Parameters.Add(theID);
SqlParameter discontinuedItemParam = new SqlParameter("@discontinuedItem", SqlDbType.Bit);
discontinuedItemParam.Value = SomeValue;
theSqlCommand.Parameters.Add(discontinuedItemParam);
..
..
theSqlCommand.Connection.Open();
theSqlCommand.ExecuteNonQuery();
theSqlCommand.Connection.Close();
this is just an example, involving your situation/problem. We have a field in the database called "discontinued", which is a bit value (boolean, 1/0, true/false) and we set a parameter value to the value of the checkbox in the "discontinuedItemParam", then add it to the collection of parameters and execute that query and should update the database
of course technically the best practice/preferred method is to use a Stored Procedure to execute these functions but this is something to look into later perhaps
does this help
OC