0

我有以下数据库设计:

Employee Table: 
---------------
Username, Name, JobTitle, BadgeNo, IsActive, DivisionCode 


Divisions Table:    
----------------
SapCode, DivisionShortcut

我有一个 GridView,我用它来添加、删除和更新/编辑员工信息。此信息是员工用户名、姓名、徽章编号、职位名称、IsActive(来自 Employee 表)和 DivisionShortcut(来自 Divsions 表)。部门将列在 DropDownList 中。分区将在 GridView 内作为 DropDownList 列出,并将在编辑模式下显示。我现在正在努力通过这个 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:LinkButton ID="lnkB" runat="Server" Text="Delete" CommandName="Delete"></asp:LinkButton>
                        </span>
                    </ItemTemplate>
                </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"
            UpdateCommand="UPDATE [employee], [Divisions] SET [Name] = @Name, [JobTitle] = @JobTitle, 
                                                [BadgeNo] = @BadgeNo, [DivisionShortcut] = @division 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="DivisionShortcut" Type="String" />
                <asp:Parameter Name="Username" Type="String" />
            </UpdateParameters>
            <DeleteParameters>
                <asp:Parameter Name="Username" Type="String" />
            </DeleteParameters>
        </asp:SqlDataSource>
    

我忘了提到 DropDownList 有一个 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 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";
        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;
            //            SqlDataSource1.UpdateCommand = @"UPDATE Employee SET Name = @Name, JobTitle = @JobTitle, 
            //                                                BadgeNo = @BadgeNo 
            //                                                WHERE Username = @Username";
            //            SqlDataSource1.Update();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
        }
        GridView1.DataBind();
    }

那么我如何才能通过 DropDownList 更新员工的部门呢?

更新:

我更新了 UpdateEmployeeInfo() 方法,但仍然遇到同样的错误。

更新代码:

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 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();
    }
4

1 回答 1

0

我所看到的是您正在使用UpdateEmployeeInfo方法来更新 Employee 表的列以及 Division 表的列,但是在那个 sql 查询中,您只提到了员工表,这就是您收到此错误的原因,因为 DivisionShortcut不是员工的一部分桌子。

根据我的理解,您应该使用DivisionCode代替DivisionShortcut 并为您的下拉列表设置 AutoPostBack="True"

于 2012-07-11T11:35:25.027 回答