How to insert a record in a table?

Feb 17, 2010 at 8:46 PM

I am trying to insert a record in Customer table. Customer_Id is a primary key, defined as guid and defaults to
newsequentialid() in SQL SERVER 2005.   Here is the code:

CustomerCollection customerCollection = new CustomerCollection();
            Customer cust = customerCollection.NewItem();
            cust.CompanyName = txtCustomerName.Text;
            cust.IsActive = chkClientStatus.Checked;
            custmerCollection.AddItem(cust);
            customerCollection.Persist();

Is this the correct way of adding a record?

I am getting an error 'Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). Your help is much appreciated.

 

 

 

 

 

 

 

 

 

 

Feb 20, 2010 at 7:07 AM

you did'nt pass a guid value in your Customer_Id.  You have to pass a value in your Customer_Id 

Ex.

Guid customerId = Guid.NewGuid();

CustomerCollection customerCollection = new CustomerCollection();
            Customer cust = customerCollection.NewItem();
	    cust.CustomerId = customerId;
            cust.CompanyName = txtCustomerName.Text;
            cust.IsActive = chkClientStatus.Checked;
            custmerCollection.AddItem(cust);
            customerCollection.Persist();

try this one.

Coordinator
Feb 24, 2010 at 12:48 PM

Yes this is the right track. If you wish to explicitly insert a specific primary key value into a table, you must specify it in the construction of the object.

CustomerCollection customerCollection = new CustomerCollection();
Customer cust = customerCollection.NewItem(Guid.NewGuid());
cust.CompanyName = txtCustomerName.Text;
cust.IsActive = chkClientStatus.Checked;
custmerCollection.AddItem(cust);
customerCollection.Persist();