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

I developed small post ad page here am writing code properly and post ad has been successfully but here columns:id,Image,description,dateadded,price,categories,categoriestype,categoriesname,state,city

these are column names once fill these columns, post successfully and here without select state and city also post ad successfully is coming

please reply me how i can write logic(validation) here

please reply me
Posted
Comments
Tom Marvolo Riddle 21-Mar-14 2:59am    
do you want validation for dropdownlist?
member1431 21-Mar-14 3:03am    
Dear jas,

here code please check and reply me am also given validation but is not working properly

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class postad : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string query = "select StateId, StateName from State";
BindDropDownList(ddlState, query, "StateName", "StateId", "Select State");
ddlCity.Enabled = false;
ddlCity.Items.Insert(0, new ListItem("Select City", "0"));
}
}
private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
{
string conString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
con.Open();
ddl.DataSource = cmd.ExecuteReader();
ddl.DataTextField = text;
ddl.DataValueField = value;
ddl.DataBind();
con.Close();
}
}
ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}
protected void btn_Post1_Click(object sender, EventArgs e)
{
string s = @"~\productImage\" + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath(s));
SqlConnection a = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
String cmd = "insert into Post(Image,Description,DateAdded,Price,Categories,CategoriesType,State,City,CategoriesName) values('" + s + "','" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + DropDownList1.Text + "','" + DropDownList2.Text + "','" + ddlState.SelectedItem.Text + "','" + ddlCity.SelectedItem.Text + "','" + DropDownList3.Text + "')";

SqlCommand b = new SqlCommand(cmd, a);

a.Open();
b.ExecuteNonQuery();
a.Close();

Label1.Text = "upload successfully";
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
ddlCity.Enabled = false;
ddlCity.Items.Clear();
ddlCity.Items.Insert(0, new ListItem("Select City", "0"));
int StateID = int.Parse(ddlState.SelectedItem.Value);
if (StateID > 0)
{
string query = string.Format("select CityId, CityName from Cities where StateId = {0}", StateID);
BindDropDownList(ddlCity, query, "CityName", "CityId", "Select cities");
ddlCity.Enabled = true;
}
}

protected void ddlState_SelectedIndexChanged1(object sender, EventArgs e)
{
ddlCity.Enabled = false;
ddlCity.Items.Clear();
ddlCity.Items.Insert(0, new ListItem("Select City", "0"));
int StateID = int.Parse(ddlState.SelectedItem.Value);
if (StateID > 0)
{
string query = string.Format("select CityId, CityName from cities where StateId = {0}", StateID);
BindDropDownList(ddlCity, query, "CityName", "CityId", "Select cities");
ddlCity.Enabled = true;
}
}
}
Tom Marvolo Riddle 21-Mar-14 3:06am    
will you please explain?.User must select the state and city when clicking submit button am i right?
member1431 21-Mar-14 3:23am    
Dear Jas,
please modify my code reply me please

am given that type of validations but output is not coming properly please reply me jas
Tom Marvolo Riddle 21-Mar-14 3:35am    
protected void btn_Post1_Click(object sender, EventArgs e)
{
if (ddlState.SelectedItem.Text != "Select State" && ddlCity.SelectedItem.Text != "Select City")
{
string s = @"~\productImage\" + FileUpload1.FileName; FileUpload1.PostedFile.SaveAs(Server.MapPath(s)); SqlConnection a = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString); String cmd = "insert into Post(Image,Description,DateAdded,Price,Categories,CategoriesType,State,City,CategoriesName) values('" + s + "','" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + DropDownList1.Text + "','" + DropDownList2.Text + "','" + ddlState.SelectedItem.Text + "','" + ddlCity.SelectedItem.Text + "','" + DropDownList3.Text + "')"; SqlCommand b = new SqlCommand(cmd, a); a.Open(); b.ExecuteNonQuery(); a.Close(); Label1.Text = "upload successfully";
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Select State/City')", true);
}
}
}

In .cs page:

IN submit button click event :
C#
  if (dropdownstate.SelectedItem.Text != "Select" && dropdowncity.SelectedItem.Text != "Select")
                {
//post add
}
else
{
//warning message
}
}


OR
In Javascript:
JavaScript
function Validate() {
      if (document.getElementById('<%= dropdownstate.ClientID%>').value == "Select") {
          alert("Please select the State");
          document.getElementById('<%= dropdownstate.ClientID%>').focus();
          return false;
      }
      if (document.getElementById('<%= dropdowncity.ClientID%>').value == "Select") {
          alert("Please select the City");
          document.getElementById('<%= dropdowncity.ClientID%>').focus();
          return false;
      }


In button:
ASP.NET
<asp:imagebutton id="btnsubmit" runat="server" onclientclick="return Validate();" xmlns:asp="#unknown" />
 
Share this answer
 
you can make validation in javascript like that
Put dropdown default value 0 and text "Select City"

JavaScript
  if(document.getElementById("ddlCity").value!="0")
  {

          //Your Code
  }
else
  {
        // Select Dropdown Value Validations
   }


At Code Behind You can do like that..

C#
{

  if(ddlCity.SelectedValue != "0")
    {
      //Your Code
     }
else
   {
  // Select Dropdown Value Validations
   }


}
 
Share this answer
 
Use validation group :

<asp:DropDownList runat="server" ID="ddl1" AutoPostBack="true" OnSelectedIndexChanged="dddl1_SelectedIndexChanged" ValidationGroup="Valddl" />


And hendle this validation group in your submit button :

<asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" ValidationGroup="Valddl" />
 
Share this answer
 
Hi
u use drop down list for state and city ? can u post ur insert query ? what u use for state and city in insert query ?

ddlcity.text for city
ddlstate.text for sate in insert query ?

if u use like above ,definitely it will save city and state,bcz it will get first text form dropdownlist.

so u have add dummy string(item) at first like "<---Select--->",i don't know how u bind items in dropdownlist,in design time or run time(dynamically) anyway u add one dummy string like Select,choose any thing.

And check drop down list before going to insert values like this in vb language


VB
If (ddlcity.Text <> "<---Select--->") Then

      If (ddlstate.Text <> "<---Select--->") Then

    ''Here writ insert query

      Else
     'Show message "choose state"
 
     EndIF

Else
   'Show message "choose city"
 
EndIF
 
Share this answer
 
v2
Comments
member1431 21-Mar-14 3:33am    
Dear Aravindba,
your answer right but here ddlcity.text and ddlstate.text once use is not select perticular state and cities

please reply me any logic or url paths
Aravindba 21-Mar-14 3:37am    
pls post ur insert query,how u get item from dropdownlist in insert query.
if u use ddlcity.Text it will work.i mean if u select particular state or city it will return in ddlcity.Text or ddlstate.Text ,no need to use Selecteditem or selectedindex
member1431 21-Mar-14 3:39am    
ya sure Aravind currently am going to lunch just give me some time i will gill give reply please dont mind

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