i have used, two tables, i m inserting in two tables at a time, and tables are rellated with foreign key:
protected void btn_submit_Click(object sender, EventArgs e)
{
cnn.ConnectionString = ConfigurationManager.ConnectionStrings["jin"].ConnectionString;
try
{
string path = @"~/images/";
if (FileUpload1.HasFile)
{
string path1 = Server.MapPath(path);
string fnm = FileUpload1.FileName;
string p = path1 + fnm;
FileUpload1.SaveAs(p);
string img = @"~/images" + fnm;
SqlCommand cm = new SqlCommand("menu", cnn);
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "str_collection";
cm.Parameters.Add("@itemname", txt_itemname.Text);
cm.Parameters.Add("@item_img", img);
SqlCommand cm1 = new SqlCommand("submenu", cnn);
cm1.CommandType = CommandType.StoredProcedure;
cm1.CommandText = "str_collection";
cm1.Parameters.Add("@item_descript", txt_descript.Text);
cm1.Parameters.Add("@item_rate",txt_rate.Text);
cm1.Parameters.Add("@subitem_name", txt_subitem.Text);
cnn.Open();
SqlDataReader dr = cm.ExecuteReader();
SqlDataReader dr1 = cm1.ExecuteReader();
cnn.Close();
Response.Write("<script>alert('Your Collection is Done Successfully!!')</script>");
}
}
catch (Exception ex)
{
lbl_msg.Text = ex.Message;
}
The Stored Proceedure is:
ALTER PROCEDURE [dbo].[str_collection]
@itemname nvarchar(50),
@item_img nvarchar(MAX)
AS
BEGIN
SET NOCOUNT ON
declare @subitem_name nvarchar(50)
declare @item_rate decimal(18,2)
declare @item_descript nvarchar(100)
declare @id varchar(50)
SET @id= COUNT(Getdate())
declare @item_id nvarchar(50)
SET @item_id=@itemname+left(convert(varchar,datepart(YYYY,GETDATE())),2)+@id
INSERT INTO menu(itemname,item_id,item_img)values(@itemname,@item_id,@item_img)
INSERT INTO submenu(item_id,item_descript,item_rate,subitem_name)values(@item_id,@item_descript,@item_rate,@subitem_name)
END
The Exception: I m getting is:
There is already an open DataReader associated with this Command which must be closed first.
Please make me, resolve this...