I have tried the code below but my SQL table is empty and there was no error message. Below is my code:
private void button1_Click(object sender, EventArgs e)
{
string mainconn = ConfigurationManager.ConnectionStrings["Delivery_Inspection.Properties.Settings.TransitiondayConnectionString"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
String query = "INSERT INTO Table1 (Busnumber, Remarks, Dt1check1) VALUES (@bn, @rm, @dt1)";
using (SqlCommand command = new SqlCommand(query, sqlconn))
{
command.Parameters.AddWithValue("@bn", label1.Text.ToString());
command.Parameters.AddWithValue("@rm", richTextBox1.Text.ToString());
command.Parameters.AddWithValue("@dt1", this.dataGridView1[1, 0].Value);
sqlconn.Open();
int result = command.ExecuteNonQuery();
// Check Error
if (result < 0)
Console.WriteLine("Error inserting data into Database!");
}
}
Try to move
sqlconn.Open();before you createcommandand execute itand I suggest to put it in uses as well: