it shows the following error?

Posted by Sudheep.grandhe under C# on 11/8/2011 | Points: 10 | Views : 1277 | Status : [Member] | Replies : 2
HI,

in my project.there is a form called customername search.user enters the customer name that particular record should be displayed insead of that it will display all the records.but,in company name it display like system.data.datarowview.how can i avoid this error.please do the needful.here is my code i will post

public void cmddisplaycustomer()
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("select custid, custname from tbl_customer order by custname", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr = dt.NewRow ();
dr[0] = "0";
dt.Rows.InsertAt(dr, 0);
//cmd.EndExecuteNonQuery();
//con.Close();
cmb_customername.DataSource = dt;
cmb_customername.ValueMember = "custid";
cmb_customername.DisplayMember = "custname";
//cmb_customername.Items.Insert(1, "-select one-");
cmb_customername.SelectedIndex = -1;
}


private void btn_search_Click(object sender, EventArgs e)
{
GridView2.Controls.Clear();
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("select CompName,CompAddress,InvoiceNo,OrderNo,convert(varchar,OnDate,105),convert(varchar,InvoiceDate,105),AmountStatus,NetAmount,NetVat,TAmount from tbl_Report2 where compname=@compname", con);
cmd.Parameters.AddWithValue("@compname", cmb_customername.SelectedItem.ToString());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
}
else
{
GridView1.Controls.Clear();
}
SqlConnection con2 = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd2 = new SqlCommand("select invoiceid from tbl_report1 where compname=@compname", con2);
cmd2.Parameters.AddWithValue("@compname", cmb_customername.SelectedItem.ToString());
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
string invoiceid = dt2.ToString();

SqlConnection con1 = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd1 = new SqlCommand("select Particulars,OrderNo,Quantity,PerPrice,Amount from tbl_Report1 where InvoiceId=@InvoiceId", con1);
cmd1.Parameters.AddWithValue("@invoiceid", invoiceid);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
{
GridView2 .DataSource =dt1 ;
}



}

private void cmb_customername_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.Controls.Clear();
GridView2.Controls.Clear();
}

private void CustomerNameSearch_Load(object sender, EventArgs e)
{
cmddisplaycustomer();
GridView1.Controls.Clear();
GridView2.Controls.Clear();

}
}
}

Best,
Sudheep.



Responses

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

Up
0
Down
hi,

you didn't open your con in search button click. You can use same connection object for all operations.

SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true"); 

com.Open();
SqlCommand cmd;
SqlDataAdapter da;
cmd = new SqlCommand("select CompName,CompAddress,InvoiceNo,OrderNo,convert(varchar,OnDate,105),convert(varchar,InvoiceDate,105),AmountStatus,NetAmount,NetVat,TAmount from tbl_Report2 where compname=@compname", con);
cmd.Parameters.AddWithValue("@compname", cmb_customername.SelectedItem.ToString());
da= new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
}
else
{
GridView1.Controls.Clear();
}

cmd = new SqlCommand("select invoiceid from tbl_report1 where compname=@compname", con);
cmd.Parameters.AddWithValue("@compname", cmb_customername.SelectedItem.ToString());
da = new SqlDataAdapter(cmd);
DataTable dt2 = new DataTable();
da.Fill(dt2);
string invoiceid = dt2.ToString();

cmd = new SqlCommand("select Particulars,OrderNo,Quantity,PerPrice,Amount from tbl_Report1 where InvoiceId=@InvoiceId", con);
cmd.Parameters.AddWithValue("@invoiceid", invoiceid);
da = new SqlDataAdapter(cmd);
DataTable dt1 = new DataTable();
da.Fill(dt1);
{
GridView2 .DataSource =dt1 ;
}


finally you should close the connection.

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

Up
0
Down
Hi,

it wont work.it shows the error

Best,
Sudheep.

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

Login to post response