hi. I was trying to choose data from the combobox then When I choose something.. it will also generate the other data into the textbox.

I just wanted to choose a STUD_ID in the combobox and the fname and the lname should show in the textbox. actually.. I just did that.. but the error is I can only choose one time in the comboxBOX. then the "using System.Data.DataRowView;" will just appear in the comboBox... Someone might help me plsss... :(

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;


namespace LECTURE
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\LECTURE\LECTURE\Properties\LEESH.accdb";
                conn.Open();
                OleDbDataAdapter daa = new OleDbDataAdapter("SELECT * FROM STUDENT WHERE STUD_ID=" + "'" + comboBox1.Text + "'", conn);
                DataSet dss = new DataSet();
                daa.Fill(dss);
                comboBox1.DataSource = dss.Tables[0].DefaultView;
                DataView dv = (DataView)comboBox1.DataSource;
                if (dv.Count > 0)
                {
                    textBox1.Text = dv[0]["STUDFNAME"].ToString();
                    textBox2.Text = dv[0]["STUDLNAME"].ToString();
                }
                else
                {
                    MessageBox.Show("No Reocrd hs been found", "Append");
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetBaseException().ToString(), "teka parang may mali?");
            }


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\LECTURE\LECTURE\Properties\LEESH.accdb";
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM STUDENT", conn);
                DataTable ds = new DataTable();
                da.Fill(ds);
                comboBox1.Text = "--Select--";
                for (int i = 1; i < ds.Rows.Count; i++)
                {
                    comboBox1.Items.Add(ds.Rows[i]["STUD_ID"]);
                }
                
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetBaseException().ToString(), "Error In Connection");
            }
           
            
        }
    }
}

Recommended Answers

All 5 Replies

Hey,

first of all:

//replace this
OleDbDataAdapter daa = new OleDbDataAdapter("SELECT * FROM STUDENT WHERE STUD_ID=" + "'" + comboBox1.Text + "'", conn);

// with this 
String cbxText = comboBox1.Text;
if(!String.IsNullOrEmpty)
{
String strSelect = String.Format("SELECT * FROM STUDENT WHERE STUD_ID='{0}'", comboBox1.Text);
OleDbDataAdapter daa = new OleDbDataAdapter(strSelect, conn);
}

Then you would move the Form_Load Code to an extra method, that's what OO is all about. Last but not least, try to use the working code that you moved out of your Form_Load to Load the Form new ;-)

did I make it right? Im sorry.. Im just new in programming database....

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\LECTURE\LECTURE\Properties\LEESH.accdb";
                conn.Open();

                // with this 
                String cbxText = comboBox1.Text;
                if (!String.IsNullOrEmpty)
                {
                    String strSelect = String.Format("SELECT * FROM STUDENT WHERE STUD_ID='{0}'", comboBox1.Text);
                    OleDbDataAdapter daa = new OleDbDataAdapter(strSelect, conn);
                }

               DataSet dss = new DataSet();
                daa.Fill(dss);
                comboBox1.DataSource = dss.Tables[0].DefaultView;
                DataView dv = (DataView)comboBox1.DataSource;
                if (dv.Count > 0)
                {
                    textBox1.Text = dv[0]["STUDFNAME"].ToString();
                    textBox2.Text = dv[0]["STUDLNAME"].ToString();
                }
                else
                {
                    MessageBox.Show("No Reocrd hs been found", "Append");
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetBaseException().ToString(), "teka parang may mali?");
            }


        }

"Then you would move the Form_Load Code to an extra method, that's what OO is all about. Last but not least, try to use the working code that you moved out of your Form_Load to Load the Form new ;-)"
where part? Im sorry Im just a bit confuse. :/

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\LECTURE\LECTURE\Properties\LEESH.accdb";
                conn.Open();

                // with this 
                String cbxText = comboBox1.Text;
                if (!String.IsNullOrEmpty)
                {
                    String strSelect = String.Format("SELECT * FROM STUDENT WHERE STUD_ID='{0}'", comboBox1.Text);
                    OleDbDataAdapter daa = new OleDbDataAdapter(strSelect, conn);
                }

               DataSet dss = new DataSet();
                daa.Fill(dss);
                comboBox1.DataSource = dss.Tables[0].DefaultView;
                DataView dv = (DataView)comboBox1.DataSource;
                if (dv.Count > 0)
                {
                    textBox1.Text = dv[0]["STUDFNAME"].ToString();
                    textBox2.Text = dv[0]["STUDLNAME"].ToString();
                }
                else
                {
                    MessageBox.Show("No Reocrd hs been found", "Append");
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetBaseException().ToString(), "teka parang may mali?");
            }


        }

did I make it right? Im sorry.. Im just new in programming database....


"Then you would move the Form_Load Code to an extra method, that's what OO is all about. Last but not least, try to use the working code that you moved out of your Form_Load to Load the Form new ;-)"
---where part? Im sorry Im just a bit confuse. :/

Your code seems incorrect to me. I have one question, why are you setting DataSource property of Combobox again in Selection Index Changed event ?

Try to use below modified code.

***Code of Form Load event*********
da.Fill(ds)
comboBox1.DataSource = ds;
comboBox1.DisplayMember = "Stud_Id";
comboBox1.ValueMember = "Stud_Id";
comboBox1.Items.Insert(0, "---Select----");
//no need for FOR loop. So comment those portion.

********Code of Selection Index Changed Event ********
OleDbDataAdapter daa = new OleDbDataAdapter("SELECT * FROM STUDENT WHERE STUD_ID=" + "'" + comboBox1.SelectedValue.ToString() + "'", conn);
DataSet dss = new DataSet();                
daa.Fill(dss);      
if(dss.Tables[0].Rows.Count > 0)
{
   textBox1.Text = dss.Tables[0].Rows[0]["STUDFNAME"].ToString();                    
   textBox2.Text = dss.Tables[0].Rows[0]["STUDLNAME"].ToString();                
}                
else                
{                    
   MessageBox.Show("No Reocrd hs been found", "Append");                
}

try by it and let me know...

hi. I was trying to choose data from the combobox then When I choose something.. it will also generate the other data into the textbox.

I just wanted to choose a STUD_ID in the combobox and the fname and the lname should show in the textbox. actually.. I just did that.. but the error is I can only choose one time in the comboxBOX. then the "using System.Data.DataRowView;" will just appear in the comboBox... Someone might help me plsss... :(

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;


namespace LECTURE
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\LECTURE\LECTURE\Properties\LEESH.accdb";
                conn.Open();
                OleDbDataAdapter daa = new OleDbDataAdapter("SELECT * FROM STUDENT WHERE STUD_ID=" + "'" + comboBox1.Text + "'", conn);
                DataSet dss = new DataSet();
                daa.Fill(dss);
                comboBox1.DataSource = dss.Tables[0].DefaultView;
                DataView dv = (DataView)comboBox1.DataSource;
                if (dv.Count > 0)
                {
                    textBox1.Text = dv[0]["STUDFNAME"].ToString();
                    textBox2.Text = dv[0]["STUDLNAME"].ToString();
                }
                else
                {
                    MessageBox.Show("No Reocrd hs been found", "Append");
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetBaseException().ToString(), "teka parang may mali?");
            }


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\LECTURE\LECTURE\Properties\LEESH.accdb";
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM STUDENT", conn);
                DataTable ds = new DataTable();
                da.Fill(ds);
                comboBox1.Text = "--Select--";
                for (int i = 1; i < ds.Rows.Count; i++)
                {
                    comboBox1.Items.Add(ds.Rows[i]["STUD_ID"]);
                }
                
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetBaseException().ToString(), "Error In Connection");
            }
           
            
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.