How to insert primary key via row[0]= ?

jpergega

New member
Joined
Feb 12, 2015
Messages
3
Programming Experience
Beginner
Hi
I am having trouble to add new record to my database. Without assign anything to the primary key field I am keep getting an error says "string or binary data would be truncated, statement terminated" I have set the PK field to Auto Increment so I don't really want to insert ids myself.

Please help:

DataRow row = ds.Tables[0].NewRow();
row[0] = ????
row[1] = textBox1.Text;
row[2] = textBox3.Text;

ds.Tables[0].Rows.Add(row);

try
{
objConnect.UpdateDatabase(ds);
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
MessageBox.Show("New Record Added");
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,140
Location
Sydney, Australia
Programming Experience
10+
That's got nothing to do with the primary key. The error message is telling you that the string you're trying to insert is too long for the destination. How long are those two strings you're using and what is the maximum size of the data type of the column your inserting it into? If that's not the issue, what is the maximum size of the parameter you're trying to insert it via?
 

jpergega

New member
Joined
Feb 12, 2015
Messages
3
Programming Experience
Beginner
Hi
You are right one of the column size was too small.

Thanks for your help
:encouragement:
 
Top Bottom