how can i bind the datasource to the combobox?(windows applications)

Posted by Sudheep.grandhe under C# on 11/28/2011 | Points: 10 | Views : 5306 | Status : [Member] | Replies : 4
Hi,

i want to display the companynames in combobox.if i selected the companyname it should display the below columns .for this how can i bind the data to combobox and how can i declare display member and valuemember from the below columns.

private void loadcompanies()
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("select t2.compaddress,t1.invoiceno,t1.orderno,t1.ondate,t1.particulars,t1.quantity,t1.perprice,t1.amount,t2.netvat,t2.tamount,t2.netamount from tbl_report2 t2,tbl_report1 t1 where
t1.invoiceno= t2.invoiceno and t2.compname=t1.compname ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
da.Fill(dt);
cmb_companyname.DataSource = dt;
cmb_companyname.DisplayMember = "compname";
cmb_companyname.ValueMember = "compaddress";
//SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
//SqlCommand cmd = new SqlCommand("select 0 as custid,'selectone' as custname union select custid,custname from dbo.tbl_customer", con);
//SqlDataAdapter da = new SqlDataAdapter(cmd);
//DataTable dt = new DataTable();
//if (con.State == ConnectionState.Closed)
//{
// con.Open();
//}
//da.Fill(dt);

//cmb_companyname.DataSource = dt;
//cmb_companyname.DisplayMember = "custname";
//cmb_companyname.ValueMember = "custid";
}

Best,
Sudheep.



Responses

Posted by: Blessyjees on: 11/28/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Try this

private void loadcompanies()
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("select compname from tbl_report1 ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
da.Fill(dt);
cmb_companyname.DataSource = dt;
cmb_companyname.DisplayMember = "compname";
cmb_companyname.ValueMember = "compname";
}
If you have master table company, then you can use that table for select data like "select compname from tblCompany"

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

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

Posted by: Sudheep.grandhe on: 11/28/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

I am not having the master table like tblcompany.what can i do.please do the needful.

Best,
Sudheep.

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

Posted by: Arefin on: 11/28/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

First, collect comoanyName form combobox and then execute the same query with an extra WHERE condition, like

select t2.compaddress,t1.invoiceno,t1.orderno,t1.ondate,t1.particulars,t1.quantity,t1.perprice,t1.amount,t2.netvat,t2.tamount,t2.netamount from tbl_report2 t2,tbl_report1 t1 where

t1.invoiceno= t2.invoiceno and t2.compname=t1.compname
AND compname = combobox_value ;


Thanks,
Arefin

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

Posted by: Sudheep.grandhe on: 11/30/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,


it shows the error as: Ambiguous column name 'compname'.
private void loadcompanies()
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("select t2.compaddress,t1.invoiceno,t1.orderno,t1.ondate,t1.particulars,t1.quantity,t1.perprice,t1.amount,t2.netvat,t2.tamount,t2.netamount from tbl_report2 t2,tbl_report1 t1 where t1.invoiceno= t2.invoiceno and t2.compname=t1.compname AND compname ='hdfcbank'",con );
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
da.Fill(dt);
cmb_companyname.DataSource = dt;
cmb_companyname.DisplayMember = "compname";
cmb_companyname.ValueMember = "compname";
}
private void cmb_companyname_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("select t2.compaddress,t1.invoiceno,t1.orderno,t1.ondate,t1.particulars,t1.quantity,t1.perprice,t1.amount,t2.netvat,t2.tamount,t2.netamount from tbl_report2 t2,tbl_report1 t1 wheret1.invoiceno= t2.invoiceno and t2.compname=t1.compname AND compname =@cmb_companyname;", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
//txt_customeraddress.Text = cmb_companyname.SelectedValue.ToString();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txt_customeraddress .Text =dt .Rows [0][0].ToString ();
txt_customeraddress.Text = dt.Rows[0]["compaddress"].ToString();
txt_invoiceno.Text = dt.Rows[0]["invoiceno"].ToString();
txt_orderno.Text = dt.Rows[0]["orderno"].ToString();
txt_ondate.Text = dt.Rows[0]["ondate"].ToString();
cmb_particulars.Text = dt.Rows[0]["particulars"].ToString();
txt_quantity.Text = dt.Rows[0]["quantity"].ToString();
txt_amount.Text = dt.Rows[0]["amount"].ToString();
txt_vat.Text = dt.Rows[0]["netvat"].ToString();
txt_totalamount.Text = dt.Rows[0]["tamount"].ToString();
txt_netamount.Text = dt.Rows[0]["netamount"].ToString();
}

}

Best,
Sudheep.

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

Login to post response