private void Insertdatabtn_Click(object sender, EventArgs e) { bool validated = false; if (CityName.Text == "" || CityName.Text == "City Name") { MessageBox.Show("Enter City Name"); } else { if (Regex.IsMatch(CityName.Text, "^[a-zA-Z ]*$")) { if (population.Text == "" || population.Text == "Enter Population") { MessageBox.Show("Enter Population city"); } else { if (Regex.IsMatch(population.Text, "^[0-9]*$")) { if (Prosecutionfir.Text == "" || Prosecutionfir.Text == "Prosecution Fir's") { MessageBox.Show("Enter prosecution Fir"); } else { if (Regex.IsMatch(Prosecutionfir.Text, "^[0-9]*$")) { if (Murderfr.Text == "" || Murderfr.Text == "Murder Fir's") { MessageBox.Show("Enter Murder fir's"); } else { if (Regex.IsMatch(Murderfr.Text, "^[0-9]*$")) { if (attemptmurderfir.Text == "" || attemptmurderfir.Text == "Attempmurder Fir's") { MessageBox.Show("Enter Attempmurder fir's"); } else { if (Regex.IsMatch(attemptmurderfir.Text, "^[0-9]*$")) { if (robaryfir.Text == "" || robaryfir.Text == "robary Fir's") { MessageBox.Show("Enter robary fir's"); } else { if (Regex.IsMatch(robaryfir.Text, "^[0-9]*$")) { if (rapefir.Text == "" || rapefir.Text == "Rape Fir's") { MessageBox.Show("Enter rape fir's"); } else { if (Regex.IsMatch(rapefir.Text, "^[0-9]*$")) { if (gangrapesfirs.Text == "" || gangrapesfirs.Text == "Gang Rape Fir's") { MessageBox.Show("Enter gangrape fir's"); } else { if (Regex.IsMatch(gangrapesfirs.Text, "^[0-9]*$")) { if (kidnappingabdutionfirs.Text == "" || kidnappingabdutionfirs.Text == "Kidnapping Abduction Fir's") { MessageBox.Show("Enter Kidnapping Abduction fir's"); } else { if (Regex.IsMatch(kidnappingabdutionfirs.Text, "^[0-9]*$")) { if (kidnappingrensomfirs.Text == "" || kidnappingrensomfirs.Text == "kidnappingrensom") { MessageBox.Show("Enter kidnapping rensom fir's"); } else { if (Regex.IsMatch(kidnappingrensomfirs.Text, "^[0-9]*$")) { if (LnLfirs.Text == "" || LnLfirs.Text == "Local and Law's Fir's") { MessageBox.Show("Enter Local and Law's Fir's"); } else { if (Regex.IsMatch(LnLfirs.Text, "^[0-9]*$")) { validated = true; } else { MessageBox.Show(" should be Numbers"); validated = false; LnLfirs.Focus(); } } } else { MessageBox.Show(" should be Numbers"); validated = false; kidnappingrensomfirs.Focus(); } } } else { MessageBox.Show(" should be Numbers"); validated = false; kidnappingabdutionfirs.Focus(); } } } else { MessageBox.Show(" should be Numbers"); validated = false; gangrapesfirs.Focus(); } } } else { MessageBox.Show(" should be Numbers"); validated = false; rapefir.Focus(); } } } else { MessageBox.Show(" should be Numbers"); validated = false; robaryfir.Focus(); } } } else { MessageBox.Show(" should be Numbers"); validated = false; attemptmurderfir.Focus(); } } } else { MessageBox.Show(" should be Numbers"); validated = false; Murderfr.Focus(); } } } else { MessageBox.Show(" should be Numbers"); validated = false; Prosecutionfir.Focus(); } } } else { MessageBox.Show("Population should be Numbers"); validated = false; population.Focus(); } } } else { MessageBox.Show("Enter City Name in Alphabets"); validated = false; CityName.Focus(); } } if (validated) { string connectionstring = @"Data Source=.;Initial Catalog=CrimeReport;Integrated Security=True"; using (SqlConnection con = new SqlConnection(connectionstring)) { con.Open(); string checkquery = "select cityname from crime where cityname like '%" + CityName.Text + "%';"; SqlCommand cmd = new SqlCommand(checkquery, con); SqlDataReader reader1 = cmd.ExecuteReader(); if (reader1.Read() == true) { MessageBox.Show("Record already exist"); string choice = MessageBox.Show("Do yOu want To Update record..?", "update record", MessageBoxButtons.YesNo).ToString(); if (choice == "Yes") { reader1.Close(); string updatequery = @"UPDATE crime SET people = '" + population.Text + "', prosecutionfir = '" + Prosecutionfir.Text + "', Murderfir = '" + Murderfr.Text + "', attemptedmurderfirs = '" + attemptmurderfir.Text + "', hurtfirs = '" + robaryfir.Text + "', kidnapingRansomfirs = '" + kidnappingrensomfirs.Text + "', kidnapingabductionfirs = '" + kidnappingabdutionfirs.Text + "', RapeFirs = '" + rapefir.Text + "', Gangrapefirs = '" + gangrapesfirs.Text + "', others = '" + LnLfirs.Text + "' WHERE cityname like '%" + CityName.Text + "%';"; SqlCommand updatecommand = new SqlCommand(updatequery, con); int updaterecord = updatecommand.ExecuteNonQuery(); if (updaterecord != 0) { MessageBox.Show("Your Record has been updated"); } } } else { reader1.Close(); string query = "INSERT INTO crime(cityname,people,prosecutionfir,Murderfir,attemptedmurderfirs,hurtfirs,kidnapingRansomfirs,kidnapingabductionfirs,RapeFirs,Gangrapefirs,others) VALUES('" + CityName.Text + "','" + population.Text + "','" + Prosecutionfir.Text + "','" + Murderfr.Text + "','" + attemptmurderfir.Text + "','" + robaryfir.Text + "','" + kidnappingrensomfirs.Text + "','" + kidnappingabdutionfirs.Text + "','" + rapefir.Text + "','" + gangrapesfirs.Text + "','" + LnLfirs.Text + "');"; CityName.Text = "City Name"; population.Text = "City Population"; Prosecutionfir.Text = "prosecution Fir's"; Murderfr.Text = "Murder Fir's"; attemptmurderfir.Text = "Attemted mUrder Fir's"; robaryfir.Text = "Robary Fir's"; rapefir.Text = "rape Fir's"; gangrapesfirs.Text = "gang rape Fir's"; kidnappingrensomfirs.Text = "kidnappinf rensom Fir's"; kidnappingabdutionfirs.Text = "kidnaping abduction Fir's"; LnLfirs.Text = "Laws and Local Fir's"; SqlCommand com = new SqlCommand(query, con); int record = com.ExecuteNonQuery(); if (record != 0) { MessageBox.Show(record + "Data Added"); } } con.Close(); } } }