How to get data from combo box?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kumardharanik
    New Member
    • Jul 2010
    • 7

    How to get data from combo box?

    Pls could anyone help me how to get data from combo box with my following code

    i used txt_app_code for combo box name.

    Code:
    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;
    
    namespace WindowsFormsApplication8
    {
        
        public partial class frm_Application_Details : Form
        {
            public ADODB.Connection con = new ADODB.Connection();
            public ADODB.Recordset rs = new ADODB.Recordset();
            public string ConStr;
            public string db_Server;
            public string db_port;
            public string db;
            public string db_User;
            public string db_Pass;
            public string query;
         
    
            public frm_Application_Details()
            {
                InitializeComponent();
            }
    
           
    
    
            private void button2_Click(object sender, EventArgs e)
            {
                if (txt_App_Code.Text == "")
                {
                    MessageBox.Show("Please Enter the Application Code");
                    return;
                }
                if (txt_App_Name.Text == "") { MessageBox.Show("Please Enter the name"); return; }
                if (txt_Start_Date.Text == "") { MessageBox.Show("Please Enter the Date and Time"); return; }
    
                query = "select * from application_m where ApplicationCode='" + txt_App_Code.Text + "'";
                rs.Open(query, con, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, -1);
                if (rs.EOF == false)
                {
                    rs.Close();
                    MessageBox.Show("Application Code is Already used. Please Enter Different Application code");
                    return;
                }
                else
                {
                    rs.Close();                
                    query = "Insert Into application_m(ApplicationCode,ApplicationName,StartDate) Values('" + txt_App_Code.Text + "','" + txt_App_Name.Text + "',now())";
                    rs.Open(query, con, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, -1);
                    MessageBox.Show("Application details saved");
                }
            }
    
     private void frm_Application_Details_Load(object sender, EventArgs e)
            {
                db_Server = "localhost";
                db_port = "3306";
                db = "etechdata";
                db_User = "root";
                db_Pass = "";
                ConStr = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" + db_Server + ";PORT=" + db_port + ";DATABASE=" + db + ";UID=" + db_User + ";PWD=" + db_Pass + ";OPTION=3";
                //OdbcCon.ConnectionString = ConStr;
             try
                {
    
                    con.Open(ConStr, null, null, 0);
    
                }
                catch (System.Data.Odbc.OdbcException Ex)
                {
    
                    MessageBox.Show("Could not access the database.\r\nPlease make sure you completed the fields with the correct information and try again.\r\n\r\nMore details:\r\n" + Ex.Message, "Database connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I don't understand your question... what data do you need to get? You're already calling txt_App_Code.Te xt to get the currently selected item.

    Do you need something else? Please be more specific about your question.

    Comment

    • kumardharanik
      New Member
      • Jul 2010
      • 7

      #3
      Hi gary tnx for replying me..

      i need to get the data from database and populate in the combo box.

      Comment

      • Leito
        New Member
        • Apr 2010
        • 58

        #4
        Here is how I retrieve data from my database to populate the comboBox:
        Code:
        // Set query to retrieve clients
        String query = "SELECT id, name FROM tblClients";
        // Execute query
        SqlDataAdapter clientsDA = new SqlDataAdapter(query, database_con);
        // Fill dataset
        DataSet clientsDS = new DataSet();
        clientsDA.Fill(clientsDS);
        // Add clients list to the comboBox
        this.comboBoxClients.DataSource = clientsDS.Tables[0];
        this.comboBoxClients.DisplayMember = "name";
        this.comboBoxClients.ValueMember = "id";

        Comment

        Working...