Assigning Value To Textbox When An Item In Combobox Selected(windows applications)?

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


I am beginner to Windows Applications. I populate all the company names in one combobox ,If i select one company name then thier invoiceno,orderno,customer address,ondate,invoicedate will automatically display in their respective textboxes from database.for that i wrote the code like this.it displays only customer address only remaining it wont display.like orderno,invoiceno,ondate,invoicedate etc..please do the needful.

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.perprice,t1.amount,t2.netvat,t2.tamount,t2.netamount from tbl_report2 t2,tbl_report1 t1 where t1.compname=@compname and t2.compname=@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";

}



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.perprice,t1.amount,t2.netvat,t2.tamount,t2.netamount from tbl_report2 t2,tbl_report1 t1 where t1.compname='hdfcbank' and t2.compname='hdfcbank'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txt_customeraddress .Text =dt .Rows [0][0].ToString ();
txt_customeraddress.Text = dt.Rows[0][0].ToString();
txt_invoiceno.Text = dt.Rows[0][0].ToString();
txt_orderno.Text = dt.Rows[0][0].ToString();
txt_ondate.Text = dt.Rows[0][0].ToString();
cmb_particulars.Text = dt.Rows[0][0].ToString();
txt_quantity.Text = dt.Rows[0][0].ToString();
txt_perprice.Text = dt.Rows[0][0].ToString();
txt_amount.Text = dt.Rows[0][0].ToString();
txt_vat.Text = dt.Rows[0][0].ToString();
txt_totalamount.Text = dt.Rows[0][0].ToString();
txt_netamount.Text = dt.Rows[0][0].ToString();
}

}

Best,
Sudheep.



Responses

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

Up
0
Down
Hi,

In the above code you specified to fill the companyaddress in every textbox. In the "dt .Rows [0][0].ToString (); " the second [0] indicates the column number, so you need to update your code with coressponding column number like txt_invoiceno.Text = dt.Rows[0][1]ToString();
or you can specify the column name like
txt_invoiceno.Text = dt.Rows[0]["invoiceno"].ToString();


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 Chechi,

i do like this only.But,it shows the customeraddress only .when i choose the companyname from the combobox.it won't shows the remaining details.if any corrections in this code.please send me the corrections.please do the needful.


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.perprice,t1.amount,t2.netvat,t2.tamount,t2.netamount from tbl_report2 t2,tbl_report1 t1 where t1.compname=@compname and t2.compname=@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";

}


Best,
Sudheep.

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

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

Up
0
Down
Hi,

What you mean by this "But,it shows the customeraddress only". If all textbox shows the company address, update your code with this

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.perprice,t1.amount,t2.netvat,t2.tamount,t2.netamount from tbl_report2 t2,tbl_report1 t1 where t1.compname='hdfcbank' and t2.compname='hdfcbank'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txt_customeraddress .Text =dt .Rows [0][0].ToString ();
txt_invoiceno.Text = dt.Rows[0][1].ToString();
txt_orderno.Text = dt.Rows[0][3].ToString();
txt_ondate.Text = dt.Rows[0][4].ToString();
cmb_particulars.Text = dt.Rows[0][5].ToString();
//txt_quantity.Text = dt.Rows[0][0].ToString(); i am not able to see the coresponding column in the sql query
txt_perprice.Text = dt.Rows[0][6].ToString();
txt_amount.Text = dt.Rows[0][7].ToString();
txt_vat.Text = dt.Rows[0][8].ToString();
txt_totalamount.Text = dt.Rows[0][9].ToString();
txt_netamount.Text = dt.Rows[0][10].ToString();
}

}



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,

Thanks for replying to my issue.Previously as you said i had change my code.it wont work

Best,
Sudheep.

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

Login to post response