Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Here is my issue: First time really using Linq to SQL and I'm stuck on what to do with optional parameters in my stored procedures.

I have an insert stored procedure that has some of it's parameters that can be optional since the user will not have to supply that information if they don't have it. Here is what that looks like:
SQL
@CompanyName varchar(MAX),
	@ParentCompany varchar(MAX) = Null,
	@Description varchar(MAX),
	@EightACert bit,
	@SDBCert bit,
	@Minority bit,
	@Hubzone bit,
	@WBE bit,
	@DVBE bit,
	@MinoritySupplier bit,
	@Verified bit,
	/*Contact Table*/
	@WebSite varchar(100) = Null,
	@Email varchar(200) = Null,
	@Phone varchar(20),
	@Fax varchar(20) = Null,
	@KeyContact varchar(Max),
	@KeyContactTitle varchar(Max),
	@FedEmployerID varchar(10) = Null,
	@SSN varchar(4) = Null,
	/*AddressTable*/
	@PrimaryAddressLine1 varchar(max),
	@PrimaryAddressLine2 varchar(max) = Null,
	@PrimaryAddressCity varchar(100),
	@PrimaryAddressZip varchar(5),
	@HomeOffice bit,
	@PrimaryStateID varchar(2),
	@MailingAddressLine1 varchar(max) = Null,
	@MailingAddressLine2 varchar(max) = Null,
	@MailingAddressCity varchar(100) = Null,
	@MailingAddressStateID varchar(2) = Null,
	@MailingAddressZip varchar(5) = null,	
	/*Geographical Market*/
	@GeoMarket int


Now, when I go to use this stored proc in my code, I don't know how to send a dbnull.value if those optional fields do not have text in them. For example, before I would use something like the following:
C#
if (txtParentCompany.Text.Trim().Length == 0)
{
   command.Parameters.AddWithValue("@ParentCompany", DBNull.Value);
}
else
{
   command.Parameters.AddWithValue("@ParentCompany", txtParentCompany.Text.Trim());
}


How do I do that with Linq to SQL? Right now my code looks like this:
C#
var vendorID = (int)db.InsertVendor(
                            txtCompanyName.Text.Trim(),
                            txtParentCompany.Text.Trim(),
                            txtDescript.Text.Trim(),
                            chblCerts.Items[0].Selected,
                            chblCerts.Items[3].Selected,
                            chblCerts.Items[1].Selected,
                            chblCerts.Items[2].Selected,
                            chblCerts.Items[4].Selected,
                            chblCerts.Items[5].Selected,
                            chblCerts.Items[6].Selected,
                            false,
                            txtWebsite.Text.Trim(),
                            txtEmail.Text.Trim(),
                            txtPhone.Text.Trim(),
                            txtFax.Text.Trim(),
                            txtKeyContact.Text.Trim(),
                            txtKeyContactTitle.Text.Trim(),
                            txtFedNumber.Text.Trim(),
                            txtSSN.Text.Trim(),
                            txtPrimaryAddressLine1.Text.Trim(),
                            txtPrimaryAddressLine2.Text.Trim(),
                            txtPrimaryAddressCity.Text.Trim(),
                            txtPrimaryAddressZip.Text.Trim(),
                            chbHomeOffice.Checked ,
                            dropPrimaryAddressState.SelectedValue,
                            txtMailingAddressLine1.Text.Trim(),
                            txtMailingAddressLine2.Text.Trim(),
                            txtMailingAddressCity.Text.Trim(),
                            mailingAddressStateID,
                            txtMailingAddressZip.Text.Trim(),
                            Convert.ToInt32(dropGeoMarket.SelectedValue)
                        ).ReturnValue;
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900