ASP .net Data Insert Different way

protected void insert_Click(object sender, EventArgs e)
{
SqlConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings
["RegistrationandExamination"].ConnectionString);
SqlCommand com = new SqlCommand();
SqlTransaction tran;
db.Open();
tran = db.BeginTransaction();
com.CommandText = "INSERT T_Term_Std_ThesisProject_Result_Details(StudentId,StudentName,TermId,
CourseId,CourseCode,[4thYear1stTerm],VivaVoce,InternalExaminer,ExternalExaminer,CourseType)" +
" VALUES(@StudentId,@StudentsName,@TermId,@CourseId,
@CourseCode,@Year4Term1,@VivaVoce,@InternalExaminer,@ExternalExaminer,
@CourseType)";
com.Connection = db;
com.Transaction = tran;
com.Parameters.Add("@StudentId", SqlDbType.Int);
com.Parameters.Add("@StudentsName", SqlDbType.VarChar);
com.Parameters.Add("@TermId", SqlDbType.VarChar);
com.Parameters.Add("@CourseId", SqlDbType.VarChar);
com.Parameters.Add("@CourseCode", SqlDbType.VarChar);
com.Parameters.Add("@Year4Term1", SqlDbType.Decimal);
com.Parameters.Add("@VivaVoce", SqlDbType.Decimal);
com.Parameters.Add("@InternalExaminer", SqlDbType.Decimal);
com.Parameters.Add("@ExternalExaminer", SqlDbType.Decimal);
com.Parameters.Add("@CourseType", SqlDbType.VarChar);
try
{
foreach (GridViewRow grRow in gvThesisProject.Rows)
{
HiddenField hdnStudentId = (HiddenField)grRow.FindControl("hdnStudentId");
string StudentId = hdnStudentId.Value.ToString();
HiddenField hdnStudentsName = (HiddenField)grRow.FindControl("hdnStudentsName");
string StudentName = hdnStudentsName.Value.ToString();
HiddenField hdnCourseId = (HiddenField)grRow.FindControl("hdnCourseId");
string CourseId = hdnCourseId.Value.ToString();
HiddenField hdnCourseCode = (HiddenField)grRow.FindControl("hdnCourseCode");
string CourseCode = hdnCourseCode.Value.ToString();
HiddenField hdnCourseType = (HiddenField)grRow.FindControl("hdnCourseType");
string CourseType = hdnCourseType.Value.ToString();
TextBox txt4thYear1stTerm = ((TextBox)grRow.FindControl("txt4thYear1stTerm"));
string Year4Term1 = txt4thYear1stTerm.Text;
TextBox txtVivaVoce = ((TextBox)grRow.FindControl("txtVivaVoce"));
string VivaVoce = txtVivaVoce.Text;
TextBox txtInternalExaminer = ((TextBox)grRow.FindControl("txtInternalExaminer"));
string InternalExaminer = txtInternalExaminer.Text;
TextBox txtExternalExaminer = ((TextBox)grRow.FindControl("txtExternalExaminer"));
string ExternalExaminer = txtExternalExaminer.Text;
com.Parameters["@StudentId"].Value= StudentId;
com.Parameters["@StudentsName"].Value = StudentName;
com.Parameters["@TermId"].Value = ddTermId.Text;
com.Parameters["@CourseId"].Value = ddCourseId.Text;
com.Parameters["@CourseCode"].Value = CourseCode;
com.Parameters["@Year4Term1"].Value = Year4Term1;
com.Parameters["@VivaVoce"].Value = VivaVoce;
com.Parameters["@InternalExaminer"].Value = InternalExaminer;
com.Parameters["@ExternalExaminer"].Value = ExternalExaminer;
com.Parameters["@CourseType"].Value = CourseType;
com.ExecuteNonQuery();
}
tran.Commit();
lblErrorMessage.Text = "Result Insertion Successful.";
}
catch (SqlException sqlex)
{
tran.Rollback();
lblErrorMessage.Text = "Result Insertion Failed. Error: " + sqlex.Message;
}
finally
{
db.Close();
}
insert.Enabled = false;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s