Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
try
        {
            if (HttpContext.Current.Request.Cookies["CRegistrationID"].Value != "")
            {
            if (FUImages.HasFile && FUBrochure.HasFile)
            {


                string CateName = ddlCreateAddCategory.SelectedItem.ToString();
                string ArName = ddlCreateAddArea.SelectedItem.ToString();

                Con.Open();

                string uid = HttpContext.Current.Request.Cookies["CRegistrationID"].Value;

                HttpContext.Current.Response.Cookies["Image"].Value = CateName;
                HttpContext.Current.Response.Cookies["Upload"].Value = ArName;

                ImageAutoNum = Guid.NewGuid().ToString();
                ImageAutoNum = ImageAutoNum.Substring(0, 3);

                FUImages.SaveAs(Server.MapPath("~/ConsumerImages/" + ImageAutoNum + FUImages.FileName));
                ImageUrl = "~/ConsumerImages/" + ImageAutoNum + FUImages.FileName;

                ImageAutoNum2 = Guid.NewGuid().ToString();
                ImageAutoNum2 = ImageAutoNum2.Substring(0, 3);

                FUBrochure.SaveAs(Server.MapPath("~/ConsumerImages/" + ImageAutoNum2 + FUBrochure.FileName));
                FUBImages = "~/ConsumerImages/" + ImageAutoNum2 + FUBrochure.FileName;

                Cmd = new SqlCommand("Insert into ConsumerAdvertiesment(CategoryName,ADshop_name,ADShop_Address,ADshop_city,ADShop_mob,ADshop_Email,RegistrationId,startddate,endddate,ImageName,Description,ImagePath,BImageName,BImagePath,Status) values('" + CateName + "','" + txtshopname.Text + "','" + txtCadress.Text + "','" + txtCity.Text + "','" + txtcontact.Text + "','" + txtemail.Text + "','" + uid + "','" + txtstart.Text + "','" + txtEnd.Text + "','" + ImageUrl + "','" + txtDesc.Text + "','" + ImageUrl + "','" + FUBImages + "','" + FUBImages + "','Active')", Con);
                Cmd.ExecuteNonQuery();

                Response.Redirect("Success.aspx?Pop=Adv");
                Con.Close();

            }
            else
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Please Select Image & Brochure');", true);
            }
            }
            else
            {
                Response.Cookies["LoginRedirect"].Value = "Yes";
                ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Please Log in First !!! ');", true);
            }
        }
        catch (Exception ex)
        { }
        finally
        {
            Con.Close();
        }
Posted
Comments
On which line?

Hi,
if you get that problem you should close the existing open connection, before opening connection

before the line
con.Open();

place the code
if(con.ConnectionState == ConnectionState.Open)
{
Con.close();
}

it should solve your problem,
 
Share this answer
 
Comments
Logic is correct, but code is not... See my answer. :)
C#
if (con.State == ConnectionState.Open)
{
    Con.Close();
}
 
Share this answer
 
Comments
sameer549 5-Jul-14 4:37am    
yes you are rite. actually mine is a typing mistake :)
Don't Share the Connection Object. you can keep connection string on app config or web config.
When you need to do a database operation, create new connection and dispose it after you done with it. following code sample will help you.
C#
using (SqlConnection conn = new SqlConnection(YourConnectionString))
using (SqlCommand cmd = new SqlCommand(SqlString, conn))
{
    conn.Open();
    cmd.ExecuteNonQuery();
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900