update textbox based on value selected in combobox?

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

i have the problem in my code.if i wrote the code in selectedindexchanged it shows the error as invalid columns.please do the needful.

Errors:
Invalid column name 'compaddress'.
Invalid column name 'particulars'.
Invalid column name 'perprice'.
Invalid column name 'amount'.
Invalid column name 'netvat'.
Invalid column name 'tamount'.
Invalid column name 'netamount'.

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 t1,tbl_report1 t2 where t1.compname=@compname and t2.compname=@compname",con );
cmd.Parameters.AddWithValue("@compname",cmb_companyname .SelectedItem .ToString ());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
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_perprice.Text = dt.Rows[0]["perprice"].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.



Responses

Posted by: muralikrishnasurap-12704 on: 11/24/2011 [Member] Starter | Points: 25

Up
0
Down
hi , i didn't find any problem with your code more over i also done some work around your logic...using data table it worked for me with out any issue..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
string Connection = "data source=WS-INBLR131\\SQLEXPRESS;initial catalog=Practive;integrated security=true";
SqlConnection _con = new SqlConnection(Connection);
SqlCommand cmd = new SqlCommand("select t2.name,,t1.fff,t1.password from user_name t2,User_details t1 where t1.name=@name and t2.name=@name", _con);
cmd.Parameters.AddWithValue("@name", "murali");
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
string name = dt.Rows[0]["name"].ToString();
}

}
catch (Exception ex)
{
ex.Message.ToString();
}


}
}


only thing i can suggest is specify the column names explicitly in your SQL query string because this error will occur generally when you are trying to access column which is not available in DataTable row...

SqlCommand cmd=new SqlCommand ("
select t2.compaddress as Compaddress,
t1.invoiceno as invoice,
t1.orderno as Orderno,
t1.ondate as ondate,
t1.particulars as particlars,
t1.perprice as Perprice,
t1.amount as amount,
t2.netvat as netvat,
t2.tamount as Amount,
t2.netamount as NetAmount
from tbl_report2 t1,tbl_report1 t2 where t1.compname=@compname and t2.compname=@compname",con );

Best Regards
Murali Krishna.S

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

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

Up
0
Down
Hi,

if i try your code as mention above.But, same errors are occur here also.

Errors:
Invalid column name 'compaddress'.
Invalid column name 'particulars'.
Invalid column name 'perprice'.
Invalid column name 'amount'.
Invalid column name 'netvat'.
Invalid column name 'tamount'.
Invalid column name 'netamount'.

Best,
Sudheep.

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

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

Up
0
Down
Hi Sudheep,

Replace your code with below

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_report1 t1,tbl_report2 t2 where t1.compname=@compname and t2.compname=@compname",con );

Only need to change this line.

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/24/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Chechi,

it's working .But,it wont displays the data.please do the needful.

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=@compname and t2.compname=@compname", con);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.SelectedItem .ToString ());
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]["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_perprice.Text = dt.Rows[0]["perprice"].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

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

Up
0
Down
Hi,

Dubug your code and check the company name, also you can run this query in query analayser.

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: Blessyjees on: 11/24/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi

Try
cmb_companyname.Text
in place of cmb_companyname.SelectedItem.ToString()




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

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

Login to post response