private bool SaveDataViewGridDatas() { bool isSave = false; try { List<Student> studentLst = new List<Student>(); foreach (DataGridViewRow row in this.dgvStudents.Rows) { if (!row.IsNewRow) { int batchId = Convert.ToInt32(row.Cells[0].Value); string batchName = row.Cells[1].Value.ToString(); int subjectId = Convert.ToInt32(row.Cells[2].Value); string subjectName = row.Cells[3].Value.ToString(); int studentId = Convert.ToInt32(row.Cells[4].Value); string studentName = row.Cells[5].Value.ToString(); string studentAddress = row.Cells[6].Value.ToString(); string studentMobile = row.Cells[7].Value.ToString(); string studentEmail = row.Cells[8].Value.ToString(); Student student = new Student() { StudentId = studentId, StudentName = studentName, StudentAddress = studentAddress, StudentMobile = studentMobile, StudentEmail = studentEmail, BatchId = batchId, SubjectId = subjectId }; studentLst.Add(student); } } if (InsertStudentsBySP(studentLst)) isSave = true; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } return isSave; }
private bool InsertStudent(Student model) { bool isInsert = false; SqlConnection sqlConnection = null; try { string dbConnectionString = ConfigurationManager.ConnectionStrings["appConStr"].ConnectionString; sqlConnection = new SqlConnection(dbConnectionString); string sql = "INSERT INTO Student (StudentId, StudentName, StudentAddress, StudentMobile, StudentEmail, BatchId, SubjectId) values (@StudentId, @StudentName, @StudentAddress, @StudentMobile, @StudentEmail, @BatchId, @SubjectId)"; SqlCommand command = new SqlCommand(sql, sqlConnection); SqlParameter parmStudentId = new SqlParameter("@StudentId", SqlDbType.Int); parmStudentId.Value = model.SubjectId; command.Parameters.Add(parmStudentId); SqlParameter parmStudentName = new SqlParameter("@StudentName", SqlDbType.NVarChar, 50); parmStudentName.Value = model.StudentName; command.Parameters.Add(parmStudentName); SqlParameter parmStudentAddress = new SqlParameter("@StudentAddress", SqlDbType.NVarChar, 50); parmStudentAddress.Value = model.StudentAddress; command.Parameters.Add(parmStudentAddress); SqlParameter parmStudentMobile = new SqlParameter("@StudentMobile", SqlDbType.NVarChar, 50); parmStudentMobile.Value = model.StudentMobile; command.Parameters.Add(parmStudentMobile); SqlParameter parmStudentEmail = new SqlParameter("@StudentEmail", SqlDbType.NVarChar, 50); parmStudentEmail.Value = model.StudentEmail; command.Parameters.Add(parmStudentEmail); SqlParameter parmBatchId = new SqlParameter("@BatchId", SqlDbType.Int); parmBatchId.Value = model.BatchId; command.Parameters.Add(parmBatchId); SqlParameter parmSubjectId = new SqlParameter("@SubjectId", SqlDbType.Int); parmSubjectId.Value = model.SubjectId; command.Parameters.Add(parmSubjectId); sqlConnection.Open(); command.ExecuteNonQuery(); isInsert = true; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } finally { if (sqlConnection.State == ConnectionState.Open) sqlConnection.Close(); } return isInsert; }