Click here to Skip to main content
15,911,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please help me friends...........


in window form i have three different-2 criteria to filter data for thre i have three cheakesdlistbox(1st for partyname .2nd for exchange type and 3rd one for itemname) each contain number of items let if user select(by cheeking cheak box) 10 party name now he select 2 exhange type of the available exchange type and in third ithemname may he select all item or sum now as he press show button data should be fill in datagridview with the above selection .........now i want to pass these selected values in whare clause ..like..whrere partyanme="all value from partyname" and exchangetype="all selected exhange type"and itemname="selected itemname"....
please help me how to write selct query with multiple selected values....

C#.sqlserver

thanks in advanced
u can code on my id LAkhanp22@gmail.com


lakhan
Posted
Updated 8-Dec-11 2:39am
v2

Assuming your selected party names and exchange types are in some sort of collection, you could use a StringBuilder to build the query string:

C#
StringBuilder query = new StringBuilder("select * from table t where ");
for (int i = 0; i < names.Count; i++)
{
    builder.AppendFormat("t.partyname='{0}' ", names[i]);
    if (names.Count - 1 == i)
    {
        builder.Append("AND ");
    }
}
if (types.Count > 0)
{
    builder.Append("AND ");
}
for (int i = 0; i < types.Count; i++)
{
    builder.AppendFormat("t.exchangetype='{0}'", types[i]);
    if (types.Count - 1 == i)
    {
        builder.Append("AND ");
    }
}
 
Share this answer
 
Comments
[no name] 8-Dec-11 8:44am    
first of all thanks ..
its NOT working Actually how can i pass cheakedlistbox selected Items
[no name] 8-Dec-11 8:45am    
can u describe how please
Jephunneh Malazarte 8-Dec-11 11:43am    
what is the variable datatype that holds your selected items? i think based on the solution of john you need to do minor modifications as follow

1. you should use IN instead of = operator found inside the for loop
2. paste that inside one function let say public void SaveMe(string []types, string[] names){//paste the code here...}
[no name] 22-Dec-11 7:48am    
thanks john .......
If you post code it will be helpful to solve perfectly. You can use Inner join between tables to filter the criteria
 
Share this answer
 
Comments
[no name] 8-Dec-11 8:37am    
ok string sqlqry = "SELECT ITEMCODE, BROKTYPE,BROKRATE,TRANRATE,TranType,uptostdt,MARTYPE,MARRATE,PARTYTYPEv FROM PITBROK where ITEMCODE=////now how to put all selected values from cheakedlistbox....

SqlDataAdapter da = new SqlDataAdapter(sqlqry, con);
DataSet ds1 = new DataSet();
da.Fill(ds1, "PITBROK");

//if (ds1.Tables[0].Rows.Count > 0)
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
//for (int j = 0; j < ds1.Tables[0].Columns.Count; j++)
//{
dataGridView1.ColumnCount = 9;
dataGridView1.Rows.Add();

dataGridView1[0, i].Value = ds1.Tables[0].Rows[i][0].ToString();
//dataGridView1[1, i].Value = ds1.Tables[0].Rows[i][1].ToString();
string b = ds1.Tables[0].Rows[i][1].ToString();

if (b == "O")
{

dataGridView1[1, i].Value = "Opening Sauda";
dataGridView1[1, i].Style.BackColor = Color.LightBlue;
}
else if (b == "P")
{

dataGridView1[1, i].Value = "Persentage Wise";
dataGridView1[1, i].Style.BackColor = Color.SteelBlue;
}
else if (b == "T")
{

dataGridView1[1, i].Value = "Transaction";
dataGridView1[1, i].Style.BackColor = Color.Tomato;
}
dataGridView1[2, i].Value = ds1.Tables[0].Rows[i][2].ToString();
dataGridView1[3, i].Value = ds1.Tables[0].Rows[i][3].ToString();
string l = ds1.Tables[0].Rows[i][4].ToString();


if (l == "O")
{

dataGridView1[4, i].Value = "Opening Sauda";
dataGridView1[4, i].Style.BackColor = Color.LightBlue;
}
else if (l == "P")
{

dataGridView1[4, i].Value = "Persentage Wise";
dataGridView1[4, i].Style.BackColor = Color.SteelBlue;
}
else if (l == "T")
{

dataGridView1[4, i].Value = "Transation";
}


dataGridView1[5, i].Value = ds1.Tables[0].Rows[i][5].ToString();
string m = ds1.Tables[0].Rows[i][6].ToString();
if (m == "Q")
{

dataGridView1[6, i].Value = "Qty Wise(perunit)";
}
else if (m == "V")
{

dataGridView1[6, i].Value = "Value Wise(in%)";
}
else if (m == "I")
{

dataGridView1[6, i].Value = "Import Rates";
}
else if (m == "C")
{
dataGridView1[6, i].Value = "Client Wise Margine";
}

dataGridView1[7, i].Value = ds1.Tables[0].Rows[i][7].ToString();
dataGridView1[8, i].Value = ds1.Tables[0].Rows[i][8].ToString();

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900