C# .NET - how to link combo box with text boxes

Asked By Priyanka
02-Sep-10 01:48 PM

suppose i have one combo box and two text boxes which are linked with access database and i want that whenever user selct student id from combo box then the name of the student and age should be filled in text boxes...
plz give me some simple example with code,\\.....
  Reena Jain replied to Priyanka
03-Sep-10 02:58 AM
Hi,

use the following code

protected void Combobox1_SelectedIndexChanged(object sender, EventArgs e)
    {
//to send the your combox id in database to fatch name and other detail

 SqlCommand cmdd = new SqlCommand("Select * table where studentid='" + Combobox1.selectedvalue + "'", LiveCon);
                          SqlDataAdapter adpp = new SqlDataAdapter(cmdd);
                          DataTable dtr = new DataTable();
                          adpp.Fill(dtr);
                          if (dtr.Rows.Count > 0)
                          {
                           textbox1.Text=dt.rows[0]["Name"].tostring();
                          }
}

hope this will help
Create New Account
help
LIKE '"+ combobox1.text +"' here is how you can do that protected void Button1_Click( object sender, EventArgs e) { SqlConnection con = new SqlConnection ( "Connection String" ); con.Open(); SqlCommand comm = new SqlCommand ( "select * from Table where employee name = '" + comboBox1.Text+ "'" , con); SqlDataAdapter da = new SqlDataAdapter (comm); DataTable dt = new DataTable (); da.Fill(dt); con.Close(); if (dt.Rows.Count > 0) { / * Employee Exisit do somthing * / } else First search the empname from the table using this code : private void button1_Click( object sender, EventArgs e) { / / Query string q = "select * from table1 where empname = " + textBox1.Text; SqlDataAdapter da = new SqlDataAdapter
list from the database. Protected Sub country_SelectedIndexChanged(ByVal sender As Object , ByVal e As System.EventArgs) Handles country.SelectedIndexChanged Dim myreader As Odbc.OdbcDataReader Dim ConnStr As String = " Driver = {MySQL ODBC 3.51 Driver selectedIndex of State For this we need to write code in code behind in the SelectedIndexChanged Event of respective Dropdown to implement the cascading of DropDowns The html source of the div > < / form > < / body > < / html > The Code Behind is as follows protected void Page_Load( object sender, EventArgs e) { if (!IsPostBack) { FillCountry(); } } protected void ddlCountry_SelectedIndexChanged( object sender, EventArgs e) { int CountryID = Convert.ToInt32(ddlCountry.SelectedValue.ToString()); FillStates(CountryID); } protected void ddlState_SelectedIndexChanged( object sender, EventArgs e) { int StateID = Convert.ToInt32(ddlState.SelectedValue.ToString()); FillCities(StateID); } private void FillCountry() { string strConn ConfigurationManager.ConnectionStrings[ "DatabaseConnectionString" ].ConnectionString; SqlConnection con = new SqlConnection(strConn); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = "Select CountryID, Country from Country" ; DataSet
SqlConnection("Data Source = 10.0.2.8;Initial Catalog = JitendraDB;User ID = sa;password = change_123"); SqlDataAdapter da; string mySQL = "SELECT empid, empname, dept FROM emp "; da = new SqlDataAdapter(mySQL, con); con.Open(); DataSet ds = new DataSet(); da.Fill(ds); GridView2.DataSource = ds; GridView2 deleteQuery = "delete from emp where empid = ' " + strEmpId + " ' "; SqlConnection cn = new SqlConnection("CONNECTION STRING"); cn.Open(); SqlCommand cmd = new SqlCommand(deleteQuery, cn); cmd.ExecuteNonQuery(); cn.Close(); GridView1.EditIndex = -1; Response.Write("<script> alert('Record Deleted empname = '" + strEmpName + "' , dep = '" + strDeptId + "' where empid = ' " + strEmpId + " ' "; SqlConnection cn = new SqlConnection("CONNECTION STRING"); cn.Open(); SqlCommand cmd = new SqlCommand(UpdateQuery, cn); cmd.ExecuteNonQuery(); cn.Close(); GridView1.EditIndex = -1; getdata(); Response.Write("<script> alert('Record will be used below inside the code. String fn; String path; SqlConnection cnn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); SqlDataAdapter adp; DataTable dt; Int32 id; / / id as a integer variable it will be used
Error str1 = "Select Rights_Tbl_PK from Tbl_Rights where Rights_Desc = '" + ddlRights.SelectedItem .ToString () + "' "; cmd1 = new SqlCommand(str1, conn); dr1 = cmd1.ExecuteReader(); Use this code to retrieve Rights_Tbl_PK , but it getting - -Select- -, instead of dropdownlist selected value. Dropdownlist values binded: SqlCommand cmd1 = new SqlCommand("select Rights_Tbl_PK, Rights_Desc as Rights from tbl_rights", conn SqlDataAdapter da = new SqlDataAdapter(cmd1); DataTable main = new DataTable(); da.Fill(main); ddlRights.DataSource = main; ddlRights.DataTextField = "Rights"; ddlRights.DataValueField = "Rights_Tbl_PK"; ddlRights.DataBind(); ddlRights DropDownList1.Items.Clear(); string ConnString = ConfigurationManager .ConnectionStrings[ "ConnectionString" ].ConnectionString; string query = "select name from people" ; SqlDataAdapter da = new SqlDataAdapter (query, ConnString); DataTable dt = new DataTable (); da.Fill(dt); / / load records DropDownList1
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 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 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 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