C# .NET - I want to textbox value ID (when we enter city in textbox we want to ID of that city)

Asked By ALOK RANJAN on 25-Jun-12 01:34 AM
hi every one,

                     I have three textbox first one for city second one for type and last for Area. when we enter city in first textbox (ex: Delhi) Now I want to pick up ID of delhi .also area should be bind according to this ID .and last when we write anything in type textbox that record should be match only particular city.




Thanks & Best Record
ALOK RANJAN


TSN ... replied to ALOK RANJAN on 25-Jun-12 01:45 AM
hi..

its better to use the dropdown list for this sort of problems.
Chintan Vaghela replied to ALOK RANJAN on 25-Jun-12 01:48 AM

Hi Frndz,

 

Functionality:  Find Area and Type based on City Name

 

To achieve this task,

On textbox City , Textchange Event call following query

Select AreaID,AreaName from AreaTable where CityID in

(SElect CityID from CityTable where CityName = 'txtCityName') and AreaName = 'txtAreaName'

Now this row return value it means there is Area Exit for this City....

Same way check for Type

Make your select query as following way

Logic   :

Select AreaID,AreaName from AreaTable where CityID in

(SElect CityID from CityTable where CityName = 'txtCityName') and AreaName = 'txtAreaName'

Select TypeID,TypeName from TypeTable where CityID in

(SElect CityID from CityTable where CityName = 'Delhi') and TypeName = 'txtTypeName'

Hope this helpful!

Thanks

Jitendra Faye replied to ALOK RANJAN on 25-Jun-12 08:48 AM
It is better to implement Cascading DropDownList for this functionality.

If you want to display data based on DropDownList selection, means if you want to implement cascading then you have to implement code in  SelectedIndexChanged() Event of DropDownList.

before that set AutoPostBack Property of All DropDownList ti true.

I m giving sample code for that-
Here i m doing cascading for country, state and city.

1. here i m filling DropDownList1 with county


//for binding DropDownList1 with country


private void DataGrid_Load(object sender, EventArgs e)
{
 getdata();
}

//function for getting country
private void getdata()
{
SqlConnection cn = new SqlConnection("constring");
cn.Open();
string strQuery = "Select conid,conname from countrytab";
SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DisplayMember = "conid";
DropDownList1.ValueMember = "conname";
DropDownList1.DataSource = ds.Tables[0];
}


2. here i m filling DropDownList2 with state, based on selection on DropDownList1

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("constring");
cn.Open();
string strQuery = "Select stateid,statename from statetab where conid='" + DropDownList1.SelectedValue .ToString() + "'";
SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList2 .DisplayMember = "stateid";
DropDownList2.ValueMember = "statename";
DropDownList2.DataSource = ds.Tables[0];
}



3. here i m filling DropDownList3 with city, based on selection on DropDownList2


 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("constring");
cn.Open();
string strQuery = "Select cityid,cityname from citytab where conid='" + DropDownList1.SelectedValue .ToString() + "' and stateid='" + DropDownList2.SelectedValue .ToString() + "' ";
SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList3.DisplayMember = "cityid";
DropDownList3.ValueMember = "cityname";
DropDownList3.DataSource = ds.Tables[0];
}

USE THIS CODE AND LET ME KNOW.