The code that I have here to insert a record works fine, but I tried to put a check in here to see if the record was entered and send back to a label on the page if it was entered or not.
but when I run this with the lines that are commented out here, it stops at the line with "Object result = cmd2.ExecuteScalar()
Stating incorrect sysntext.
Any ideas I would appreciate
thank you
My code:
private void Button1_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Data Source=(local)\\SQLEXPRESS;Initial Catalog=NotthWind;Integrated Security=True");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("INSERT INTO Shippers(CompanyName, Phone) VALUES(@CompName,@Phone)", con);
cmd.Parameters.AddWithValue("@CompName", TextBox1.Text).SqlDbType = System.Data.SqlDbType.NVarChar;
cmd.Parameters.AddWithValue("@Phone", TextBox2.Text).SqlDbType = System.Data.SqlDbType.NVarChar;
using (con)
{
con.Open();
cmd.ExecuteNonQuery();
}
con.Close();
//System.Data.SqlClient.SqlConnection con2 = new System.Data.SqlClient.SqlConnection("Data Source=(local)\\SQLEXPRESS;Initial Catalog=NotthWind;Integrated Security=True");
//System.Data.SqlClient.SqlCommand cmd2 = new System.Data.SqlClient.SqlCommand("Select CompanyName From Shippers Where CompanyName = @CompName2)", con2);
//cmd2.Parameters.AddWithValue("@CompName2", TextBox1.Text).SqlDbType = System.Data.SqlDbType.NVarChar;
//lblResult.Visible = true;
//using (con2)
//{
// con2.Open();
// Object result = cmd2.ExecuteScalar();
// if (result != null)
// lblResult.Text = string.Format("{0:c}", result);
// else
// lblResult.Text = "was not inserted";
//}
}
but when I run this with the lines that are commented out here, it stops at the line with "Object result = cmd2.ExecuteScalar()
Stating incorrect sysntext.
Any ideas I would appreciate
thank you
My code:
private void Button1_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Data Source=(local)\\SQLEXPRESS;Initial Catalog=NotthWind;Integrated Security=True");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("INSERT INTO Shippers(CompanyName, Phone) VALUES(@CompName,@Phone)", con);
cmd.Parameters.AddWithValue("@CompName", TextBox1.Text).SqlDbType = System.Data.SqlDbType.NVarChar;
cmd.Parameters.AddWithValue("@Phone", TextBox2.Text).SqlDbType = System.Data.SqlDbType.NVarChar;
using (con)
{
con.Open();
cmd.ExecuteNonQuery();
}
con.Close();
//System.Data.SqlClient.SqlConnection con2 = new System.Data.SqlClient.SqlConnection("Data Source=(local)\\SQLEXPRESS;Initial Catalog=NotthWind;Integrated Security=True");
//System.Data.SqlClient.SqlCommand cmd2 = new System.Data.SqlClient.SqlCommand("Select CompanyName From Shippers Where CompanyName = @CompName2)", con2);
//cmd2.Parameters.AddWithValue("@CompName2", TextBox1.Text).SqlDbType = System.Data.SqlDbType.NVarChar;
//lblResult.Visible = true;
//using (con2)
//{
// con2.Open();
// Object result = cmd2.ExecuteScalar();
// if (result != null)
// lblResult.Text = string.Format("{0:c}", result);
// else
// lblResult.Text = "was not inserted";
//}
}