Hi!
On my form I have a combobox1 and label1.
With these codes I would like to populate data from on my combobox1 from the name column to show others datas columns on the textboxes. Curiously its cant populate in my combobox1.
Anyone can help?
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\INAC\Documents\Windows_Application_Samples\ComboboxItems_show_in_label\App_Data\office.mdb;Persist Security Info=False";
OleDbConnection con = new OleDbConnection(str);
string query = "select * from company where name = '"+comboBox1.Text+"' ";
OleDbCommand cmd = new OleDbCommand( query,con);
OleDbDataReader dbr;
try
{
con.Open();
dbr = cmd.ExecuteReader();
while(dbr.Read())
{
string sID = (string) dbr["ID"].ToString();
string sname = (string) dbr["name"].ToString(); // name is string value
string ssurname = (string) dbr["surname"].ToString();
string sage = (string) dbr["age"].ToString();
label1.Text = ssurname;
}
}
catch(Exception es)
{
MessageBox.Show(es.Message);
}
}
IsraelPosted Oct 13, 2014, 5:37 PM
See what I do try. Its showing me a messagebox with items that I put inside of the combobox. But when I delete all the items inside of my combobox... When I run my program the combobox its still blank without data of my database.
private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
if (comboBox1.SelectedIndex == -1) return;
MessageBox.Show(comboBox1.SelectedItem.ToString());
string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\INAC\Documents\Windows_Application_Samples\ComboboxItems_show_in_label\App_Data\office.mdb;Persist Security Info=False";
OleDbConnection con = new OleDbConnection(str);
string query = "select * from company where name = '"+comboBox1.Text+"' ";
OleDbCommand cmd = new OleDbCommand( query,con);
OleDbDataReader dbr;
try
{
con.Open();
dbr = cmd.ExecuteReader();
while(dbr.Read())
{
string sID = (string) dbr["ID"].ToString();
string sname = (string) dbr["name"].ToString(); // name is string value
string ssurname = (string) dbr["surname"].ToString();
string sage = (string) dbr["age"].ToString();
label1.Text = ssurname;
}
}
catch(Exception es)
{
MessageBox.Show(es.Message);
}
}
VulpesPosted Oct 13, 2014, 6:32 AM
IsraelPosted Oct 13, 2014, 4:00 AM
I do make the change exactly as you do write it. Nothing happen.
@Riddhi Valecha
The codes you are seing now it's the entire code.
At all when I fill manually my combobox with data inserted in my database its works. But my goal is not that. I want to see the data loaded in the combobox without it this combobox manually.
Riddhi ValechaPosted Oct 13, 2014, 1:23 AM
How have you uploaded the combo box ??
The values are of type ListItem or String ??
Please send your code if possible.
VulpesPosted Oct 12, 2014, 5:54 PM