HI
I have 2 table in data base
1-HOuse_info Table
2-House_p Table
in House_p table save users Product information and in House_info table save Users Information
In both Tables is column that name is BehcodeN
I want if users in BehcodeN save this text='free' 'they can't insert product morethan 2 number and if they want insert their third product It show error that You can't insert morethan 2 product below are my code
In Product.aspx page I have button that when users click on it they can insert their product
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("insertproduct3", _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"]));
int ID = Convert.ToInt32(_cmd.ExecuteScalar());
if (ID > 0)
{
ViewState["Id"] = ID.ToString();
}
Session["Message"] = true;
Response.Redirect(Request.Url.AbsoluteUri);
_cn.Close();
lblerrorV.Text="you just can insert 2 product";
}
and SP
USE [behtop]
GO
/****** Object: StoredProcedure [dbo].[insertproduct3] Script Date: 03/14/2013 17:42:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[insertproduct3]
@Name nvarchar(30),
@Model nvarchar(30)
,@Behcode nvarchar(10)
,@price nvarchar(30)
,@Classification nvarchar(30)
,@Description nvarchar(max)
,@subset nvarchar(30)
,@id int =0
AS BEGIN
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)
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)
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 SCOPE_IDENTITY()
END
END
ok here I can insert 2 product succesfully
but when I want insert third produt it makes below error instead of showing error message
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 805: _cmd.Parameters.AddWithValue("@behcode", data);
Line 806: _cmd.Parameters.AddWithValue("@id", Convert.ToInt32(ViewState["Id"]));
Line 807: int ID = Convert.ToInt32(_cmd.ExecuteScalar()); Line 808: if (ID > 0)
Line 809: {
|
why this happen?
Thanks alot