private string conString = @"server=localhost;database=stamppaper;port=3306;username=root;password=;SslMode=none";
private void BindGrid()
{
using (MySqlConnection con = new MySqlConnection(conString))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM Stamp", con))
{
cmd.CommandType = CommandType.Text;
using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
grdStamp.DataSource = dt;
grdStamp.Columns[0].HeaderCell.Value = "Sr. No.";
id = 0;
}
}
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
string query = "DELETE FROM STAMP WHERE ID = '" + id + "'";
if (id == 0)
{
MessageBox.Show("Please select stamp from List...");
}
else
{
var confirmDelete = MessageBox.Show("Are you sure you want to delete?", "Delete", MessageBoxButtons.YesNo);
if (confirmDelete == DialogResult.Yes)
{
try
{
MySqlConnection con = new MySqlConnection(conString);
MySqlCommand cmd = new MySqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
this.BindGrid();
MessageBox.Show("Stamp deleted Successfully...");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
private void btnSave_Click(object sender, EventArgs e)
{
if (this.validateAllFields())
{
if (id == 0)
{
string query = "insert into Stamp ( Rupees, Date, Name, Address, AadharNumber, PhoneNumber, CreatedBy, " +
"ModifiedBy) values ('" + txtRupees.Text + "' ,'" + txtDate.Text + "' ,'" + txtName.Text + "'," +
" '" + txtAddress.Text + "', '" + txtAadharNumber.Text + "', '" + txtMobileNumber.Text + "', '1'," +
" '1');";
MySqlConnection conDatabase = new MySqlConnection(conString);
MySqlCommand cmd = new MySqlCommand(query, conDatabase);
MySqlDataReader myReader;
try
{
conDatabase.Open();
myReader = cmd.ExecuteReader();
while (myReader.Read())
{
}
this.BindGrid();
MessageBox.Show("Stamp Saved Successfully...");
this.clearAllFields();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
No comments:
Post a Comment