954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Execution of a stored procedure

I have the following code that executes an sql stored procedure and I pass a customer name and customer contact to the procedure to use to create a new record in a table. When I execute the stored procedure in sql it inputs the customer and contact in the new record but when I use the following code it creates a new record but the customer and contact are blank. Can anyone see what I have done wrong. Here is my code. Thanks in advance.


private void copyestBN_Click(object sender, EventArgs e)
{
{
int estno;
char customer;
char contact;
char.TryParse(customercopyCB.Text, out customer);
char.TryParse(newcontactTB.Text, out contact);
int.TryParse(estimatenocopyCB.Text, out estno);

{
SqlConnection conn = new SqlConnection("Data Source=server1;Initial Catalog=estimator;Integrated Security=True");
SqlCommand cmd = new SqlCommand("copyestimate", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@estnum", estno);
cmd.Parameters.AddWithValue("@customer", customer);
cmd.Parameters.AddWithValue("@contact", contact);
cmd.Parameters.AddWithValue("@newestnum", SqlDbType.Int).Direction = ParameterDirection.Output;

try
{
conn.Open();
cmd.ExecuteNonQuery();
newestLB.Items.Add(Convert.ToString(cmd.Parameters["@newestnum"].Value.ToString()));

}
catch (SqlException err)
{
MessageBox.Show(err.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
}
}
}
}

MJV
Junior Poster in Training
67 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

dont cast those strings to char
especially if is not a char[] array... hint

just send the text of the components into the parameter assignment.

cmd.Parameters.AddWithValue("@customer", customercopyCB.Text);
JerryShaw
Posting Pro in Training
465 posts since Nov 2006
Reputation Points: 69
Solved Threads: 75
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You