0% found this document useful (0 votes)
8 views6 pages

Lecture 6 2nd Course C#

The document outlines a lecture on advanced C# programming, focusing on object manipulation and database updates. It includes examples of updating student information in a database and displaying selected rows from a DataGridView to textboxes. The code snippets demonstrate how to connect to a database, execute update queries, and handle user input through Windows Forms.

Uploaded by

yousif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Lecture 6 2nd Course C#

The document outlines a lecture on advanced C# programming, focusing on object manipulation and database updates. It includes examples of updating student information in a database and displaying selected rows from a DataGridView to textboxes. The code snippets demonstrate how to connect to a database, execute update queries, and handle user input through Windows Forms.

Uploaded by

yousif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

‫س‬ ‫ح‬‫ل‬‫ا‬ ‫ل‬ ‫ع‬ ‫س‬‫ق‬ ‫م‬ ‫ل‬ ‫مع‬‫كل عل الح س تكن ل جي ال‬

‫ م وم ا وب‬/ ‫ية وم ا وب و و و ا و اب‬


3rd Class – 2nd Course

Subject: Programming Language II (Advance C#)

Lecturer: Dr. Yousif A. Hamad


Lecture No 6

1
C# Objects (controls)
1-Command Update
2-Call all data from table to objects
3-Display the selected row from dataGV to textbox

UPDATE SAVED INFO IN TABLE

Description
private void button2_Click(object sender, EventArgs e)
{ try {
//This is my connection string i have assigned the database file address path
string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";
//This is my update query in which i am taking input from the user through windows forms and
update the record.
string Query = "update student.studentinfo set idStudentInfo='" + this.IdTextBox.Text + "',Name='"
+ this.NameTextBox.Text + "',Father_Name='" + this.FnameTextBox.Text + "',Age='" +
this.AgeTextBox.Text + "',Semester='" + this.SemesterTextBox.Text + "' where idStudentInfo='" +
this.IdTextBox.Text + "';";
//This is MySqlConnection here i have created the object and pass my connection string.
MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
MySqlDataReader MyReader2;
MyConn2.Open();
MyReader2 = MyCommand2.ExecuteReader();
MessageBox.Show("Data Updated");
while (MyReader2.Read())
{ }
MyConn2.Close();//Connection closed here
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); }

2
Example 1 Command Update

private void Button5_Click(object sender, EventArgs e)


{
int IDD = int.Parse(textBox2.Text);
string fn = textBox3.Text;
string ln = textBox4.Text;
int Age1 = int.Parse(textBox5.Text);
string stage1 = comboBox1.SelectedItem.ToString();
int ph1 = int.Parse(textBox7.Text);
con.Open();
SqlCommand cmdup = new SqlCommand("update StudentInfo set
Id='"+IDD+ "', fname='"+fn+ "',lname='"+ln+ "', age='"+Age1+
"',phoneno='"+ph1+"' where Id='"+int.Parse(textBox1.Text)+"'
", con);
cmdup.ExecuteNonQuery();
MessageBox.Show("done");
con.Close();
button1.PerformClick();
}

3
Example 2 Command Update

private void UPDATE_Click(object sender, EventArgs e)


{
// delete data from table
con.Open(); SqlCommand cmd = new SqlCommand("update TB1 set ID=@ID,FN=@FN,LN=@LN
where ID='"+textBox1.Text+"'",con);
cmd.Parameters.AddWithValue("@ID",int.Parse(textBox1.Text));
cmd.Parameters.AddWithValue("@FN", textBox2.Text);
cmd.Parameters.AddWithValue("@LN", textBox3.Text);
cmd.Parameters.AddWithValue("@Age", int.Parse(textBox4.Text));
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("data has been Updated");
}

4
Example 3 Display selected row from datagridview to textbox

- private void DataGridView1_CellContentClick(object sender,


DataGridViewCellEventArgs e)
{
{
if (e.RowIndex != -1)
{
DataGridViewRow dgvr = dataGridView1.Rows[e.RowIndex];
textBox2.Text = dgvr.Cells[0].Value.ToString();
textBox3.Text = dgvr.Cells[1].Value.ToString();
textBox4.Text = dgvr.Cells[2].Value.ToString();
textBox5.Text = dgvr.Cells[3].Value.ToString();
comboBox1.Text = dgvr.Cells[4].Value.ToString();
textBox7.Text = dgvr.Cells[5].Value.ToString();
} } }

5
- Example 4 Display selected row from datagridview to textbox using ID

# How to search for object information using textbox-based ID and display the information over
their textbox as shown in windows.

private void textBox5_Search (object sender, EventArgs e)


{
if (textBox5.Text! = "")
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from TB1 where ID=@Id ", con);
cmd.Parameters.AddWithValue("@Id", textBox5.Text);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
textBox1.Text = sdr.GetValue(0).ToString();
textBox2.Text = sdr.GetValue(1).ToString();
textBox3.Text = sdr.GetValue(2).ToString();
textBox4.Text = sdr.GetValue(3).ToString();
}
con.Close();
}}

You might also like