Click here to Skip to main content
15,886,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
webform.aspx.cs:
C#
SqlConnection conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);

conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add("@empcode", SqlDbType.VarChar).Value = Convert.ToString(TextBox1.Text);
cmd.Parameters.Add("@empname", SqlDbType.VarChar).Value = TextBox2.Text;

cmd = new SqlCommand("submitrecord", conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.ExecuteNonQuery(); //error shown on this line

conn.Close();

Stored procedure-submitrecord:
SQL
CREATE PROCEDURE submitrecord

@empcode varchar(50),
@empname varchar(50),
@sname varchar(50),
@location varchar(50),
@deptname varchar(50)
AS
insert into acyutaTB(emp_code,emp_name,sname,location,dept_name) values(@empcode,@empname,@sname,@location,@deptname)
GO
Posted
Updated 6-Jan-15 3:56am
v2
Comments
ZurdoDev 6-Jan-15 10:00am    
First off, simplify your code. Try:

cmd.Parameters.AddWithValue("@empcode", TextBox1.Text);

1 solution

Um...
C#
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add("@empcode", SqlDbType.VarChar).Value = Convert.ToString(TextBox1.Text);
cmd.Parameters.Add("@empname", SqlDbType.VarChar).Value = TextBox2.Text;

cmd = new SqlCommand("submitrecord",conn);
cmd.CommandType = CommandType.StoredProcedure;
So...You create a Command, add the parameters to it, and then throw it away to create a new one? Why?
Try this:
C#
SqlCommand cmd = new SqlCommand("submitrecord",conn);
cmd.Parameters.Add("@empcode", SqlDbType.VarChar).Value = Convert.ToString(TextBox1.Text);
cmd.Parameters.Add("@empname", SqlDbType.VarChar).Value = TextBox2.Text;
cmd.CommandType = CommandType.StoredProcedure;
Or better this:
C#
SqlCommand cmd = new SqlCommand("submitrecord",conn);
cmd.Parameters.AddWithValue("@empcode", TextBox1.Text);
cmd.Parameters.AddWithValue("@empname", TextBox2.Text);
cmd.CommandType = CommandType.StoredProcedure;
 
Share this answer
 
Comments
Member 11356129 6-Jan-15 10:21am    
Thanks a lot..it worked :)
OriginalGriff 6-Jan-15 10:24am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900