Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

please check below code and in this code without and with image is coming output properly but here select state and city validation is not coming properly please reply me

C#
protected void btnPostAd_Click(object sender, EventArgs e)
    {
        if (ddlState.SelectedItem.Text != "Select State" && ddlCity.SelectedItem.Text != "Select City" || FileUpload1.PostedFile.ContentLength != 0)
        {
            string strimage = @"~\ProductImage\" + FileUpload1.FileName;
            FileUpload1.PostedFile.SaveAs(Server.MapPath(strimage));
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCONNECTION"].ConnectionString);
            String strInsert = "insert into ProductDetailsTable(Categories,CategoriesType,Title,Description,Price,State,City,ContactName,ContactEmail,ContactMobile,ContactPhoto,ContactViaEmail,Date)values('" + lblEducation.Text + "','" + ddlCategoryType.Text + "','" + txtTitle.Text + "','" + txtDescription.Text + "','" + txtPrice.Text + "','" + ddlState.SelectedItem.Text + "','" + ddlCity.SelectedItem.Text + "','" + txtContactName.Text + "','" + txtContactEmail.Text + "','" + txtContactMobile.Text + "','" + strimage + "','" + txtContactedViaEmail.Text + "','" + lblDateTime.Text + "')";
            SqlCommand cmd = new SqlCommand(strInsert, con);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                Thread.Sleep(3000);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Your Add has been Posted Successfully')", true);                
            }
            catch (SqlException ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Select State/City')", true);
                string errorMessage = "Error in entering competition";
                errorMessage += ex.Message;
                throw new Exception(errorMessage);
            }
            finally
            {
                con.Close();
            }
            Clear();
        }
        else
        {
            string strimage = "NoImage.jpg";
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCONNECTION"].ConnectionString);
            String strInsert = "insert into ProductDetailsTable(Categories,CategoriesType,Title,Description,Price,State,City,ContactName,ContactEmail,ContactMobile,ContactPhoto,ContactViaEmail,Date)values('" + lblEducation.Text + "','" + ddlCategoryType.Text + "','" + txtTitle.Text + "','" + txtDescription.Text + "','" + txtPrice.Text + "','" + ddlState.SelectedItem.Text + "','" + ddlCity.SelectedItem.Text + "','" + txtContactName.Text + "','" + txtContactEmail.Text + "','" + txtContactMobile.Text + "','" + strimage + "','" + txtContactedViaEmail.Text + "','" + lblDateTime.Text + "')";
            SqlCommand cmd = new SqlCommand(strInsert, con);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                Thread.Sleep(3000);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Your Add has been Posted Successfully')", true);                
            }
            catch (SqlException ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Select State/City')", true);
                string errorMessage = "Error in entering competition";
                errorMessage += ex.Message;
                throw new Exception(errorMessage);
            }
            finally
            {
                con.Close();
            }
            Clear();
        }
    }
Posted
Updated 23-Apr-14 20:11pm
v2
Comments
Tom Marvolo Riddle 24-Apr-14 2:12am    
Try this:

if (ddlState.SelectedItem.Text != "Select State" && ddlCity.SelectedItem.Text != "Select City" && FileUpload1.PostedFile.ContentLength != 0)
member1431 24-Apr-14 2:14am    
Dear Jas,

this is also same validation is working
Tom Marvolo Riddle 24-Apr-14 2:15am    
Explain the question clearly.What's the issue?what do you want to achieve?
member1431 24-Apr-14 2:27am    
Q:In the above code select state or city validation is not working

and once not select state or city here i want validation message like: ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Select State/City')", true);
member1431 24-Apr-14 2:44am    
Dear Jas,
i got output thank you jas thanks for your reply

1 solution

Try this:
C#
protected void btnPostAd_Click(object sender, EventArgs e)
{
    if (ddlState.SelectedItem.Text != "Select State" && ddlCity.SelectedItem.Text != "Select City")
    {
        if (FileUpload1.PostedFile.ContentLength != 0)
        {

            string strimage = @"~\ProductImage\" + FileUpload1.FileName;
            FileUpload1.PostedFile.SaveAs(Server.MapPath(strimage));
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCONNECTION"].ConnectionString);
            String strInsert = "insert into ProductDetailsTable(Categories,CategoriesType,Title,Description,Price,State,City,ContactName,ContactEmail,ContactMobile,ContactPhoto,ContactViaEmail,Date)values('" + lblEducation.Text + "','" + ddlCategoryType.Text + "','" + txtTitle.Text + "','" + txtDescription.Text + "','" + txtPrice.Text + "','" + ddlState.SelectedItem.Text + "','" + ddlCity.SelectedItem.Text + "','" + txtContactName.Text + "','" + txtContactEmail.Text + "','" + txtContactMobile.Text + "','" + strimage + "','" + txtContactedViaEmail.Text + "','" + lblDateTime.Text + "')";
            SqlCommand cmd = new SqlCommand(strInsert, con);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                Thread.Sleep(3000);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Your Add has been Posted Successfully')", true);
            }
            catch (SqlException ex)
            {
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Select State/City')", true);
                //string errorMessage = "Error in entering competition";
                //errorMessage += ex.Message;
                //throw new Exception(errorMessage);
            }
            finally
            {
                con.Close();
            }
            Clear();
        }

        else
        {
            string strimage = "NoImage.jpg";
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCONNECTION"].ConnectionString);
            String strInsert = "insert into ProductDetailsTable(Categories,CategoriesType,Title,Description,Price,State,City,ContactName,ContactEmail,ContactMobile,ContactPhoto,ContactViaEmail,Date)values('" + lblEducation.Text + "','" + ddlCategoryType.Text + "','" + txtTitle.Text + "','" + txtDescription.Text + "','" + txtPrice.Text + "','" + ddlState.SelectedItem.Text + "','" + ddlCity.SelectedItem.Text + "','" + txtContactName.Text + "','" + txtContactEmail.Text + "','" + txtContactMobile.Text + "','" + strimage + "','" + txtContactedViaEmail.Text + "','" + lblDateTime.Text + "')";
            SqlCommand cmd = new SqlCommand(strInsert, con);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                Thread.Sleep(3000);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Your Add has been Posted Successfully')", true);
            }
            catch (SqlException ex)
            {
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Select State/City')", true);
                //string errorMessage = "Error in entering competition";
                //errorMessage += ex.Message;
                //throw new Exception(errorMessage);
            }
            finally
            {
                con.Close();
            }
            Clear();
        }
    }
    else
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Select State/City')", true);
    }
}



Do not concatenate stringsin query.It leads to Sql Injection[^]
 
Share this answer
 
v2

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