Reputation: 195
I'm trying to add new records to my table tblEmployee
. This is my code:
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();
I'm getting this error message on this line: dRow["Phone"] = txtPhone;
. It says 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.
The code for the textbox:
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
The datatype of Phone
column on my database is int but I changed it to bigint and still getting the same error. What seems to be the problem?
By the way I'm using C# ASP.NET.
Upvotes: 0
Views: 315
Reputation: 39
Just cast your phone to text by adding txtPhone.text. Actually you should do it for all fields value from webforms and also need to proper validation for every form fields it is a good practice..
Upvotes: 0
Reputation: 1959
Fist of all use the .Text
property everywhere and for phone cast it is integer
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();
}
Upvotes: 0
Reputation: 10275
try this
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();
}
Upvotes: 0
Reputation: 5947
Use txtPhone.Text
to assign the value of the Phone Number.
Upvotes: 0
Reputation: 35812
If the Phone
field is a number field, then you should:
dRow["Phone"] = Convert.ToInt64(txtPhone.Text);
Upvotes: 1
Reputation: 9527
Use txtPhone.Text to get the value in the textbox instead of the textbox object itself.
Upvotes: 1