I am a new ASP.NET developer and trying to use a GridView control to show all the employees in the employee table in the database. I am working now in enabling updating the information in the GridView. I am facing the following probelm and I don’t know why:
**
Updating is not supported by data source ‘SqlDataSource1’ unless
UpdateCommand is specified.
**
FYI, I have the following database design:
Employee Table: Username, Name, JobTitle, BadgeNo, IsActive, DivisionCode
Divisions Table: SapCode, DivisionShortcut
(IsActive is like a flag (bit datatype) to indicate if the employee is in an assignment or not)
ASP.NET code:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="Username"
DataSourceID="SqlDataSource1" BorderWidth="1px" BackColor="#DEBA84"
CellPadding="3" CellSpacing="2" BorderStyle="None"
BorderColor="#DEBA84" OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowUpdating="GridView1_RowUpdating">
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<PagerStyle ForeColor="#8C4510"
HorizontalAlign="Center"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#A55129"></HeaderStyle>
<Columns>
<asp:CommandField ButtonType="Button" ShowEditButton="true" ShowCancelButton="true" />
<asp:TemplateField>
<ItemTemplate>
<%# Eval("DivisionShortcut")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DivisionsList" runat="server" DataSourceID="DivisionsListDataSource"
DataTextField="DivisionShortcut" DataValueField="SapCode"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Username" HeaderText="Network ID" ReadOnly="True"
SortExpression="Username" />
<asp:TemplateField>
<ItemTemplate>
<%# Eval("Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmployeeName" runat="server" Text='<%# Eval("Name")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%# Eval("JobTitle")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJobTitle" runat="server" Text='<%# Eval("JobTitle")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%# Eval("BadgeNo")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBadgeNo" runat="server" Text='<%# Eval("BadgeNo")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%# Eval("IsActive")%>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="isActive" runat="server" Text='<%# Eval("IsActive")%>' />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:UsersInfoDBConnectionString %>"
SelectCommand="SELECT dbo.Divisions.DivisionShortcut, dbo.employee.Username, dbo.employee.Name, dbo.employee.JobTitle, dbo.employee.BadgeNo, dbo.employee.IsActive
FROM dbo.Divisions INNER JOIN
dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode">
</asp:SqlDataSource>
<asp:SqlDataSource ID="DivisionsListDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:UsersInfoDBConnectionString %>"
SelectCommand="SELECT * FROM Divisions">
</asp:SqlDataSource>
Code-Behind:
//For updating the information in any row in the GridView
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gvrow = GridView1.Rows[e.RowIndex];
DropDownList DivisionsList = (DropDownList)gvrow.FindControl("DivisionsList");
TextBox txtEmployeeName = (TextBox)gvrow.FindControl("txtEmployeeName");
TextBox txtJobTitle = (TextBox)gvrow.FindControl("txtJobTitle");
TextBox txtBadgeNo = (TextBox)gvrow.FindControl("txtBadgeNo");
CheckBox isActive = (CheckBox)gvrow.FindControl("isActive");
//For getting the ID (primary key) of that row
string username = GridView1.DataKeys[e.RowIndex].Value.ToString();
string name = txtEmployeeName.Text;
string jobTitle = txtJobTitle.Text;
string badgeNo = txtBadgeNo.Text;
UpdateEmployeeInfo(username, name, jobTitle, badgeNo);
}
private void UpdateEmployeeInfo(string username, string name, string jobTitle, string badgeNo)
{
try
{
string connString = ConfigurationManager.ConnectionStrings["UsersInfoDBConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
string update = @"UPDATE Employee SET Name = @Name, JobTitle = @JobTitle,
BadgeNo = @BadgeNo
WHERE Username = @Username";
SqlCommand cmd = new SqlCommand(update, conn);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@JobTitle", jobTitle);
cmd.Parameters.AddWithValue("@BadgeNo", badgeNo);
cmd.Parameters.AddWithValue("@Username", username);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
GridView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}
So could you please help me in modifying this?
The
SqlDataSourcecan do the updates in the database directly for you.You don’t need to capture the new values on row updating and do the updates on your own.
For this you need to set the
UpdateCommandEg: