my final code
protected void SaveBtn_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
sqlCon.Open();
string Query = ("INSERT INTO Courses (courseCode,courseUnit) VALUES (@courseCode,@courseUnit)");
SqlCommand sqlCmd = new SqlCommand(Query, sqlCon);
List<KeyValuePair<object, object>> lst = new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<object, object>>();
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox1.Text ?? (object)DBNull.Value, TextBox3.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox2.Text ?? (object)DBNull.Value, TextBox4.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox7.Text ?? (object)DBNull.Value, TextBox8.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox9.Text ?? (object)DBNull.Value, TextBox10.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox13.Text ?? (object)DBNull.Value, TextBox14.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox15.Text ?? (object)DBNull.Value, TextBox16.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox19.Text ?? (object)DBNull.Value, TextBox20.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox21.Text ?? (object)DBNull.Value, TextBox22.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox25.Text ?? (object)DBNull.Value, TextBox26.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox27.Text ?? (object)DBNull.Value, TextBox28.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox31.Text ?? (object)DBNull.Value, TextBox32.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox33.Text ?? (object)DBNull.Value, TextBox34.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox37.Text ?? (object)DBNull.Value, TextBox38.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox39.Text ?? (object)DBNull.Value, TextBox40.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox5.Text ?? (object)DBNull.Value, TextBox6.Text ?? (object)DBNull.Value));
lst.Add(new System.Collections.Generic.KeyValuePair<object, object>(TextBox11.Text ?? (object)DBNull.Value, TextBox12.Text ?? (object)DBNull.Value));
foreach (var item in lst)
{
if (item.Key != null && item.Value != null)
{
sqlCmd.Parameters.Clear();
sqlCmd.Parameters.AddWithValue("@courseCode", item.Key);
sqlCmd.Parameters.AddWithValue("@courseUnit", item.Value);
}
sqlCmd.ExecuteNonQuery();
}
lblSuccessMessage.Text = "Courses successfully added";
}
}
catch (Exception ex)
{
lblSuccessMessage.Text = "";
lblErrorMessage.Text = ex.Message;
}
}