Discussion Locked

C# checkbox INSERT record into SQL

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Would like a simple example of a webform in C# containing checkbox inserting Y/N into a SQL table.

3 replies

by careed

To make this short, let's assume you have the webform designed and laid out. Given this, we have a CheckBox control with an ID of chkYN and a SQL Table (tblTest) that has two columns: TableID and YNResponse. Let's consider two scenarios for YNResponse: it is either a bit field or char(1) field. Given a stored procedure into SQL Server, then you could do the following as a parameter into your stored procedure, given ptrTest as the SQLParameter object and cmdTest as the SQLCommand object:

As a bit field:

ptrTest = new SqlParameter();
ptrTest.ParameterName = "@YNResponse";
ptrTest.SqlDbType = SqlDbType.Bit;
ptrTest.Value = chkYN.Checked;
cmdTest.Parameters.Add(ptrTest);
cmdTest.ExecuteNonQuery();

As a char(1) field:

ptrTest = new SqlParameter();
ptrTest.ParameterName = "@YNResponse";
ptrTest.SqlDbType = SqlDbType.Char;
ptrTest.Size = 1;
ptrTest.Value = chkYN.Checked ? "Y" : "N";
cmdTest.Parameters.Add(ptrTest);
cmdTest.ExecuteNonQuery();

Hope this helps!

This discussion is closed to new replies.