Hi
I have insertp.aspx page there are textboxs ,DropDownlist and Fileupload control and buttons
in this page I define button that when click on it ,it insert image(that users select with fileupload) in data base
below is code
rotected void BtnUpload2_Click(object sender, EventArgs e)
{
string path = Server.MapPath(".") + "\\../image/House/product\\";
fup3.PostedFile.SaveAs(path + filename);
SqlCommand _cmd = new SqlCommand("fileupload13", _cn);
_cmd.CommandType = CommandType.StoredProcedure;
string data = Server.UrlDecode(Request.QueryString["BehCode"]);
_cn.Open();
_cmd.Parameters.AddWithValue("@image2", filename);
_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();
}
_cn.Close();
SqlCommand _cmd1 = new SqlCommand("selectFUP1", _cn);
_cmd1.CommandType = CommandType.StoredProcedure;
_cn.Open();
_cmd1.Parameters.AddWithValue("@Behcode", data);
SqlDataReader _dr = _cmd1.ExecuteReader();
while (_dr.Read())
{
lblimg2.Visible = false;
Upbdt2.Visible = false;
Button3.Visible = true;
ThumbPic2.Visible = true;
ThumbPic2.ImageUrl = "~/image/house/product/" + _dr["Image2"].ToString();
}
_cn.Close();
}
and SP
USE [behtop]
GO
/****** Object: StoredProcedure [dbo].[Fileupload13] Script Date: 04/10/2013 22:19:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[Fileupload13]
@image2 nvarchar(MAX),
@behcode nvarchar(10),
@id int = 0
AS
BEGIN
IF @id > 0 AND EXISTS(SELECT behcode FROM House_p WHERE iD = @id)
BEGIN
UPDATE House_p
SET Image2 =@image2,
behcode = @behcode,
Date=GETDATE()
WHERE iD = @id
SELECT @id
END
ELSE
BEGIN
insert into House_p (Image2,behcode,Date)values (@image2,@behcode,GETDATE())
SELECT SCOPE_IDENTITY()
END
END
USE [behtop]
GO
/****** Object: StoredProcedure [dbo].[selectFUP1] Script Date: 04/10/2013 22:19:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[selectFUP1]
@Behcode nvarchar(10)
as
begin
select image,image1,image2,Date from House_p
where behcode=@Behcode
end
and there is ImageButton2 that when click on it, it insert data in data base in sp I define that if user's behcode='free' they can just insert 2 product in table if they enter data for third time it make error
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;
object o = _cmd.ExecuteScalar();
int ID = (o != null && o != DBNull.Value) ? Convert.ToInt32(o) : 0;
if (ID > 0)
{
ViewState["Id"] = ID.ToString();
}
_cn.Close();
lblerrorV.Text = _cmd.Parameters["@Result"].Value.ToString();
}
and SP
USE [behtop]
GO
/****** Object: StoredProcedure [dbo].[insertproduct4] Script Date: 04/10/2013 22:36:07 ******/
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 @Result INT
--SET @Result = 1
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
--SELECT @Result
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
My problem is:
when I enter data in TB and select item from DDL and enter imagebutton2 it insert data in table and SP worked correctly I mean if I insert data for third time it make error that 'you can just enter 2 product'
untill now every thing is ok
but when I select image from fileupload and click on BtnUpload2 button it doesn't worked
I mean if I enter data for third time if I select image and click on BtnUpload2 it insert data in database and doesn't show this text ''you can just enter 2 product' I mean it insert data
why this happen?
Best regards