Click here to Skip to main content
15,918,211 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
at first load combobox data from sql database. the load textbox another field the combobox releted data.

please any one solve the problem any with aproject
Posted

In desktop application, just use ComboBox1_SelectedIndexChanged method.

[Edit]Shouting removed[/Edit]
 
Share this answer
 
v2
first on the form load event
C#
private void Form1_Load(object sender, EventArgs e)
       {
           //Fill your combo box
       }

after this on the Combobox_SelectedIndexChange event
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
         String str="select YourFieldName from YourTableName where FieldName='" + Combobox1.Text + "'";//your select query
        //after getting record assign value to your textbox
         //Textbox1.Text=datareader[0];
       }
 
Share this answer
 
Hello friend

as per your question's need

you have to do three things as below.....


1st just make a method that load data from DB to comb box.

private void GetColumnOnComboBox()
       {
           try
           {
              SqlConnection ObjConnection = new SqlConnection();
               ObjConnection.ConnectionString = ConfigurationSettings.AppSettings["constring"];
               ObjConnection.Open();
               string str = "select ColumnName from TableName";
               SqlDataAdapter da = new SqlDataAdapter(str, ObjConnection);
               DataSet ds = new DataSet();
               da.Fill(ds, "YourTableName");
               this.comboBox1.DataSource = ds.Tables["ProductMaster"];
               this.comboBox1.DisplayMember = "ColumnName";//column that you want to show on combo Box
               this.comboBox1.ValueMember = "ColumnName";//same ColumnName Here

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
           finally { ObjConnection.Close(); }
       }


2nd now pass this method on form load event................

C#
private void form1_Load(object sender, EventArgs e)
{
GetColumnOnComboBox();
}


3rd on comboBox SelectedIndexChanged event you have to fill text box from database with where clause of selected text of Combo Box.....like

C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
         SqlConnection ObjConnection = new SqlConnection();
            ObjConnection.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["constring"];
            ObjConnection.Open();
            string str = "select * from YourTableName where columnName='"+ComboBox.text+"'";
            SqlCommand cmd = new SqlCommand(str, ObjConnection);
            SqlDataReader dr=cmd.ExecuteReader();
            if(dr.Read())
            {
            textbox1.Text=dr["ColumnName"].ToString());//column name should be that you want to show on textbox
           
            }
       }




Its Simple but if any problem feel free to ask.....

Happy to help
 
Share this answer
 
Comments
Nareen Nair 25-Feb-16 3:36am    
not working
Nareen Nair 25-Feb-16 3:38am    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace ComboboxAndTextboxDatabase
{
public partial class Form1 : Form
{
OleDbConnection con = new OleDbConnection("Provider=MSDAORA;Data Source=orcl;User ID=nareen1093;Password=nareen1093;Unicode=True");

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
con.Open();
OleDbDataAdapter oda1 = new OleDbDataAdapter("select subject_code from subjectinfo", con);
DataTable dt1 = new DataTable();
oda1.Fill(dt1);
comboBoxSubjectCodeUpdate.DataSource = dt1;
comboBoxSubjectCodeUpdate.DisplayMember = "subject_code";
comboBoxSubjectCodeUpdate.SelectedIndex = -1;
con.Close();

}

private void textBox1_TextChanged(object sender, EventArgs e)
{




}

private void comboBoxSubjectCodeUpdate_SelectedIndexChanged(object sender, EventArgs e)
{
con.Open();
string str = "select subject_abbreviation from subjectinfo where subject_code ='" + comboBoxSubjectCodeUpdate.Text + "'";
OleDbCommand cmd = new OleDbCommand(str, con);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
textBox1.Text = dr["subject_abbreviation"].ToString();//column name should be that you want to show on textbox

}
}
}
}

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