Link to home
Start Free TrialLog in
Avatar of BOEING39
BOEING39

asked on

SQL SERVER CHECKBOX VALUE

The following code inserts "1" or "0" into my DB.   I need help with regard to inserting a specific value ie.  if the checkbox is checked it would insert "Eqpt Swap" and if left unchecked
the value would be NULL or "No Swap".   My DB column for "MM" is currently setup as "NVarChar".  

cmd.CommandText = ("INSERT  into Delays VALUES (@Dates,@Ship,@Station,@GT,@Log,@Inbd,@Otbd,@Dep,@Ground,@Code,@Time,@Code1,@Time1,@Wake,@Captain,@RON,@Ck,@Hist,@Call,@MM,@RB,@Tech,@Lead,@Reason,@Comment,@ATA,@DelayReason)");

Open in new window



Open in new window

cmd.Parameters.AddWithValue("@MM", (MM.Checked ? 1 : 0));
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

INSERT INTO Delays
Values (...,, CASE WHEN @MM = 1 THEN 'Eqpt Swap' ELSE 'No Swap' END, ...)
Hi BOEING39 ,

This is an example:

insert into table
values (@ssn,@empid,@race,
CASE @description
WHEN 'deputy' THEN 'Y'
WHEN 'civilian' THEN 'N'
end
)
Avatar of BOEING39
BOEING39

ASKER

I am not sure of the placement of the code.   Please assist?

 
cmd.CommandText = ("INSERT  into Delays VALUES (@Dates,@Ship,@Station,@GT,@Log,@Inbd,@Otbd,@Dep,@Ground,@Code,@Time,@Code1,@Time1,@Wake,@Captain,@RON,@Ck,@Hist,@Call,...,, CASE WHEN @MM = 1 THEN 'Eqpt Swap' ELSE 'No Swap' END, ...,@RB,@Tech,@Lead,@Reason,@Comment,@ATA,@DelayReason)");

Open in new window

In your commandtext
I am getting the following error with the code inserted as follows:

Error      Incorrect syntax near ','.  


cmd.CommandText = ("INSERT  into Delays VALUES (@Dates,@Ship,@Station,@RON,@Ck,@Hist,@Call,(...,,CASE WHEN @MM = 1 THEN 'Eqpt Swap' ELSE 'No Swap' END, ...),@RB,@Tech,@Lead,@Reason,@Comment,@ATA,@DelayReason)");

Open in new window

I think I had an extra comma in my example...post exactly what you're trying pls
Here is the entire code as revised that is producing the error:

 cmd.CommandText = ("INSERT  into Delays VALUES (@Dates,@Ship,@Station,@GT,@Log,@Inbd,@Otbd,@Dep,@Ground,@Code,@Time,@Code1,@Time1,@Wake,@Captain,@RON,@Ck,@Hist,@Call,(...,CASE WHEN @MM = 1 THEN 'Eqpt Swap' ELSE 'No Swap' END, ...),@RB,@Tech,@Lead,@Reason,@Comment,@ATA,@DelayReason)");

        cmd.Parameters.AddWithValue("@Dates", DateTime.Now);
        cmd.Parameters.AddWithValue("@Ship", Ship.SelectedValue);
        cmd.Parameters.AddWithValue("@Station", Station.Text);
        cmd.Parameters.AddWithValue("@GT", GT.Text);
        cmd.Parameters.AddWithValue("@Log", Log.Text);
        cmd.Parameters.AddWithValue("@Inbd", Inbd.Text);
        cmd.Parameters.AddWithValue("@Otbd", Otbd.Text);
        cmd.Parameters.AddWithValue("@Dep", Dep.Text);
        cmd.Parameters.AddWithValue("@Ground", Ground.Text);
        cmd.Parameters.AddWithValue("@Code", Code.SelectedValue);
        cmd.Parameters.AddWithValue("@Time", Time.Text);
        cmd.Parameters.AddWithValue("@Code1", Code1.SelectedValue);
        cmd.Parameters.AddWithValue("@Time1", Time1.Text);
        cmd.Parameters.AddWithValue("@Wake", Wake.SelectedValue);
        cmd.Parameters.AddWithValue("@Captain", Captain.SelectedValue);
        cmd.Parameters.AddWithValue("@RON", RON.SelectedValue);
        cmd.Parameters.AddWithValue("@Ck", Ck.SelectedValue);
        cmd.Parameters.AddWithValue("@Hist", HistCk.SelectedValue);
        cmd.Parameters.AddWithValue("@Call", Call.Text);
        cmd.Parameters.AddWithValue("@MM", (MM.Checked ? 1 : 0));
        cmd.Parameters.AddWithValue("@RB", RB.Text);
        cmd.Parameters.AddWithValue("@Tech", Tech.SelectedValue);
        cmd.Parameters.AddWithValue("@Lead", Lead.SelectedValue);
        cmd.Parameters.AddWithValue("@Reason", Reason.Text);
        cmd.Parameters.AddWithValue("@Comment", Comment.Text);
        cmd.Parameters.AddWithValue("@ATA", ATA.SelectedValue);
        cmd.Parameters.AddWithValue("@DelayReason", DelayReason.SelectedValue);
        
             
        cmd.ExecuteNonQuery();
        con.Close();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks very much.  Works fine now.  I appreciate the quick responses and follow-up.