开发者

LINQ Insert query

Me again!

I have a question re. inserting into a sql table using LINQ. I am essentially creating a feedback form whereby a user enters there details, hits submit and their data is inserted into a sql table.

I haven't got very far!!!

protected void btnSubmitFeedback_Click(object sender, EventArgs e)
{
    ORFeedDataClassesDataContext db = new ORFeedDataClassesDataContext();
    tblOnlineReportingFeedback newtblonlineReportingFeedback = new tblOnlineReportingFeedback();
    newtblonlineReportingFeedback.Name = "txtbxFdName";
    db.tblOnlineReportingFeedbacks.InsertOnSubmit(newtblonlineReportingFeedback);
    db.SubmitChanges();
}

I have set up the sql table so that an unique autonumber is inserted each time a row is inserted. However, when I run the above, I receive开发者_StackOverflow中文版 the following:

"SqlException was handled by user code - String or binary data would be truncated. The statement has been truncated"

Can anyone think of a work around for this? Chances ar emy code is a load of poo anyway so if someone can correct me, I would be most grateful.


The error you're getting is caused by your Name column in the tblOnlineReportFeedback table.

The length of the column is not long enough to hold the data.

try changing the column to at least VARCHAR(11)

Also, i'm not sure if you're meaning to put txtbxFdName as the string to insert. It sounds to be it should be getting it from a textbox named "txtbxFdName"

  newtblonlineReportingFeedback.Name = txtbxFdName.Text;


The error message states that the field Name in the database is too small to contain the value you're trying to insert. Try redesigning the table to fit more characters


I would check the lengths of your column in your table ... looks like you are trying to put in something a wee bit too big and hence the truncation.

Kindness,

Dan


What a tool!!! I got that excited about LINQ I forgot to make sure the table was set up correctly.

Maybe its time to step away from the computer for a while!

Hang on a second, I have run it, the error reported above is sorted but I the name returned is "txtbxFdName" as opposed to the value within that particular textbox.

Just if anyone is looking in at this, the correct code should look like this:

  protected void btnSubmitFeedback_Click(object sender, EventArgs e)
{
    ORFeedDataClassesDataContext db = new ORFeedDataClassesDataContext();
    tblOnlineReportingFeedback newtblonlineReportingFeedback = new tblOnlineReportingFeedback();
    newtblonlineReportingFeedback.Name = txtbxFdName.Text;
    db.tblOnlineReportingFeedbacks.InsertOnSubmit(newtblonlineReportingFeedback);
    db.SubmitChanges();

}

Thanks for a fresh pair of eyes all.


You may want to take a look at not only your column sizes in the actual table, but in the properties of the objects in the .dbml view too, make sure all the datatypes in this pane match up to those in the actual table.
You should also consider putting this code into a using statement as its good practise to minimise memory problems =]

using(ORFeedDataClassesDataContext db = new ORFeedDataClassesDataContext())
{
   tblOnlineReportingFeedback newtblonlineReportingFeedback = new tblOnlineReportingFeedback
   newtblonlineReportingFeedback.Name = txtbxFdName.Text;
   db.tblOnlineReportingFeedbacks.InsertOnSubmit(newtblonlineReportingFeedback);
   db.SubmitChanges();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜