想通过UI在我的下拉列表中添加一个新项目,以便它也添加到数据库中

时间:2014-06-27 12:23:16

标签: c# asp.net

这是我在asp.net中的表单:

<div>
    <fieldset>
        <legend>Sign Up NOW!!</legend>
        <table>
            <tr>
                <td>
                    <asp:Label ID="lblUserName" runat="server" Text="User Name">
                    </asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rqrdUserName" runat="server" 
                        Text="Required" ForeColor="red" 
                        ErrorMessage="Please enter UserName" 
                        ControlToValidate="txtUserName" ValidationGroup="SignUp">
                    </asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblFirstName" runat="server" Text="First Name">
                    </asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rqrdFirstName" runat="server" 
                        Text="Required" ForeColor="red" 
                        ErrorMessage="Please provide first name" 
                        ControlToValidate="txtFirstName" ValidationGroup="SignUp">
                    </asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblLastName" runat="server" Text="Last Name">
                    </asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rqrdLastName" runat="server" 
                        Text="Required" ForeColor="red" 
                        ErrorMessage="Please provide last name" 
                        ControlToValidate="txtLastName" ValidationGroup="SignUp">
                    </asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblGender" runat="server" Text="Gender">
                    </asp:Label>
                </td>
                <td>
                    <asp:RadioButtonList ID="rdbtnlst" runat="server">
                        <asp:ListItem>Male</asp:ListItem>
                        <asp:ListItem>Female</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblDob" runat="server" Text="Date of Birth">
                    </asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtDob" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblEmail" runat="server" Text="Email">
                    </asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtEmail" runat="server" TextMode="Email">
                    </asp:TextBox>
                    <asp:RequiredFieldValidator ID="rqrdEmail" runat="server" 
                        ForeColor="red" ErrorMessage="Please provide Email address" 
                        ControlToValidate="txtEmail" ValidationGroup="SignUp">
                    </asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblPassword" runat="server" Text="Password">
                    </asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" 
                        TextMode="Password">
                    </asp:TextBox>
                    <asp:RequiredFieldValidator ID="rqrdPassword" runat="server" 
                        ForeColor="red" ErrorMessage="You must provide a password" 
                        ControlToValidate="txtPassword" 
                        ValidationGroup="SignUp">
                    </asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
               <td>
                   <asp:Label ID="lblConfirmPassword" runat="server" 
                       Text="Confirm Password">
                   </asp:Label>
               </td>
               <td>
                   <asp:TextBox ID="txtConfirmPassword" runat="server" 
                       TextMode="Password">
                   </asp:TextBox>
                   <asp:CompareValidator ID="matchPassword" runat="server" 
                       ForeColor="red" ErrorMessage="Passwords don't match" 
                       ControlToValidate="txtConfirmPassword" 
                       ControlToCompare="txtPassword" ValidationGroup="SignUp">
                   </asp:CompareValidator>
               </td>
           </tr>
           <tr>
               <td></td>
               <td>
                   <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
                       OnClick="btnSubmit_Click" />
               </td>
               <asp:ValidationSummary ID="ValidationSummary1" runat="server" 
                   ValidationGroup="SignUp" />
           </tr>
       </table>
    </fieldset>
</div>

这是我的小事:

 public void addNewDepartment(string name)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@dept_Name", SqlDbType.VarChar);
            param[0].Value = name;

            objdbConnection = new dbConnection();
            objdbConnection.executeInsertprocedure("addNewDepartment", param);
        }
        public DataTable getDepartments()
        {
            objdbConnection = new dbConnection();
            string query = "getDepartments";
            try
            {
                return objdbConnection.executeSelectProcedure(query);
            }
            catch
            {
                return null;
            }
        }
        public DataTable getPosition()
        {
            objdbConnection = new dbConnection();
            string query1 = "getPosition";
            try
            {
                return objdbConnection.executeSelectProcedure(query1);
            }
            catch
            {
                return null;
            }
        }

这是我的c#课程:

public partial class SignUp : System.Web.UI.Page
    {
        private BLL BLL;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getDepartments();
                getPosition();
            }
        }
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            BLL = new BLL();
            BLL.insertEmployee(txtName.Text, ddlDepartment.Text, Convert.ToInt32(txtSalary.Text), txtEmail.Text, ddlPosition.Text, txtPassword.Text, Convert.ToInt32(txtContactNo.Text), txtAddress.Text);
        }
        private void getDepartments()
        {
            BLL = new BLL();
            DataTable dtDepartments = BLL.getDepartments();
            ddlDepartment.DataSource = dtDepartments;
            ddlDepartment.DataTextField = dtDepartments.Columns[1].ToString();
            ddlDepartment.DataValueField = dtDepartments.Columns[0].ToString();

            ddlDepartment.DataBind();
        }
        private void getPosition()
        {
            BLL = new BLL();
            DataTable dtPosition = BLL.getPosition();
            ddlPosition.DataSource = dtPosition;
            ddlPosition.DataTextField = dtPosition.Columns[1].ToString();
            ddlPosition.DataValueField = dtPosition.Columns[0].ToString();
            ddlPosition.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            BLL = new BLL();
            BLL.addNewDepartment(txtNewDept.Text);
            ddlDepartment.Items.Add(txtNewDept.Text);
        }
    }
}

我在该部门的登录表单中有一个下拉列表。它的数据源绑定到数据库。我想通过UI在数据库中添加项目。它不是在数据库中插入项目。我该怎么做?

0 个答案:

没有答案