Updating a bit field with a VB checkbox and a Stored Procedure
I have added a checkbox to a visual Basic windows application. I would like the checkbox when clicked to run the following stored procedure in order to change the bit value in sql server to 1:
Createproc [dbo].[Checked]
@idint
as
update Check
set
Checked=1where
ID=@IDI'd like the following stored procedure to run when the box is unchecked.
Createproc [dbo].[UnChecked]
@id
intas
update
Checkset
Checked=0where
ID=@IDIs this a standard process? And how can I accomplish this? I am using VS2005 and SQL Server 2005 Express Edition. Also I am in the dataset window. I am not sure which to add a datatable, tableadapter, or a query here. I've tried all 3 but when I go back to the design form I am unable to bind to the proc. Am I going about this the right way or should I be doing all of this in the code.
[2019 byte] By [
DBAJDS] at [2007-12-28]
I can explain how to do it without using datasets, using typed dataset and table adapters are quite easy actually, it creates everythin automatically for you, but it would take a litle time to explain..
First of all you don't need two stored procedures to do what you want to do, You want to update a bit field to 1 or 0 so, you can sen it as a parameter as well...
Here is the stored procedure:
Create proc [dbo].[SetCheckStatu]
@id int
@Checked bit
as
update Check
set
Checked = @Checkedwhere
ID=@IDSo.. You have the stored procedure.. Now lets run this procedure from your code
Open your windows forms code page and write the folloewing code into your checkBoxes CheckedChanged event
Dim conStr as String = "Server = YOURSERVER; Database= YUORDATABASE; UID=YourUserName; PWD=YourPassword"
Dim conn as new SQLCLient.SQLConnection (conStr)
Dim cmd as new SQLClient.SQLCommand
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "SetCheckStatu"
cmd.Parameters.Add("@Checked",Me.CheckBox.Checked) ''if the combobox is selected than it will return 1 else it will return 0
cmd.Parameters.Add("@Id", ID) ' your ID variable
conn.open
cmd.executeNoneQuery
conn.close
Sooo everytime you check or uncheck your checkbox, this commad will run, and send the statu as a parameter..
I hope it helps,
Success