Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 check sql temp variable with checkbox checked cont

Author  Topic 

bplvid
Starting Member

45 Posts

Posted - 2012-04-13 : 12:45:01
I've a Stored procedure which handles insert into multiple tables and relation table as well.On based on file extension i do a insert and at the end I insert the newly inserted userid and viewid(checkbox checked value) into my bridge table.

I need to insert form data and also if checkbox is checked those values(viewid) into my db which my SP handles at this time.I need help on if no checkbox checked but still inserts other datas ignoring insertion into bridge table.Rigntnow if i don't check any checkbox it throws error saying null values cannot be inserted.I don't want the erro msg to be shown but insertionn based on checkbox selected handling in same stored procedure.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-13 : 12:48:39
you need to handle this part from form. Set a form variable for checkbox having boolean value based on its selection and pass it onto store procedure. Add bit parameter in store procedure to capture it and use it for if condition check to place where you do insertion to bridge table

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-13 : 12:50:02
Sort of hard to say what to change in your code without seeing the DDL for the tables and some sample data. But assuming that you have a parameter in your stored proc that indicates whether the checkbox is checked or not, you would want something like this:

IF (@CheckBoxChecked = 1)
BEGIN
-- Your code for inserting into Bridge table here.
-- INSERT INTO BridgeTable(col1,col2) VALUES (@val1, @val2);
END
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-13 : 14:48:44
Instead of doing
if ((chckBox.Checked) && (chckBox != null))
you should do
if ((chckBox != null) && (chckBox.Checked))


That may be a minor problem - more importantly, are your stored procedures really called Insert and Insert2? Can you post the code for the stored procedures?

Also, I think you need a conn.Open() before you do command.ExecuteNonQuery(). And, you are not executing the first command in cmd.

Google for a simple ADO.Net sample and pattern your code like it. That may be the best and fastest way to fix it.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-13 : 16:12:22
The stored procedure you posted does not seem to match with the C# code you posted earlier.
---- STORED PROC--------
ALTER PROCEDURE [dbo].[InsertResource]
(
@Name nvarchar(256),
@Path nvarchar(256),
@Date datetime2(7),
@UserName nvarchar(256)

)

------ C# CODE --------
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("Insert", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = TextBox1.Text;
cmd.Parameters.Add("@Date", SqlDbType.DateTime2.ToString()).Value = DateTime.Now.ToString();



1. Stored proc name is InsertResource, but your C# code is using the name "Insert".
2. Stored proc requires four parameters, @Name, @Path, @Date, @Username. But the C# code is supplying only two parameters.

If you deleted some of the variables from the C# code, you need to alter the stored procedure to match that.
Go to Top of Page
   

- Advertisement -