General discussion

Locked

C# checkbox INSERT record into SQL

By vhansen86 ·
Would like a simple example of a webform in C# containing checkbox inserting Y/N into a SQL table.

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by careed In reply to C# checkbox INSERT record ...

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!

Collapse -

by vhansen86 In reply to

Poster rated this answer.

Collapse -

by vhansen86 In reply to C# checkbox INSERT record ...

This question was closed by the author

Back to Web Development Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums