Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
 
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 :
 
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 7-Jan-13 18:32pm
Edited 7-Jan-13 18:37pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,609
1 Sergey Alexandrovich Kryukov 6,240
2 CPallini 5,180
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100