SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 check sql temp variable with checkbox checked cont
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bplvid
Starting Member

45 Posts

Posted - 04/13/2012 :  12:45:01  Show Profile  Reply with Quote
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

India
47157 Posts

Posted - 04/13/2012 :  12:48:39  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 04/13/2012 :  12:50:02  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 04/13/2012 :  14:48:44  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 04/13/2012 :  16:12:22  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000