Reputation: 147
I am trying to update columns in my table by filling out text boxes and clicking save; I don't get an error or anything. Just nothing happens!
Here is my stored procedure:
ALTER PROCEDURE [dbo].[sp_UpdateProj]
@ORAID INT = NULL,
@FullTitle NVARCHAR(250) = NULL
AS
BEGIN
UPDATE tbl_ProjectFile
SET FullTitle = @FullTitle
WHERE ORAID = @ORAID
END
and it works when I run it in SQL Server Management Studio, given an ID and Title name
Here is my C# code
protected void Button_Save_Click(object sender, EventArgs e)
{
string connectionStr = ConfigurationManager.ConnectionStrings["ORAProjectConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionStr))
{
con.Open();
string query = "sp_UpdateProj Where ORAID=" + int.Parse(TextBox_ORAID.Text);
SqlCommand cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.AddWithValue("@ORAID", Convert.ToInt32(TextBox_ORAID.Text));
cmd.Parameters.AddWithValue("@FullTitle", TextBox_FullTitle.Text);
con.Close();
}
}
Upvotes: 1
Views: 3599
Reputation: 729
There are some errors in the Button_Save_Click
event handler:
When you use commandType
is StoredProcedure
you have to pass just the stored procedure name
With having a stored procedure with sp_
prefix create performance issue (Using sp_ as prefix for user stored procedures in SQL server causing performance impact)
You forgot to call the ExecuteNonQuery
method
Try this code:
protected void Button_Save_Click(object sender, EventArgs e)
{
string connectionStr = ConfigurationManager.ConnectionStrings["ORAProjectConnectionString"].ConnectionString;
string procedureName = "dbo.UpdateProj";
using (SqlConnection con = new SqlConnection(connectionStr))
using(SqlCommand cmd = new SqlCommand(procedureName , con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ORAID", Convert.ToInt32(TextBox_ORAID.Text));
cmd.Parameters.AddWithValue("@FullTitle", TextBox_FullTitle.Text);
con.Open();
cmd.ExecuteNonQuery()
con.Close();
}
}
Upvotes: 1
Reputation: 177
Here is the brief info bout executing stored procedure using C#
Call a stored procedure with parameter in c#
Upvotes: 0
Reputation: 754488
You're setting everything up (almost) correctly - but you're never actually executing the stored procedure!
Try this code:
protected void Button_Save_Click(object sender, EventArgs e)
{
string connectionStr = ConfigurationManager.ConnectionStrings["ORAProjectConnectionString"].ConnectionString;
// the query string should be **ONLY** the stored procedure name - nothing else!
string query = "dbo.sp_UpdateProj";
// you should put **both** SqlConnection and SqlCommand in "using" blocks
using (SqlConnection con = new SqlConnection(connectionStr))
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.StoredProcedure;
// fill the parameters - avoiding "AddWithValue"
cmd.Parameters.Add("@ORAID", SqlDbType.Int).Value = Convert.ToInt32(TextBox_ORAID.Text);
cmd.Parameters.Add("@FullTitle", SqlDbType.NVarChar, 250).Value = TextBox_FullTitle.Text;
con.Open();
// you need to **EXECUTE** the command !
cmd.ExecuteNonQuery();
con.Close();
}
}
Upvotes: 3
Reputation: 5810
Your query line should just be:
string query = "sp_UpdateProj";
You already have the parameters as objects below that.
Then add
cmd.ExecuteNonQuery();
to execute
Upvotes: 0