Many posts about Parameters in SQL with C# but I am still missing something. I am not getting an error message but no data is inserted. What is missing? I have text boxes na
This will be much shorter:
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "INSERT INTO Contacts ([First], [Last], [Address], [City], [State], [ZIP]) VALUES (@first, @last, @address, @city, @state, @zip)";
command.Parameters.AddWithValue("@first", first);
// or
// command.Parameters.Add("@first", SqlDbType.Type).Value = first;
// ...
connection.Open();
command.ExecuteNonQuery();
}
But first of all here's what you missed:
comm.Parameters.Add(firstparam);
// instead of
// comm.Parameters.Add("@first", SqlDbType.Text);
and
command.ExecuteNonQuery();