I'm trying to call a stored procedure from a C# windows Form application. It gets executed but nothing happens up in the database ie no records are added to the table. Here's my C# code:
private void buttonAddData_Click(object sender, EventArgs e)
{
string actionTaken = "Added";
string changeDescription = "Added new Code";
string updatedBy = "ICherry";
string currentStatus = "In development";
SqlConnection conDatabase = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ConfigData.mdf;Integrated Security=True;");
try
{
SqlCommand cmd = conDatabase.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spLogConfigChanges";
cmd.Parameters.AddWithValue("@ActionTaken",actionTaken);
cmd.Parameters.AddWithValue("@ChangeDescription", changeDescription);
cmd.Parameters.AddWithValue("@UpdatedBy", updatedBy);
cmd.Parameters.AddWithValue("@CurrentStatus", currentStatus);
conDatabase.Open();
MessageBox.Show("Connection opened");
cmd.ExecuteNonQuery();
MessageBox.Show("Command Executed");
conDatabase.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
And my SP when executed in Query analyser runs with no errors and I can see the records being created.
instead of using
SqlCommand cmd = conDatabase.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spLogConfigChanges";
cmd.Parameters.AddWithValue("@ActionTaken",actionTaken);
cmd.Parameters.AddWithValue("@ChangeDescription", changeDescription);
cmd.Parameters.AddWithValue("@UpdatedBy", updatedBy);
cmd.Parameters.AddWithValue("@CurrentStatus", currentStatus);
conDatabase.Open();
MessageBox.Show("Connection opened");
cmd.ExecuteNonQuery();
MessageBox.Show("Command Executed");
conDatabase.Close();
try this:
conDatabase.Open();
SqlCommand cmd = new SqlCommand("spLogConfigChanges",conDatabase);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ActionTaken",SqlDbType.Varchar).Value = actionTaken;
cmd.Parameters.Add("@ChangeDescription",SqlDbType.VarChar).Value = changeDescription;
cmd.Parameters.Add("@UpdateBy",SqlDbType.VarChar).Value = updatedBy;
cmd.Parameters.Add("@CurrentStatus",SqlDbType.VarChar).Value=currentStatus);
cmd.ExecuteNonQuery();
MessageBox.Show("Command Executed");
conDatabase.Close();
if you encounter some errors please comment
I had the same issue when I was using stored procedure
I solved it by
changing this
cmd.Parameters.AddWithValue("@param", "String Value")
to this
cmd.Parameters.Add("@param", OleDbType.VarChar).Value = "String Value";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.