getting error while inserting a record [Resolved]

Posted by Sreekanthv5 under ASP.NET on 9/6/2011 | Points: 10 | Views : 967 | Status : [Member] | Replies : 2
I am getting error like this

String or binary data would be truncated.
The statement has been terminated.

here is the my code

in DAL.cs
public bool insertemployee(Cmp c)
{
EmployeesDataContext db = new EmployeesDataContext();
Employee emp=new Employee();
emp.empid = c.empid;
emp.empname = c.empname;
emp.salary = c.salary;
emp.hiredate = c.hiredate;
db.Employees.InsertOnSubmit(emp);
db.SubmitChanges();
return true;
}

in Default.aspx.cs
protected void btnsubmit_Click(object sender, EventArgs e)
{
Cmp c = new Cmp();
Employe emp = new Employe();
c.empid = Convert.ToInt32(txtempid.Text);
c.empname = txtempname.ToString();
c.hiredate = Convert.ToDateTime(txthiredate.Text);
c.salary = Convert.ToInt32(txtsalary.Text);
emp.insertemployee(c);
}




Responses

Posted by: Sreekanthv5 on: 9/7/2011 [Member] Starter | Points: 25

Up
0
Down

Resolved
It is working now.
i changed to
c.empname = txtempname.ToString();

to

c.empname = txtempname.Text ;

Sreekanthv5, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Niladri.biswas on: 9/6/2011 [Member] Platinum | Points: 25

Up
0
Down
From the message "String or binary data would be truncated. ", we can figure out that the record length from the client application has exceeded the size of the database table column. For example we have a column say Name of varchar whose size is defined as 10 . Now we are attempting to insert a record as 'This name is bigger than a size of 10' from the DAL layer. In such a case we will receive this error.

So please increase teh column size for which there is a data length mismatch and this error will go away.

Thanks

Best Regards,
Niladri Biswas

Sreekanthv5, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response