Click here to Skip to main content
15,913,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys!

I would like to get the records from the database that matches the IDs from the two columns of my grid view. What would be my query for it? Can anyone help me?
Posted

1 solution

Declare a StringBuilder.
Loop through the rows of GridView.
Inside the Loop, get the Column Value you want and append that to the StringBuilder.
After the loop, execute one query like
C#
"SELECT something FROM TableName WHERE Id in (" + objStrBuilder + ")";
 
Share this answer
 
Comments
DarkDreamer08 2-Oct-14 23:05pm    
It is somehow hard for me to understand your solution. Could you elaborate it more, please? I hope you extend your help regrading this matter.
Which point?
DarkDreamer08 2-Oct-14 23:43pm    
string builder :)
Simple use string array. Forget builder.

Your final query should look like...

SELECT something FROM TableName WHERE Id IN (1, 2, 3)

1, 2, 3 are the ids.

So these Ids you have to get from GridView. So, you will loop through the Rows and get the column value inside the loop and add to the StringBuilder.

string[] myValues = new string[gv.Rows.Count];

foreach (int i =0; i < gv.Rows.Count; i++)
{
myValues[i] = gv.Rows[i]["ID"];
}

string allIds = string.Join(",", myValues);

Now you can use this with the query like...

string query = "SELECT something FROM TableName WHERE Id in (" + allIds.ToString() + ")";

Do u understand now?

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