InsertOnSubmit for linq inserts data at first row in table.

  • Thread starter Thread starter Vaibhav
  • Start date Start date
V

Vaibhav

Hello,

I tried to insert a row in database table but its getting
inserted as the first row in the table. My second question is if i try to add
another record after first it gives me primary key violation

Here is the code below for ADD butoon :

protected void btnAdd_Click(object sender, EventArgs e)
{
try
{
Domain_List insertDom = new Domain_List { DomainName =
domainName.Text, DomainManagerID = managerID.Text };

db.Domain_Lists.InsertOnSubmit(insertDom);

db.SubmitChanges();
}
catch(Exception ex)
{
lblMsg.Text = ex.Message;
}

}
 
Vaibhav said:
I tried to insert a row in database table but its getting
inserted as the first row in the table.

That's not a problem of LINQ but a problem in your interpretation of how
databases work. The rows in a table in a relational database DO NOT HAVE ANY
ORDER. When you do a "select * from theTable", the database engine is free
to return the rows in whatever order it finds them. If you want a specific
order you need to specify "... order by Whatever". Therefore, there is no
such thing as "the first row in the table", as you said. No row is the
first, unless you specify an "order by".

Of course, usually, the rows come out in the same order as you added
them. But this will change as soon as you remove some rows so there is some
free space in the middle of the table. The next row will be inserted in that
space and the next Select will retrieve it in the position in which it
happened to find a place.
The preceding is for a table that does not have a clustered index. If it
has a clustered index, records will come out in the order of the index,
which most probably will not be the order in which you inserted them, unless
it is indexed on an IDENTITY or otherwise auto-incremented column.
 
Thanks Alberto,

But for my second question if i try to add another item it
gives me a primary key violation exception. What could be the reason? Can it
be that line

db.Domain_Lists.InsertOnSubmit(insertDom);
doesnt alow same named object to be added

or ds.SubmitChanges() method.
 
Vaibhav said:
But for my second question if i try to add another item it
gives me a primary key violation exception.

This means that you are inserting two rows that have the same value for
the column that is the primary key in the table. The primary key does not
allow duplicates.
What could be the reason? Can it
be that line

db.Domain_Lists.InsertOnSubmit(insertDom);
doesnt alow same named object to be added

The object name is irrelevant. It's the *contents* of the object you
need to worry about. From the code you posted, you are doing the following:

Domain_List insertDom = new Domain_List { DomainName =
domainName.Text, DomainManagerID = managerID.Text };

So this would imply that you are creating an object with two properties,
that will be assigned to a row with two columns in the datatable. One or
both of these columns are the primary key for the table. The values for this
or these columns must be unique, since the database will not allow
duplicates. So you have to ensure that either domainName.Text or
managerID.Text (or both) contain different values each time you run your
code, or the server will reject the new record.
 
Back
Top