Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi.
I have designed a webform wiht asp.net4 which is for Employees.in one of my page there is Checkbox and also a Textbox.so if the user checked that checkbox ,it means he has decided (to show i have studied inside the organization), and also in the Database i have mada it boolian (0,1), in otherwise if he didn't check the Checkbox the Textbox to be available for him and can write inside , and also some Nvarchar(he has studied some where else) will be inserted in the database.

a part of the code is in below:

C#
switch (IsAlmostafaCheckBox.Checked)
            {
                case true:
                command.Parameters.AddWithValue("@IsInOrganization", this.IsOrganizationCheckBox.Checked);
                command.Parameters.AddWithValue("@InstituteName", DBNull.Value);
                    break;
                case false:
                    command.Parameters.AddWithValue("@InstituteName", this.InstituteNameTextBox.Text);
                    command.Parameters.AddWithValue("@IsInOrganization", this.IsOrganizationCheckBox.Checked);
                    break;
            }


but the problem is that when i press the button an error occures and says that you have omitted the IsInOrganization parameter in your Stored Procedure , and can not accept the null value in the database

so because i didn't want to make the field Nullable, i tried to change the stored procedure , like this :

SQL
ALTER PROCEDURE [dbo].[Insert_GeneralPassedTerms_SP](
@CityId int,
@IsInOrganization bit,
@TermTitle nvarchar(1000),
@InstituteName nvarchar(500),
@Date Datetime ,
@Duration decimal(4,1),
@Score decimal(4,2),
@Description nvarchar(1000)
)
AS
begin 
if @InstituteName=null
begin
insert into GeneralPassedTerms(
											CityId,
											IsInOrganization,
											TermTitle,
											InstituteName,
											Date,
											Duration,
											Score,
											Description
)
	Values(
											@CityId,
											@IsInOrganization,
											@TermTitle,
											'Null',
											@Date,
											@Duration,
											@Score,
											@Description
)
End
 else
     Insert into GeneralPassedTerms(
											CityId,
											IsInOrganization,
											TermTitle,
											InstituteName,
											Date,
											Duration,
											Score,
											Description
)
   Values(
											@CityId,
											@IsInOrganization,
											@TermTitle,
											@InstituteName,
											@Date,
											@Duration,
											@Score,
											@Description
)	
  select @@IDENTITY			
end


I don't whether this procedure is correct or not ? would you please help me?
Posted
Updated 7-Jan-13 18:37pm
v2

The error means what it says. If your proc provides default values for parameters, those parameters do not need to be set. If you want the default to be null, put = null in the parameter in the proc, or add it to the collection and set it to null. It doesn't magically appear as null, if you did not specify it
 
Share this answer
 
Try this code hope your query will get resolved. You do not required if else condition. It can be achieve using below code...

ALTER PROCEDURE [dbo].[Insert_GeneralPassedTerms_SP](
@CityId int,
@IsInOrganization bit,
@TermTitle nvarchar(1000),
@InstituteName nvarchar(500),
@Date Datetime ,
@Duration decimal(4,1),
@Score decimal(4,2),
@Description nvarchar(1000)
)
AS
begin 
	insert into GeneralPassedTerms
			(
				CityId,
				IsInOrganization,
				TermTitle,
				InstituteName,
				Date,
				Duration,
				Score,
				Description
			)
	Values
		(
				@CityId,
				@IsInOrganization,
				@TermTitle,
				isnull(@InstituteName,'Null',@InstituteName),
				@Date,
				@Duration,
				@Score,
				@Description
		) 
  select @@IDENTITY			
end
 
Share this answer
 

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