HI
I have insertP.aspx page that users can enter their product information on it and
I wrote SP that if users have behcode='free' they can just enter 2 product in database if they want enter morethan 2 product it show error below is my SP and Code
USE [behtop]
GO
/****** Object: StoredProcedure [dbo].[insertproduct4] Script Date: 04/10/2013 17:04:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[insertproduct4]
@Name nvarchar(30),
@Model nvarchar(30)
,@Behcode nvarchar(10)
,@price nvarchar(30)
,@Classification nvarchar(30)
,@Description nvarchar(max)
,@subset nvarchar(30)
,@id int =0
,@Result NVARCHAR(90) OUTPUT
AS BEGIN
set NOCOUNT ON;
Declare @H_name nvarchar(30) set @H_name =(select H_name from House_info where behcode=@Behcode )
Declare @BehcodeN nvarchar(30) set @BehcodeN =(select BehcodeN from House_info where behcode=@Behcode )
IF @id > 0 AND EXISTS(SELECT behcode FROM House_p WHERE id = @id AND behcode = @Behcode)
BEGIN
UPDATE House_p
SET Name=@Name
,Model=@Model
,Description=@Description
,Price=@price
,Classification=@Classification
,subset=@subset
,Date=GETDATE()
,H_name=@H_name
,BehcodeN=@BehcodeN
WHERE BehCode=@Behcode and ID=@id
IF NOT EXISTS(SELECT Subset FROM Subset_menu WHERE Subset = @subset)
begin
insert into Subset_menu(H_name,Subset)
Values(@H_name,@subset)
end
SELECT @id
END
ELSE
BEGIN
IF (SELECT COUNT(behcode) FROM House_p WHERE behcode = @behcode and BehcodeN='free')< 2
Begin
INSERT INTO House_p(
Name,Model,Price,Classification,subset,[Date],H_name,BehCode,search,BehcodeN)
VALUES(
@Name,@Model,@price,@Classification ,@subset ,GETDATE(),@H_name,@Behcode,@Name+' '+@Model,@BehcodeN)
set @Result='your product insert '
end
ELSE
BEGIN
SET @Result = 'you can just enter 2 product'
END
IF NOT EXISTS(SELECT Subset FROM Subset_menu WHERE Subset = @subset)
begin
insert into Subset_menu(H_name,Subset)
Values(@H_name,@subset)
end
SELECT CAST(SCOPE_IDENTITY() AS INT)
END
END
Behind Code
protected void ImageButton2_Click1(object sender, ImageClickEventArgs e)
{
string data = Server.UrlDecode(Request.QueryString["BehCode"]);
string price = RadioButton2.Checked ? TextBox1.Text : "null";
SqlCommand _cmd = new SqlCommand("insertproduct4", _cn);
_cmd.CommandType = CommandType.StoredProcedure;
_cn.Open();
_cmd.Parameters.AddWithValue("@Name", txtname.Text);
_cmd.Parameters.AddWithValue("@Model", txtmodel.Text);
_cmd.Parameters.AddWithValue("@Description", CKEditorControl1.Text);
_cmd.Parameters.AddWithValue("@Price", price);
_cmd.Parameters.AddWithValue("@Classification", DDL1.SelectedItem.Text);
_cmd.Parameters.AddWithValue("@subset", DDL2.SelectedItem.Text);
_cmd.Parameters.AddWithValue("@behcode", data);
_cmd.Parameters.AddWithValue("@id", Convert.ToInt32(ViewState["Id"]));
_cmd.Parameters.Add("@Result", SqlDbType.NVarChar, 90);
_cmd.Parameters["@Result"].Direction = ParameterDirection.Output;
int ID = Convert.ToInt32(_cmd.ExecuteScalar());
if (ID > 0)
{
ViewState["Id"] = ID.ToString();
}
_cn.Close();
lblerrorV.Text = _cmd.Parameters["@Result"].Value.ToString();
}
here when I enter data in textboxs and click on button it insert data in database and show below text in lable
'your product insert '
i define above text in SP
untill now every thing is ok and it worked correctly but when I insert morethan 2 product I Expect that it show
'you can just enter 2 product'
and doesn't insert data in database but here when I enter data for third time it make below error
Server Error in '/behtop website' Application.
Object cannot be cast from DBNull to other types.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Object cannot be cast from DBNull to other types.
Source Error:
Line 815: _cmd.Parameters.Add("@Result", SqlDbType.NVarChar, 90);
Line 816: _cmd.Parameters["@Result"].Direction = ParameterDirection.Output;
Line 817: int ID = Convert.ToInt32(_cmd.ExecuteScalar()); Line 818:
Line 819: if (ID > 0)
|
why this happen?
thanks again