Beginner needs help inserting a row

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

I would like to insert a row into a table using a command builder.

table: col 1: string, col 2: uniqueidentifier. (I set the newID() as the
default value for uniqueid).

Do I need then to generate the ID before inserting it, or does the update
function take care?

Here is my code to insert a row into a table, something seems to be wrong:

SqlConnection myConn = new SqlConnection ( "server=xyz; Integrated
Security=SSPI; Persist Security Info=False; database=myDB" );

myConn.Open();

SqlDataAdapter myAdapter = new SqlDataAdapter ( "select * from table",
myConn );

SqlCommandBuilder custCB = new SqlCommandBuilder(myAdapter);

DataSet dataset = new DataSet();

myAdapter.Fill(dataset, "table");

DataRow newrow = sponsors.Tables["table"].NewRow();

newrow["somestring"] = "xyz";


dataset.Tables["table"].Rows.Add(newrow);


myAdapter.Update(dataset,"table");

myConn.Close();

myConn.Dispose();
 
On the column (assume the column index is 1 of this column)...
mydataSet.Tables["table"].Columns[1].AutoIncrement = true;
Set the AutoIncrmementSeed to 0 so it begins at 0 then set the
autoincrementvalue to -1 so it always generates a negative value. When the
DB sees this negative value, it knows it isn't kosherr and it steps in and
ensures a valid assignment. This is the most practical (there are many ways
but this is easy to use and implmement) way to ensure you don't get
conflicts in a multiuser scenario.

Head over the www.betav.com and check out some of Bill's articles under the
MSDN sectino like "Weaning Developers from the CommandBuilder" and managing
an @@IDentity crisis. Even though they are mprimarily comprised of SQL
Server examples, the logic is virtually identical and they are great
articles.

HTH,

Bill
 
Hi Alex,

Did not check all your code, but you can start with setting this for the
Fill
myAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

I am not sure if this is complete with it.

So when it is not, message back?

Cor
 
Hello,

I'm getting an exception on my row add command:
Column 'uniqueidentifier' does not allow nulls.

This is on this call:
dataset.Tables["table"].Rows.Add(newrow);

I did add the code line below just before creating the dataset.

Do I still have to generate a newID()? If so, how?
 
Back
Top