public void insert(Crime c) { try { SqlCommand cmd = new SqlCommand(INSERT, DBC.getConnection()); SqlConnection con = cmd.Connection; con.Open(); cmd.Parameters.AddWithValue("@crimeType", c.crimeType); cmd.Parameters.AddWithValue("@crimeDate", c.crimeDate); cmd.Parameters.AddWithValue("@location", c.location); cmd.Parameters.AddWithValue("@noOfPeopleArrest", c.noOfPeopleArrest); using (con) { int rows = cmd.ExecuteNonQuery(); if (rows > 0) { MessageBox.Show("Data inserted successfully"); } else { MessageBox.Show("something wrong"); } } } catch (Exception e1) { //MessageBox.Show(e1 + ""); } }
public void update(Crime c) { SqlCommand cmd = new SqlCommand(UPDATE, DBC.getConnection()); SqlConnection con = cmd.Connection; con.Open(); //Left side table attributes and right side class variables cmd.Parameters.AddWithValue("@crimeID", c.crimeID); cmd.Parameters.AddWithValue("@crimeDate", c.crimeDate); cmd.Parameters.AddWithValue("@crimeType", c.crimeType); cmd.Parameters.AddWithValue("@location", c.location); cmd.Parameters.AddWithValue("@noOfPeopleArrest", c.noOfPeopleArrest); using (con) { int rows = cmd.ExecuteNonQuery(); if (rows > 0) { MessageBox.Show("Data updated successfully"); } else { MessageBox.Show("something wrong"); } } }
public List <Crime> fetch(SqlCommand cmd) { SqlConnection con = cmd.Connection; con.Open(); using (con) { SqlDataReader dr = cmd.ExecuteReader(); List <Crime> crime = null; if (dr.HasRows) { crime = new List <Crime>(); while (dr.Read()) { Crime c = new Crime(); c.crimeID = Convert.ToInt32(dr["crimeID"]); c.crimeType = Convert.ToString(dr["crimeType"]); c.crimeDate = Convert.ToString(dr["crimeDate"]); c.location = Convert.ToString(dr["location"]); c.noOfPeopleArrest = Convert.ToInt32(dr["noOfPeopleArrest"]); //left side class variables and right side table attributes crime.Add(c); } crime.TrimExcess(); } return(crime); } }
private void deleteBtn_Click(object sender, EventArgs e) { c = new Crime(); c.crimeID = Convert.ToInt32(CrimeGridView.CurrentRow.Cells[0].Value.ToString()); CrimeDataBase crimeDB = new CrimeDataBase(); crimeDB.delete(c); populate(); }
private void saveBtn_Click(object sender, EventArgs e) { CrimeDataBase crimeDB = new CrimeDataBase(); c = new Crime(); c.crimeType = crimeTxt.Text; c.crimeDate = crimeDateTxt.Text; c.location = locationTxt.Text; c.noOfPeopleArrest = Convert.ToInt32(arrestTxt.Text); crimeDB.insert(c); populate(); }
private void updateBtn_Click(object sender, EventArgs e) { CrimeDataBase crimeDB = new CrimeDataBase(); c = new Crime(); c.crimeID = Convert.ToInt32(CrimeGridView.CurrentRow.Cells[0].Value.ToString()); c.crimeType = crimeTxt.Text; c.crimeDate = crimeDateTxt.Text; c.location = locationTxt.Text; c.noOfPeopleArrest = Convert.ToInt32(arrestTxt.Text); crimeDB.update(c); populate(); }
public void delete(Crime c) { SqlCommand cmd = new SqlCommand(DELETE, DBC.getConnection()); SqlConnection con = cmd.Connection; con.Open(); //Left side table attribute and right side class variabled cmd.Parameters.AddWithValue("@crimeID", c.crimeID); using (con) { int rows = cmd.ExecuteNonQuery(); if (rows > 0) { MessageBox.Show("Data deleted successfully"); } else { MessageBox.Show("something wrong"); } } }