private void button1_Click(object sender, EventArgs e) { try { Student s = new Student(); s.set_FirstName(FirstName.Text); s.set_LastName(LastName.Text); s.set_Registration_No(RegistrationNumber.Text); s.set_Contact(Contact.Text); s.set_Email(Email.Text); s.set_Status(Status.Text); if ((FirstName.Text == "") || (LastName.Text == "") || (Contact.Text == "") || (RegistrationNumber.Text == "") || (Email.Text == "") || (Status.Text == "")) { MessageBox.Show("Submssion is not allowed with null values"); } else if ((s.get_FirstName() == null) || (s.get_LastName() == null) || (s.get_Contact() == null) || (s.get_Registration_No() == null) || (s.get_Email() == null) || (s.get_Status() == null)) { MessageBox.Show("Invalid Data Entered!!"); } else { string constr = "Data Source=DESKTOP-GP94IEM\\SQLEXPRESS;Initial Catalog=Projectb;Integrated Security=True"; SqlConnection c = new SqlConnection(constr); c.Open(); string s2 = string.Format("SELECT LookupId FROM Lookup WHERE Category=@Category and Name =@Name"); SqlCommand a = new SqlCommand(s2, c); a.Parameters.Add(new SqlParameter("@Category", "STUDENT_STATUS")); a.Parameters.Add(new SqlParameter("@Name", this.Status.Text)); int id = (int)a.ExecuteScalar(); string s1 = string.Format("INSERT INTO Student(FirstName,LastName,Contact,Email,RegistrationNumber,Status) values('" + s.get_FirstName() + "', '" + s.get_LastName() + "','" + s.get_Contact() + "','" + s.get_Email() + "','" + s.get_Registration_No() + "','" + id + "')"); //string s1 = string.Format("INSERT INTO Student(FirstName,LastName,Contact,Email,RegistrationNumber,Status) values('{0}', '{1}', '{2}', '{3}', '{4}', '{5}')", s.get_FirstName(), s.get_LastName(), s.get_Contact(), s.get_Email(), s.get_Registration_No(), id); SqlCommand a2 = new SqlCommand(s1, c); //int rows = DatabaseConnection.getInstance().exectuteQuery(s1); int rows = a2.ExecuteNonQuery(); if (rows != 0) { MessageBox.Show("Student Added"); this.Close(); StudentGrid n = new StudentGrid(); n.Show(); } c.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void button1_Click(object sender, EventArgs e) { try { Student s = new Student(); s.set_FirstName(textBox5.Text); s.set_LastName(textBox4.Text); s.set_Registration_No(textBox1.Text); s.set_Contact(textBox3.Text); s.set_Email(textBox2.Text); s.set_Status(comboBox1.Text); if ((s.get_FirstName() == null) || (s.get_LastName() == null) || (s.get_Contact() == null) || (s.get_Registration_No() == null) || (s.get_Email() == null) || (s.get_Status() == null)) { MessageBox.Show("Submssion is not allowed with null values"); } else { string constr = "Data Source=DESKTOP-GP94IEM\\SQLEXPRESS;Initial Catalog=Projectb;Integrated Security=True"; SqlConnection c = new SqlConnection(constr); c.Open(); string s2 = string.Format("SELECT LookupId FROM Lookup WHERE Category=@Category and Name =@Name"); SqlCommand a = new SqlCommand(s2, c); a.Parameters.Add(new SqlParameter("@Category", "STUDENT_STATUS")); a.Parameters.Add(new SqlParameter("@Name", this.comboBox1.Text)); int id = (int)a.ExecuteScalar(); int f = dataGridView1.CurrentCell.RowIndex; string id4 = (string)(dataGridView1.Rows[f].Cells["RegistrationNumber"].Value); //MessageBox.Show("Updating this entry"); string Reg = (string)(dataGridView1.Rows[f].Cells["RegistrationNumber"].Value); string Reg_No = string.Format("SELECT Id FROM STUDENT WHERE RegistrationNumber=@RegistrationNumber"); SqlCommand o = new SqlCommand(Reg_No, c); o.Parameters.Add(new SqlParameter("@RegistrationNumber", Reg)); int t = (int)o.ExecuteScalar(); string s1 = string.Format("UPDATE STUDENT SET FirstName=@FirstName, LastName=@LastName,Contact=@Contact,Email=@Email,RegistrationNumber=@RegistrationNumber,Status=@Status where Id=@Id"); //values('" + s.get_FirstName() + "', '" + s.get_LastName() + "','" + s.get_Contact() + "','" + s.get_Email() + "','" + s.get_Registration_No() + "','" + id + "')"); //string s1 = string.Format("INSERT INTO Student(FirstName,LastName,Contact,Email,RegistrationNumber,Status) values('{0}', '{1}', '{2}', '{3}', '{4}', '{5}')", s.get_FirstName(), s.get_LastName(), s.get_Contact(), s.get_Email(), s.get_Registration_No(), id); List <SqlParameter> p = new List <SqlParameter>(); SqlCommand a2 = new SqlCommand(s1, c); //GetExample(a2, p.ToArray()); a2.Parameters.Add(new SqlParameter("Id", t)); a2.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.VarChar)); a2.Parameters["@FirstName"].Value = s.get_FirstName(); a2.Parameters.Add(new SqlParameter("@LastName", SqlDbType.VarChar)); a2.Parameters["@LastName"].Value = s.get_LastName(); a2.Parameters.Add(new SqlParameter("@Contact", SqlDbType.VarChar)); a2.Parameters["@Contact"].Value = s.get_Contact(); a2.Parameters.Add(new SqlParameter("@RegistrationNumber", SqlDbType.VarChar)); a2.Parameters["@RegistrationNumber"].Value = s.get_Registration_No(); a2.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar)); a2.Parameters["@Status"].Value = id; a2.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar)); a2.Parameters["@Email"].Value = s.get_Email(); /** * a2.Parameters.AddWithValue("@FirstName" , s.get_FirstName()); * a2.Parameters.AddWithValue("@LastName", s.get_LastName()); * a2.Parameters.AddWithValue("@ContactName", s.get_Contact()); * a2.Parameters.AddWithValue("@RegistrationNumber", s.get_Registration_No()); * a2.Parameters.AddWithValue("@Status", id); * a2.Parameters.AddWithValue("@Email", s.get_Email());**/ //int rows = DatabaseConnection.getInstance().exectuteQuery(s1); int rows = a2.ExecuteNonQuery(); if (rows != 0) { MessageBox.Show("Student Updated"); panel1.Hide(); StudentGrid m = new StudentGrid(); m.Show(); c.Close(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }