我有以下数据库设计:
Employee Table: Username, Name, JobTitle, BadgeNo, IsActive, DivisionCode
Divisions Table: SapCode, DivisionShortcut
(DivisionCode是Divisions Table中SapCode的外键)
我有一个GridView,我用它来添加,删除和更新/编辑员工信息。此信息是员工用户名,名称,BadgeNo,JobTitle,IsActive和DivisionShortcut。分部将列在DropDownList中。现在的问题是:更新员工的分工。我写了代码,但我收到以下错误: *
无效的列名称'DivisionShortcut'。
ASP.NET代码:
<asp:TemplateField HeaderText="Division">
<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 HeaderText="Name">
<ItemTemplate>
<%# Eval("Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmployeeName" runat="server" Text='<%# Bind("Name")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job Title">
<ItemTemplate>
<%# Eval("JobTitle")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJobTitle" runat="server" Text='<%# Bind("JobTitle")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Badge No.">
<ItemTemplate>
<%# Eval("BadgeNo")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBadgeNo" runat="server" Text='<%# Bind("BadgeNo")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active?">
<ItemTemplate>
<%# Eval("IsActive")%>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="isActive" runat="server"
Checked='<%# Eval("IsActive").ToString().Equals("True") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<span onclick="return confirm('Are you sure to Delete the record?')">
<asp:ImageButton ID="lnkB" runat="server" ImageUrl="Images/icons/delete24.png" CommandName="Delete" />
</span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
“
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"
UpdateCommand="UPDATE [employee] SET [Name] = @Name, [JobTitle] = @JobTitle,
[BadgeNo] = @BadgeNo WHERE [Username] = @Username"
DeleteCommand="DELETE FROM [employee] WHERE [Username] = @Username">
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="JobTitle" Type="String" />
<asp:Parameter Name="BadgeNo" Type="String" />
<asp:Parameter Name="Username" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="Username" Type="String" />
</DeleteParameters>
” SelectCommand =“SELECT * FROM Divisions”&gt;
代码隐藏: //用于编辑GridView中的任何行 protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; }
//For canceling any editng in any row in the GridView
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
e.Cancel = true;
GridView1.EditIndex = -1;
}
//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;
string division = DivisionsList.SelectedValue.ToString();
UpdateEmployeeInfo(username, name, jobTitle, badgeNo, division);
}
private void UpdateEmployeeInfo(string username, string name, string jobTitle, string badgeNo, string division)
{
string connString = ConfigurationManager.ConnectionStrings["UsersInfoDBConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
string update = @"UPDATE Employee SET Name = @Name, JobTitle = @JobTitle,
BadgeNo = @BadgeNo, DivisionShortcut = @division WHERE Username = @Username;
UPDATE Divisions SET [DivisionShortcut] = @division WHERE SapCode = @SapCode;";
SqlCommand cmd = new SqlCommand(update, conn);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@JobTitle", jobTitle);
cmd.Parameters.AddWithValue("@BadgeNo", badgeNo);
cmd.Parameters.AddWithValue("@division", division);
cmd.Parameters.AddWithValue("@Username", username);
//cmd.Parameters.AddWithValue("@IsActive", isActive.checked);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
GridView1.DataBind();
}
那么我如何能够使用GridView内的DropDownList更新员工的Divivsion字段?
更新
这是我的更新代码,但仍然没有与我合作: ASP.NET代码:
<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">
<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="Image" ShowEditButton="true" ShowCancelButton="true"
EditImageUrl="Images/icons/edit24.png" UpdateImageUrl="Images/icons/update24.png"
CancelImageUrl="Images/icons/cancel324.png" />
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<%# Eval("DivisionShortcut")%>
<asp:HiddenField ID="divisioncode" value='<%#Eval("divisioncode")%>' runat="server" />
</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 HeaderText="Name">
<ItemTemplate>
<%# Eval("Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmployeeName" runat="server" Text='<%# Bind("Name")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job Title">
<ItemTemplate>
<%# Eval("JobTitle")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJobTitle" runat="server" Text='<%# Bind("JobTitle")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Badge No.">
<ItemTemplate>
<%# Eval("BadgeNo")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBadgeNo" runat="server" Text='<%# Bind("BadgeNo")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active?">
<ItemTemplate>
<%# Eval("IsActive")%>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="isActive" runat="server"
Checked='<%# Eval("IsActive").ToString().Equals("True") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<span onclick="return confirm('Are you sure to Delete the record?')">
<asp:ImageButton ID="lnkB" runat="server" ImageUrl="Images/icons/delete24.png" CommandName="Delete" />
</span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:UsersInfoDBConnectionString %>"
SelectCommand="SELECT dbo.Divisions.DivisionShortcut,dbo.employee.DivisionCode, 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"
UpdateCommand="UPDATE [employee] SET [Name] = @Name, [JobTitle] = @JobTitle,
[BadgeNo] = @BadgeNo WHERE [Username] = @Username"
DeleteCommand="DELETE FROM [employee] WHERE [Username] = @Username">
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="JobTitle" Type="String" />
<asp:Parameter Name="BadgeNo" Type="String" />
<asp:Parameter Name="Username" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="Username" Type="String" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="DivisionsListDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:UsersInfoDBConnectionString %>"
SelectCommand="SELECT * FROM Divisions">
</asp:SqlDataSource>
代码隐藏:
//For editing any row in the GridView
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
}
//For canceling any editng in any row in the GridView
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
e.Cancel = true;
GridView1.EditIndex = -1;
}
//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");
HiddenField hidden = (HiddenField)gvrow.FindControl("divisioncode");
string division_code = hidden.Value.ToString();
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;
string division = DivisionsList.SelectedValue.ToString();
//string divisioncode = DivisionsList.SelectedValue;
UpdateEmployeeInfo(username, name, jobTitle, badgeNo, division, division_code);
}
private void UpdateEmployeeInfo(string username, string name, string jobTitle, string badgeNo, string division, string divisioncode)
{
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;
UPDATE Divisions SET [DivisionShortcut] = @division WHERE SapCode = @SapCode;";
SqlCommand cmd = new SqlCommand(update, conn);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@JobTitle", jobTitle);
cmd.Parameters.AddWithValue("@BadgeNo", badgeNo);
cmd.Parameters.AddWithValue("@division", division);
cmd.Parameters.AddWithValue("@Username", username);
cmd.Parameters.AddWithValue("@SapCode", divisioncode);
//cmd.Parameters.AddWithValue("@IsActive", isActive.checked);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
GridView1.DataBind();
}
答案 0 :(得分:2)
以下行代码部分出错:
string update = @"UPDATE Employee SET Name = @Name, JobTitle = @JobTitle,
BadgeNo = @BadgeNo, DivisionShortcut = @division WHERE Username = @Username;
^^
UPDATE Divisions SET [DivisionShortcut] = @division WHERE SapCode = @SapCode;";
dividionshortcut不是员工表的成员。只需将其从标记位置移除并再次运行它我确定这将删除您的错误。
要使用的代码。 在gridview中添加以下字段:
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<%# Eval("DivisionShortcut")%>
<asp:HiddenField ID="divisioncode" value='<%#Eval("divisioncode")%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DivisionsList" runat="server" DataSourceID="DivisionsListDataSource"
DataTextField="DivisionShortcut" DataValueField="SapCode">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
首先使用以下select命令
SelectCommand="SELECT dbo.Divisions.DivisionShortcut,dbo.employee.DivisionCode, 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"
UpdateCommand="UPDATE [employee] SET [Name] = @Name, [JobTitle] = @JobTitle,
[BadgeNo] = @BadgeNo WHERE [Username] = @Username"
DeleteCommand="DELETE FROM [employee] WHERE [Username] = @Username">
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="JobTitle" Type="String" />
<asp:Parameter Name="BadgeNo" Type="String" />
<asp:Parameter Name="Username" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="Username" Type="String" />
</DeleteParameters>
当调用行更新事件时,请使用以下命令:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gvrow = GridView1.Rows[e.RowIndex];
DropDownList DivisionsList = (DropDownList)gvrow.FindControl("DivisionsList");
HiddenField hidden = (HiddenField)gvrow.FindControl("divisioncode");
string division_code=hidden.value.ToString();
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;
string division = DivisionsList.SelectedValue.ToString();
UpdateEmployeeInfo(username, name, jobTitle, badgeNo, division,division_code);
}
现在在代码后面为函数中的除法代码添加一个参数,并调用以下更新函数:
private void UpdateEmployeeInfo(string username, string name, string jobTitle, string badgeNo, string division,string divisioncode)
{
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;
UPDATE Divisions SET [DivisionShortcut] = @division WHERE SapCode = @SapCode;";
SqlCommand cmd = new SqlCommand(update, conn);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@JobTitle", jobTitle);
cmd.Parameters.AddWithValue("@BadgeNo", badgeNo);
cmd.Parameters.AddWithValue("@division", division);
cmd.Parameters.AddWithValue("@Username", username);
cmd.Parameters.AddWithValue("@DapCode", divisioncode);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
GridView1.DataBind();
}
我希望这能解决你的问题
答案 1 :(得分:0)
这是你的代码
UPDATE Employee SET Name = @Name, JobTitle = @JobTitle,
BadgeNo = @BadgeNo, DivisionShortcut = @division WHERE Username = @Username;
DivisionShortcut不是Employee表的成员。你有一个更新声明,所以我认为你只是意外地把它留在那里,从这个更新行删除它将解决问题。