Click here to Skip to main content
15,885,720 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void fnGetConnectedToDatabase()
{
    //Connection string
    string conStr="Provider=Microsoft.Jet.OLEDB.4.0;" +
                  " Data Source=..\\..\\studentDB.mdb";
    try
     {
        //Instantiate OleDbConnection and open
        //the connection to the database
        myConn = new OleDbConnection(conStr);
        myConn.Open();
    }
    catch(OleDbException ex)    {
        //get the error message if connection failed
        MessageBox.Show("Error in connection ..."+ex.Message);
    }
    string sqlStr ="SELECT * FROM studentTable;";
    //Instantiate a DataAdapter by passing the sqlStr and myConn.
    //now data is in raw form
    dAdapter = new OleDbDataAdapter(sqlStr,myConn);
    //Instantiate a DataSet
    dset = new DataSet();
    //Gets a collection that provides the master mapping
    // between a source table and a DataTable
    dAdapter.TableMappings.Add("Table", "studentTable");
    //A data adapter object utilizes the Fill method
    //to populate a DataSet or a DataTable object with
    //data retrieved by a SELECT command.
    dAdapter.Fill(dset);
    //When binding a DataSet, .NET automatically uses the corresponding
    //DataViewManager provided through the DataSet.DefaultViewManager property
    this.dviewmanager=dset.DefaultViewManager;
    this.comboBox1.DataSource=this.dviewmanager;
    //display "studentTable.StudentID" in the ComboBox
    this.comboBox1.DisplayMember="studentTable.StudentID";
    //DataBinding for the TextBox controls
    this.textBox1.DataBindings.Add("Text",
              this.dviewmanager,"studentTable.StudentID");
    this.textBox2.DataBindings.Add("Text",
              this.dviewmanager, "studentTable.StudentSubject");
    this.textBox3.DataBindings.Add("Text",
              this.dviewmanager, "studentTable.StudentName");
    // Close the connection to the database.
    this.myConn.Close();
}
Posted
Updated 12-Jun-12 22:57pm
v2
Comments
Master Vinu 13-Jun-12 5:12am    
could you please change in code i cant understand

hi, I had modified your function:
C#
//Connection string
          string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                        " Data Source=..\\..\\studentDB.mdb";
      private void fnGetConnectedToDatabase()
       {
          try
          {
              //Instantiate OleDbConnection and open
              //the connection to the database
              myConn = new OleDbConnection(conStr);
              myConn.Open();
          }
          catch (OleDbException ex)
          {
              //get the error message if connection failed
              MessageBox.Show("Error in connection ..." + ex.Message);
          }
          string sqlStr = "SELECT * FROM studentTable;";
          //Instantiate a DataAdapter by passing the sqlStr and myConn.
          dAdapter = new OleDbDataAdapter(sqlStr, myConn);
          //Instantiate a DataSet
          dset = new DataSet();
          //fill the dataset
          dAdapter.Fill(dset);
          //set the data source
          this.comboBox1.DataSource = ds;
          //display "studentTable.StudentID" in the ComboBox
          this.comboBox1.DisplayMember = "StudentID";

          this.myConn.Close();
      }

[edited] to load related values in Textboxes
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
         try
           {
               //Instantiate OleDbConnection and open
               //the connection to the database
               myConn = new OleDbConnection(conStr);
               myConn.Open();
           }
           catch (OleDbException ex)
           {
               //get the error message if connection failed
               MessageBox.Show("Error in connection ..." + ex.Message);
           }
           string sqlStr = "SELECT * FROM studentTable wehre StudentID="+comboBox1.SelectedValue.ToString()+";";
           //Instantiate a DataAdapter by passing the sqlStr and myConn.
           dAdapter = new OleDbDataAdapter(sqlStr, myConn);
           //Instantiate a DataSet
           dset = new DataSet();
           //fill the dataset
           dAdapter.Fill(dset);
           //DataBinding for the TextBox controls
           this.textBox1.Text=ds.Table[0].Rows[0]["StudentID"].ToString();
           this.textBox2.Text = ds.Table[0].Rows[0]["StudentSubject"].ToString();
           this.textBox3.Text = ds.Table[0].Rows[0]["StudentName"].ToString();
       }

hope this help you.
 
Share this answer
 
v2
Comments
Master Vinu 13-Jun-12 5:50am    
code is not working properly, combobox getting default below message:

System.Data.DataViewManagerListItemTypeDescriptor
tanweer 13-Jun-12 6:06am    
try replacing
this.comboBox1.DataSource = ds;
to:
this.comboBox1.DataSource = ds.Tables[0];
Master Vinu 13-Jun-12 6:25am    
THANKS, NOW ITS WORK BUT AFTER SELECTING VALUE IN COMBOBOX RELATED VALUE IN TEXTBOX DOSENT CHANGE PLEASE ADVISE.
tanweer 13-Jun-12 6:53am    
i will modify the answer, please look it again.
Master Vinu 14-Jun-12 1:55am    
tanveer its not working...could you please tell which code is for page load and which one is for combobox selected??
form1_load { SqlDataAdapter ad=new SqlDataAdapter("select *from reg",con); DataSet ds=new DataSet(); ad.Fill(ds,"reg"); comboBox1.DataSource=ds.Tables["reg"].DefaultView; comboBox1.DisplayMember="name"; comboBox1.ValueMember="name"; } private void comboBox1_SelectedIndexChanged() { SqlDataAdapter addd=new sqldataadapter("select *from reg where name='"+comboBox1.SelectedValue.ToString()+"'",con); DataSet dss=new DataSet(); ad.fill(dss,"reg"); if(dss.Tables["reg"].Rows.Count>0) { textBox1.Text=dss.Tables["reg"].Rows[0]["telno"].ToString(); } else { textBox1.Text=""; } } it is working finely try it.
 
Share this answer
 

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