tags:

views:

188

answers:

3

I have a table which links to another table in the ASP.NET membership schema.

Problem is, all the PKs for the ASP.NET tables are uniqueidentifier so mine has to be too. When I add a SqlDatasource and call its Insert() method, I get the following error:

Cannot insert the value NULL into column 'DiscountCode', table 'CreamDb.dbo.CustomInfo1'; column does not allow nulls. INSERT fails. The statement has been terminated.

The uniqueidentifier is also treated as an object (its data type), but there is no Guid data type. I had this problem before, but the schema was much simpler so I could fix it.

How can I go about fixing this? If I get rid of the data type part in the markup (so just leave the field/parameter name but not the data type stuff), I get another error so that is not possible.

Thanks

+1  A: 

What do you mean by "there is no Guid data type"? What's wrong with System.Guid? Can't you just use Guid.NewGuid(), set the field appropriately, and do the insert?

EDIT: Just to give a bit more meat: attach an event handler to the Inserting event, and populate the field then, via the DbCommand returned by SqlDataSourceCommandEventArgs.Command. Or change the SQL used by the INSERT command to ask the database to populate the GUID field for you.

Jon Skeet
Hi,What I mean is in the markup (aspx) you don't have the Guid datatype to use. Only object exists. You can try this out by adding a sql datasource which performs CRUD on a table, and then checking the markup and seeing the intellisense for datatypes for parameters.
dotnetdev
This isn't a C# issue, it's purely .NET. I shouldn't have to write any custom code as sqldatasource should take care of the heavy lifting for me.
dotnetdev
Well, it seems to me that SqlDataSource is doing rather a lot of the heavy lifting for you, and you just have to write a tiny bit of code to fill in the right value when it creates a new row. (I don't have much experience with SqlDataSource, but that should be easy enough.)
Jon Skeet
+1 I agree with Jon, by attaching to the inserting event you just need a simple line to add the parameter to the command. Also changing the sql is straight forward. That said, there is also a third option, which is changing the table so its gets a new guid by default.
eglasius
Is there an example of any of this?
dotnetdev
A: 

A popullar approach when dealing with references to the ASP.NET Membership Provider's data is, instead of keeping a proper foreign key to the GUIDs, instead store something like the LoweredUserName in your table. Then, use the Membership Provider's API to interact with the object you need. In some cases, you need an ObjectDataSource abstraction layer to accomplish CRUD scenarios.

JoshJordan
Is there an example of this?
dotnetdev
I don't have one on-hand, but I could provide one once I get home. I'm going to edit in some details to help you (and others) get started with the ObjectDataSource.
JoshJordan
A: 

Set the default value of the column in SQL Sever to "newid()".

Asp.net won't send the value, and the field will get a new guid.

eglasius
Doing so results in this error: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_aspnet_Users_CustomInfo". The conflict occurred in database "CreamGlobal", table "dbo.CustomInfo", column 'ID'.The statement has been terminated.
dotnetdev
You probably had not a single insert or needed to set an existing one explicitely, my answer only gets you a new one all the time. Jon's answer is the one to go with for that.
eglasius