Link to home
Start Free TrialLog in
Avatar of forsters
forstersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

C# SQL Parameters.AddWithValue(); - how to include a default value

hi experts,

I have a sql insert in my code behind that includes a number of parameters e.g.

cmd.Parameters.AddWithValue("@matterNumber", DDLMatterNo.SelectedValue);

I want additionally to set the default value to 0 on a couple of the parameters where the controls are only visible conditionally.
In a previous version of the page this was possible because the sql was written as a datasource:
<asp:ControlParameter ControlID="DDLMatterNo" PropertyName="selectedvalue" Name="matterNumber" DefaultValue="0" />

But in the code behind the method only accepts 2 arguments so the below attempt gets a syntax error - how to overcome this?
cmd.Parameters.AddWithValue("@matterNumber", DDLMatterNo.SelectedValue, Convert.ToInt32(0));
SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of forsters

ASKER

hi thanks for the code, I had to remove the Convert.ToInt32 to get it to work but having done so it just inserts a null value instead of the required zero....
actually on closer inspection it doesn't even insert a null - just an empty space
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok glad that I could make some helps here cheers
I have selected my comment for the benefit of others trying to resolve the same, but all credit goes to Ryan for lighting the way, thank you.