簡體   English   中英

將新值插入表C#ASP.NET

[英]Insert new value to table C# ASP.NET

我正在嘗試將新記錄添加到我的表tblEmployee 這是我的代碼:

        cb = new SqlCommandBuilder(daEmp);

        DataRow dRow = dsEmp.Tables["tblEmployee"].NewRow();
        dRow["EmployeeID"] = txtID.Text;
        dRow["Lname"] = txtLname.Text;
        dRow["Fname"] = txtFname.Text;
        dRow["Mname"] = txtMname.Text;
        dRow["Address"] = txtAddress.Text;
        dRow["Email"] = txtEmail.Text;
        dRow["Phone"] = Convert.ToInt64(txtPhone.Text);
        dRow["Jobtitle"] = txtJobtitle.Text;
        dRow["Salary"] = txtSalary.Text;
        dRow["DeptID"] = drpDepartments.SelectedValue;

        dsEmp.Tables["tblEmployee"].Rows.Add(dRow);
        daEmp.Update(dsEmp, "tblEmployee");
        dsEmp.Tables["tblEmployee"].AcceptChanges();

我在這一行收到此錯誤消息: dRow["Phone"] = txtPhone; 它說Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'.Couldn't store <System.Web.UI.WebControls.TextBox> in Phone Column. Expected type is Int64. Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'.Couldn't store <System.Web.UI.WebControls.TextBox> in Phone Column. Expected type is Int64.

文本框的代碼:

<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>

我的數據庫上的Phone列的數據類型是int但我將其更改為bigint並仍然得到相同的錯誤。 什么似乎是問題?

順便說一句,我正在使用C#ASP.NET。

使用txtPhone.Text獲取文本框中的值而不是文本框對象本身。

如果Phone字段是數字字段,那么您應該:

dRow["Phone"] = Convert.ToInt64(txtPhone.Text);

使用txtPhone.Text分配電話號碼的值。

嘗試這個

protected void btnAdd_Click(object sender, EventArgs e) {
        cb = new SqlCommandBuilder(daEmp);

        DataRow dRow = dsEmp.Tables["tblEmployee"].NewRow();
        dRow["Lname"] = txtLname.Text;
        dRow["Fname"] = txtFname.Text;
        dRow["Mname"] = txtMname.Text;
        dRow["Address"] = txtAddress.Text;
        dRow["Email"] = txtEmail.Text;
        dRow["Phone"] = txtPhone.Text;
        dRow["Jobtitle"] = txtJobtitle.Text;
        dRow["Salary"] = txtSalary.Text;
        dRow["DepartmentID"] = drpDepartments.SelectedValue;

        dsEmp.Tables["tblEmployee"].Rows.Add(dRow);
        daEmp.Update(dsEmp, "tblEmployee");
        dsEmp.Tables["tblEmployee"].AcceptChanges();

    }

所有人的拳頭都使用.Text屬性,對於手機投射它是整數

protected void btnAdd_Click(object sender, EventArgs e) 
{
    cb = new SqlCommandBuilder(daEmp);

    DataRow dRow = dsEmp.Tables["tblEmployee"].NewRow();
    dRow["Lname"] = txtLname.Text;
    dRow["Fname"] = txtFname.Text;
    dRow["Mname"] = txtMname.Text;
    dRow["Address"] = txtAddress.Text;
    dRow["Email"] = txtEmail.Text;
    dRow["Phone"] = Convert.ToInt32(txtPhone.Text);
    dRow["Jobtitle"] = txtJobtitle.Text;
    dRow["Salary"] = txtSalary.Text;
    dRow["DepartmentID"] = drpDepartments.SelectedValue;
    dsEmp.Tables["tblEmployee"].Rows.Add(dRow);
    daEmp.Update(dsEmp, "tblEmployee");
    dsEmp.Tables["tblEmployee"].AcceptChanges();
}

只需添加txtPhone.text即可將手機轉換為文本。 實際上你應該對webforms中的所有字段值進行操作,並且還需要對每個表單字段進行適當的驗證,這是一個很好的做法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM