hey...
i'm working on windows form, and on that form i have a comboBox that attached to a dataBase, and several empty
textBoxes that dosen't attached to anything...
now what i want is this:
when i choose a value from the comboBox then the empty textBoxes will fill with data from the dataBase...
for example, if the comboBox attached to a "person" table to the "ID" column, then when i choose an ID number from the comboBox then the empty textBoxes will fill with that same person data, like age,birthDate,height exc...
i know that i'm suppose to use the function "SelectedIndexChanged", but how?
if someone can help me or even post a similer example it will help alot
thanks!
Loading
Satyapriya NayakPosted Aug 1, 2011, 5:18 AM
please refer this link
http://www.c-sharpcorner.com/UploadFile/satyapriyanayak/8561/"
If this post help you please mark it as answer
VulpesPosted Aug 1, 2011, 4:35 AM
roi kruvPosted Aug 1, 2011, 4:16 AM
i'v tried your code but i get an error, thats my code: ("aptNum" is the primary key of Apartment table)
DataTable dt = (DataTable)comboBox4.DataSource;
int apartmentNum = (int)((DataRowView)comboBox4.SelectedItem)["aptNum"];
DataRow dr = dt.Rows.Find(apartmentNum);
textBox6.Text = dr["floor"].ToString();
i get the following error:
Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.
i'v tried to googel the error but nothing helpful came out, got any ideas?
Satyapriya NayakPosted Jul 31, 2011, 10:57 PM
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 combobox_textbox_data
{
public partial class Form2 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
DataSet ds;
string str;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from employee";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["empid"]);
}
reader.Close();
con.Close();
comboBox1.Text = "select";
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from employee where empid='" + comboBox1.Text.Trim() + "'";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
if (reader.Read())
{
textBox1.Text = reader["empname"].ToString();
textBox2.Text = reader["sal"].ToString();
textBox3.Text = reader["empaddress"].ToString();
textBox4.Text = reader["phoneno"].ToString();
}
con.Close();
reader.Close();
}
}
}
If this post help you please mark it as answer
Ahmad Abu EidPosted Jul 31, 2011, 7:03 PM
string SqlStr = "Select * From person where id=" + comboBox1.SelectedValue.ToString();
SqlDataAdapter da = new SqlDataAdapter (SqlStr, Your Sql Connection);
DataTable dt = new DataTable();
try
{
da.Fill(dt);
}
catch
{
//Error
}
finally
{
close your connection
}
//now fill the boxes
LabelFname.Text = dt.Rows[0]["FullName"].ToString();
LabelCity.Text = dt.Rows[0]["ProvinceName"].ToString();
VulpesPosted Jul 31, 2011, 6:45 PM