Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在表C#ASP.NET中插入新值_C#_Asp.net_Sql - Fatal编程技术网

在表C#ASP.NET中插入新值

在表C#ASP.NET中插入新值,c#,asp.net,sql,C#,Asp.net,Sql,我正在尝试将新记录添加到我的表tblEmployee。这是我的代码: cb = new SqlCommandBuilder(daEmp); DataRow dRow = dsEmp.Tables["tblEmployee"].NewRow(); dRow["EmployeeID"] = txtID.Text; dRow["Lname"] = txtLname.Text; dRow["Fname"] = txtFna

我正在尝试将新记录添加到我的表
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。它表示
无法将“System.Web.UI.WebControls.TextBox”类型的对象强制转换为“System.IConvertible”。无法存储在电话列中。预期类型为Int64。

文本框的代码:

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

在我的数据库中,
Phone
列的数据类型是int,但我将其更改为bigint,仍然得到相同的错误。有什么问题吗


顺便说一句,我使用的是C#ASP.NET。

使用txtPhone.Text来获取textbox中的值,而不是textbox对象本身。

如果
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
属性,对于phone cast,它是整数

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中的所有字段值执行此操作,并且还需要对每个表单字段进行适当的验证。这是一个很好的做法

尝试将其更改为字符串,并使用
textbox.Text
获取值。或者,如果您仍然希望手机作为
int
使用
txtPhone.Text
上的
Convert.ToInt64
另一个错误
列“EmployeeID”不允许空值。
此行的错误
dsEmp.Tables[“tblEmployee”].Rows.Add(dRow)谢谢大家的帮助!现在一切正常。关于货币数据类型,我应该使用什么?另一个错误
列'EmployeeID'不允许空值。
此行错误
dsEmp.Tables[“tblEmployee”].Rows.Add(dRow)
您需要设置
dRow[“EmployeeID”]
。谢谢您的帮助!