Tags: ado, autogenerate, column, contacts, identity, int, key, net, primary, server, sql, table

Identity column wont let me autogenerate

On .Net » .Net ADO

1,972 words with 0 Comments; publish: Tue, 01 Jan 2008 02:27:00 GMT; (10031.25, « »)

Hi,

I'm using C# against MS SQL server. My table, "Contacts", has an "ID" column that is a primary key and Identity...simple int. When I try to add a row from my code, I'm getting a runtime error:

"Column 'ID' does not allow nulls."

Why do I need to provide an ID value when the table is autogenerating for me? I tried providing it a value and, of course, it doesn't like it. Here's my "Add" button function:

private void butAdd_Click(object sender, System.EventArgs e)

{

DataRow rowNew = dsContacts1.Contacts.NewRow();

rowNew["FirstName"] = "new";

rowNew["LastName"] = "user";

...etc, etc.

dsContacts1.Contacts.Rows.Add(rowNew);

adptContacts.Update(dsContacts1);

}

Here's my PageInit with my SQL commands:

private void Page_Init(object sender, EventArgs e)

{

InitializeComponent();

adptContacts = new SqlDataAdapter("SELECT ID, "+ "LastName FROM Contacts ORDER BY ID", sqlConnection1);

adptContacts.InsertCommand = new SqlCommand("InsertContact", sqlConnection1);

adptContacts.InsertCommand.CommandType = CommandType.StoredProcedure;

adptContacts.InsertCommand.Parameters.Add(".net-ado.itags.org.LastName",

SqlDbType.NVarChar, 50, "LastName");

adptContacts.InsertCommand.Parameters.Add(".net-ado.itags.org.FirstName",

SqlDbType.NVarChar, 50, "FirstName");

...etc, etc.

SqlParameter myParm = adptContacts.InsertCommand.Parameters.Add(

".net-ado.itags.org.Identity", SqlDbType.Int, 0, "ID");

myParm.Direction = ParameterDirection.Output;

sqlConnection1.Open();

adptContacts.Fill(dsContacts1);

dgContactList.DataBind();

sqlConnection1.Close();

}

Where dgContactList is my DataGrid, dsContacts1 is my DataSet, and adptContacts is my DataAdapter.

Any help is appreciated.

Thanks,

Jordan

All Comments

Leave a comment...

  • 0 Comments