In this application we have to access data from the SQL database in the form of Gridview, and update Records while selecting Records from the gridview in another Form with update Button.
First step open new project and named as in this application like DataGridview_Application.
Create a New Form with one Gridview Control as shown below with the help of arrow and one Close Button as you can see below in the snapshot.
private void Update_Load(object sender, EventArgs e)
{
//Creating Connection with server name,uid,password and database.
SqlConnection con = new SqlConnection("server=abc-pc1\\sqlexpress; uid=sa; password=sa; database=Mind");
//connection is open
con.Open();
//Creating object for dataAdapter
SqlDataAdapter sda = new SqlDataAdapter("select * from Employee", con);
//craeting datatable object
DataTable dt = new DataTable();
sda.Fill(dt);
//accessing data in grid
dataGridView1.DataSource = dt;
} }
Output of the this code look likes as:
//then double click on the close button place a code for closing the application
this.Close();
Next step is to access rows in the gridview in another form as we have created another form named “New Form” as shown below,for adding another form in the same application you have to Rightclick on the datagridview_Application option on the right side of the application and right click over there then click on add items as shown below and named NewForm.
Next we have to show fields on the event of “cellclick “ option in the gridview ,for that under properties option you will find an option Event under that you will see an option “cellClick” then double click on that option and place a code .
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//For accessing data as rows with respected cells
int id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
//Creating object for NewForm as nf
NewForm nf = new NewForm(id);
//Display it into another form
nf.ShowDialog();
}
Then Design a NewForm as shown Below in the snapshot.
//how to show data in the respected fields while click on the datagrid cell.place a code under newForm Load Event as below.
//calling Global items
SqlConnection con;
SqlDataReader rdr;
SqlDataReader rdr1;
int nID;
public NewForm(int ID)//calling constructors with one parametres as ID.
{
InitializeComponent();
con = new SqlConnection("server=abc-pc1\\sqlexpress; uid=sa; password=sa; database=Mind");
nID = ID;
}
private void NewForm_Load(object sender, EventArgs e)
{
con.Open();
//Accessing commands from the database
SqlCommand cmd = new SqlCommand("select * from Employee where Employee_id=" + nID, con);
//accepting it into datareader
rdr = cmd.ExecuteReader();
rdr.Read();
string a = rdr[0].ToString();
txtID.Text = a;
//taking itinto textfileld from the gridview
txtName.Text = rdr[1].ToString();
txtAddress.Text = rdr[2].ToString();
txtPhoneno.Text = rdr[3].ToString();
txtage.Text = rdr[4].ToString();
rdr.Close(); }
Next step is to update rows from the new Form as shown in the snapshot as soon
as we click on any of the fields in the GridView u will see an another form will open
as shown below in the snapshot. Then we click on the update button and update
the records in the database.
Code for update the Records as shown below.
private void btbUpdate_Click(object sender, EventArgs e)
{
try
{
//show a messaage box wether we want to update a record or not.
if (MessageBox.Show("Do you want to update record?", "update", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//creating a command with the update query
SqlCommand cmd = new SqlCommand("UPDATE employee SET Employee_id='" + txtID.Text + "',Employee_name='" + txtName.Text + "',Employee_address='" + txtAddress.Text + "',Employee_phoneno='" + txtPhoneno.Text + "',Employee_age='" + txtage.Text + "'where Employee_id='" + nID + "'", con);
rdr1 = cmd.ExecuteReader();
con.Close();
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Leave Comment
1 Comments
View All Comments