How to display items in listbox using c# [Resolved]

Posted by Kundan under C# on 8/29/2013 | Points: 10 | Views : 2228 | Status : [Member] | Replies : 3
How to display items in listbox using c#




Responses

Posted by: Satyapriyanayak on: 8/29/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You want to display data from database into listbox.You can bind only one column as below 


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.SqlClient;

namespace _listbox
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(@"server = HOME-11/SQLEXPRESS;data source = .; initial catalog = list; user id=sa;pwd =abc ;integrated security = SSPI");
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
DataTable dt;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
listboxbind();
}

private void listboxbind()
{
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
dt = ds.Tables["employee"];
listBox1.DisplayMember = "empid";
listBox1.ValueMember = "empname";
listBox1.DataSource = ds.Tables["employee"];
con.Close();
}

}
}


If this post helps you mark it as answer
Thanks

Kundan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 8/29/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
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.Collections;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ArrayList name = new ArrayList();
name.Add("Raj");
name.Add("Ravi");
name.Add("Rahul");
listBox1.DataSource = name;
}
}
}


If this post helps you mark it as answer
Thanks

Kundan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 8/30/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

try this pure asp.net C# Code

SqlConnection con = new SqlConnection("data source =Test;initial catalog =Test; user id=sa;pwd =test;");
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
listboxbind();
}
}

private void listboxbind()
{
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
dt = ds.Tables["employee"];
ListBox1.DataTextField = "Department";
ListBox1.DataValueField = "EMPLOYEEID";
ListBox1.DataSource = ds.Tables["employee"];
ListBox1.DataBind();
con.Close();

}

Mark as Answer if its helpful to you

Kundan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response