Editing the Database Record
Today, we will cover
Revision from previous Lecture
SQL Update statement
Updating the Database Record from ASP.Net Page
SQL Update Statement Syntax
The SQL Update statement syntax is:
Update Table_Name
Set Col1_Name = New_Value,
Col2_Name = New_Value
Where_Clause
Suppose we have the following table in the database:
Now, add a new web form to the web site, we have developed earlier and insert a
table with 6 Rows and 2 columns in each Row as shown here:
Select the Insert Table option, you will see the screen as given below
In Rows, enter 6 and press OK button. Now, your web form will be look like this
Finally, design the form as shown here
Control Type & Name Property to be changed New Value
TextBox1 ID txtClassName
TextBox2 ID txtClassDescription
TextBox3 ID txtClassSID
Now, First fill the data grid using our DAL method fillDS(). The code behind this
web forms’ Page_Load() event is shown here:
Then, on the right side of the Grid View, there is a small arrow, Click it, you will
see
Click the Edit Columns link, you will see the screen as shown
From here, select the Edit, Update, Cancel option as shown and press the Add
button
Now, in the Header Text property on the right side of this window, enter the
Update Record as shown
Press the OK button, you will see the gridview as shown
Now, select eh Grid View and press the F4 button to see the property of the Grid
View as shown here:
Press the Event link, you will see all the events of the Grid View as shown
Double click in the RowEditing, you will see the screen as shown
In between the blue highlighted line enter the code as shown
OR Code is given below
txtClassSID.Text = GridView1.Rows[e.NewEditIndex].Cells[1].Text.ToString();
txtClassName.Text GridView1.Rows[e.NewEditIndex].Cells[2].Text.ToString();
txtClassDescription.Text = GridView1.Rows[e.NewEditIndex].Cells[3].Text.ToString();
Run the form and click the Edit button as shown
After clicking, you will see the result as shown here
Finally, do some changes in the Class Name & class Description and put some
code in the click event of the button to update the database as shown here
Put the code in between two curly braces of the button_clcik event
DAL obj = new DAL();
string updateqry = "UPDATE tblitemclass Set ItemClassNAme=" + "'" +
txtClassName.Text + "', ITemClassDescription=" + "'" +
txtClassDescription.Text + "' where ItemClassSID=" +
Convert.ToInt32(txtClassSID.Text);
SlCommand cmd = new SqlCommand(updateqry,obj.opencon());
cmd.ExecuteNonQuery();
Do some changes as shown
Press the Update button, data will be saved in the database
Finally, you can show the Edit image instead of Edit text in the gridview. To do so,
follow these steps:
Open the screen
Click the Edit Columns as shown in the above figure
Next, in the screen below, look for the property: “EditImageURL”. Here enter the
path of the image that you want to shown in the gridview
Select the button type as image as shown
Then
Select the image by clicking the ellipses button.
At the bottom of the above screen there is Style Tab as shown here:
Expand the Property ControlStyle, and look for the Width and Height properties,
as shown
Set
Height = 30Px
Width = 50Px
Press the OK button, you will see the final designed page as shown here
Press OK button:
Congratulations! You have successfully implemented the qUpdate Command in
the ASP.Net web page with C-Sharp
Run the Page & you will see the page as shown below
That’s’ it what we want to do
By:
Saif ur Rehman Saifi
Assistant Professor
UIIT, PMAS Arid Agriculture University, Rawalpindi
Email: Saif@uaar.edu.pk