Hi dorsa,
Refer below sample.
Refer below link to crud operation using sqldatasource.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerId"
OnRowUpdating="OnRowUpdating" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="Title">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Mudassar</asp:ListItem>
<asp:ListItem>John</asp:ListItem>
<asp:ListItem>Robert</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" ReadOnly="True" SortExpression="CustomerId"
InsertVisible="False" />
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCountry" Text='<%#Eval("Country") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true"
ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT * FROM [Customers]" UpdateCommand="UPDATE [Customers] SET [Name] = @Name, [Country] = @Country WHERE [CustomerId] = @CustomerId"
OnUpdating="sqlDtSrcEditCourse_Updating">
<UpdateParameters>
<asp:Parameter Name="CustomerId" Type="Int32" />
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Country" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
Code
C#
int customerId;
string name;
string country;
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
customerId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
country = (row.FindControl("txtCountry") as TextBox).Text;
name = (row.FindControl("DropDownList1") as DropDownList).SelectedValue;
SqlDataSource1.Update();
}
protected void sqlDtSrcEditCourse_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
try
{
e.Command.Parameters["@Name"].Value = name;
e.Command.Parameters["@Country"].Value = country;
e.Command.Parameters["@CustomerId"].Value = customerId;
}
catch
{
}
}
VB.Net
Private customerId As Integer
Private name As String
Private country As String
Protected Sub OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
customerId = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Values(0))
country = (TryCast(row.FindControl("txtCountry"), TextBox)).Text
name = (TryCast(row.FindControl("DropDownList1"), DropDownList)).SelectedValue
SqlDataSource1.Update()
End Sub
Protected Sub sqlDtSrcEditCourse_Updating(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs)
Try
e.Command.Parameters("@Name").Value = name
e.Command.Parameters("@Country").Value = country
e.Command.Parameters("@CustomerId").Value = customerId
Catch
End Try
End Sub
Screenshot
![](https://i.imgur.com/UXJFK8w.gif)