in gridview how can i display the message as no record for particular compnamename?

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

In my project i designed form like customernamesearch.from the combobox if i selected any value it has to show the data.suppose i selected one name as sudheep it shows the records.but,if the same name is repeated with different invoiceno in that case it shows the records for both.

Best,
Sudheep.



Responses

Posted by: Sksingh on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

If you want to populate records based on invoice no then you should pass invoiceno instead of name.
Let user to selected name but internally pass invoiceno to DB.

Regards,
Sunil

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

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

Up
0
Down
Hi ,


I wrote like this .can you please check my code.please do the needful.


private void btn_search_Click(object sender, EventArgs e)
{
if (cmb_customername.SelectedIndex == -1)
MessageBox.Show("please Select Customer Name:");
else
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd = new SqlCommand("select CompName,CompAddress,InvoiceNo,OrderNo,convert(varchar,OnDate,105)as [ondate],convert(varchar,InvoiceDate,105)as [invoicedate],AmountStatus,NetAmount,NetVat,TAmount from tbl_Report2 where compname=@compname", con);

cmd.Parameters.AddWithValue("@compname", cmb_customername.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();

try
{
con.Open();
da.Fill(dt);

if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;


}
else
{
GridView1.Controls.Clear();
}


SqlCommand cmd2 = new SqlCommand("select invoiceid from tbl_report1 where compname=@compname", con);
cmd2.Parameters.AddWithValue("@compname", cmb_customername.Text);
string invoiceid = Convert.ToString(cmd2.ExecuteScalar());

SqlCommand cmd1 = new SqlCommand("select Particulars,OrderNo,Quantity,PerPrice,Amount from tbl_Report1 where InvoiceId=@InvoiceId", con);
cmd1.Parameters.AddWithValue("@invoiceid", invoiceid);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();

da1.Fill(dt1);
GridView2.DataSource = dt1;
}
catch (SqlException ex)
{
throw;

}
finally
{
con.Close();
}



}
}


Best,
Sudheep.

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

Posted by: Sksingh on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Try below code

private void btn_search_Click(object sender, EventArgs e) 

{
int invoiceno = 0;

if (cmb_customername.SelectedIndex == -1)
MessageBox.Show("please Select Customer Name:");
else
{
if( cmb_customername.SelectedValue.ToString() != "'System.Data.DataRowView" && cmb_customername.SelectedValue.ToString() != "" && cmb_customername.SelectedValue.ToString() != String.Empty )
{
invoiceno = Convert.ToInt32(cmb_customername.SelectedValue);

}

SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd = new SqlCommand("select CompName,CompAddress,InvoiceNo,OrderNo,convert(varchar,OnDate,105)as [ondate],convert(varchar,InvoiceDate,105)as [invoicedate],AmountStatus,NetAmount,NetVat,TAmount from tbl_Report2 where compname=@compname and InvoiceNo =@InvoiceNo", con);



cmd.Parameters.AddWithValue("@compname", cmb_customername.Text);
cmd.Parameters.AddWithValue("@InvoiceNo", invoiceno);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();

try
{
con.Open();
da.Fill(dt);

if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;


}
else
{
GridView1.Controls.Clear();
}


//SqlCommand cmd2 = new SqlCommand("select invoiceid from tbl_report1 where compname=@compname", con);
//cmd2.Parameters.AddWithValue("@compname", cmb_customername.Text);
//string invoiceid = Convert.ToString(cmd2.ExecuteScalar());

SqlCommand cmd1 = new SqlCommand("select Particulars,OrderNo,Quantity,PerPrice,Amount from tbl_Report1 where InvoiceId=@InvoiceId", con);
cmd1.Parameters.AddWithValue("@invoiceid", invoiceno);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();

da1.Fill(dt1);
GridView2.DataSource = dt1;
}
catch (SqlException ex)
{
throw;

}
finally
{
con.Close();
}



}
}


Even no need to hit server two times, you can return both source in on go becuase u r using Dataset.

Regards,
Sunil

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

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

Up
0
Down
HI,

thanks for replying to me it's not working.

Best,
Sudheep.

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

Posted by: Sksingh on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Keep invoiceno as globally because u can take selected value of combobox onClick of search button and then try it out.

Changes done as below:
1. From above code,Now you first grid will be fill based on companyname and invoiceno earlier it was considering only name.
2. If you keep invoice no gloabally so during selected index changed that variable get assingned so no need to hit database to get invoice no based on name.
3. Then pass invoice no to get data from your second table so that i would not so records, even though name match because in first step itself only those data will filter where name as well as invoiceno will match.

Debug it and change accordingly..


Regards,
Sunil

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

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

Up
0
Down
Hi,

as you said i wrote the coding but it shows the tbl_report1 only with no records.for every value in combobox.

Best,
Sudheep.

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

Posted by: Sksingh on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Debug it and check what value do u get get invoiceno and companyname and run same query on server and see whether u get data r not.
Also see you combobox binding whether have u set valuemember as "invoiceno" or anything else .

Regards,
Sunil

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

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

Up
0
Down
Hi,

for generating companynames list i wrote this coding.

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;
}

Best,
Sudheep.

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

Posted by: Varung on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
use like keyword in query as
select <column names> from <table name> where <column name> like '%"+txtbox.Text+"%'

G.Varun

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

Posted by: Patnaik.jyoti on: 12/13/2011 [Member] Starter | Points: 25

Up
0
Down
Hi in your dropdown/Combo you can have DataTextField as Name and DataValueField as Invoiceno, so when you select name , you need to pass the selected value i.e invoice no to your search function so that you can have only one value instead of many

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

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

Up
0
Down
Hi,

what is the solution for this .how can i modify the code.

Best,
Sudheep.

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

Login to post response