How to display Combobox selected data in text boxes using csharp

25/04/2017   12:06:35 PM

Hello,in this C# tutorial i will explain display data from sql server database to combobox then combobox selected record display in textboxes.for example we will display  students name  in combobox from database.then after select particular student name from combobox that student details like student roll no,student name will display in textboxes

1 .Create a database name as Studentdb.Create a Table name as StudentRegistration in this database.Create following fields in table
 
Column Name    
DataTypes
Id
         Int [Identity Property Set =True]
StudentName
         varchar(50)
 

2. Open VisualStudio.net / Select C# / Windows Application

 

3. Make the design of the form as Follows

 

4.add the following code in to App.config file in </configuration> tag

<connectionStrings>
<add name="cn" connectionString="Data Source=.\sqlexpress;Initial Catalog=Studentdb;Integrated Security=True"/>
</connectionStrings>


5.Add a refrence System.Configurtation

Right click on refrences folder in project folder/Add Refernces / From Refrence Manager window/ Assemblies option / click on Framework option / select System.Configuration from list /click on ok

Now System.Configuration added in project

 

6.Create a class dbconnect.cs and add the following code in that class

using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
 
namespace ComboboxToTextboxdb
{
class dbConnect
{
public static string dbconnectivity()
{
return ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
}
}
}

 

7. Add the following code  for display data in to combobox.the combobox selected record display in textboxes

using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
 
namespace ComboboxToTextboxdb
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection cn = new SqlConnection(dbConnect.dbconnectivity());
SqlDataAdapter da;
DataTable dt;
SqlCommand cmd;
SqlDataReader dr;
 
//display data in to combobox
private void Form1_Load(object sender, EventArgs e)
{
da = new SqlDataAdapter("Select * From StudentRegistration", cn);
dt = new DataTable();
da.Fill(dt);
DataRow dr = dt.NewRow();
dr["StudentName"] = "Select Student Name";
dt.Rows.InsertAt(dr, 0);
cmbStudentName.ValueMember = "StudentId";
cmbStudentName.DisplayMember = "StudentName";
cmbStudentName.DataSource = dt;
 
}
 
//display combobox selected data to textboxes
private void cmbStudentName_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
cmd = new SqlCommand("Select * From StudentRegistration where StudentName=@name", cn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@name", cmbStudentName.Text);
cn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
txtrollno.Text = dr[0].ToString();
txtName.Text = dr[1].ToString();
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
}
}
}
}

Output

                   

Support us by Sharing