Error while searching in a DataView [Resolved]

Posted by Akiii under C# on 4/28/2011 | Points: 10 | Views : 1276 | Status : [Member] | Replies : 2
Hi, I want to search the dataview with a particular value,retrieve it and show it in a datagrid but its not searching appropriately. I am pasting the code for u to see....

string connstr = "Data Source=Akiii-PC;Initial Catalog=rgdb;User Id = sa;password = 12345";

SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd;
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
DataView dv;

try
{
conn.Open();
cmd = new SqlCommand("select * from reg_table", conn);
sda.SelectCommand = cmd;
sda.Fill(ds, "reg_table");
sda.Dispose();
cmd.Dispose();
conn.Close();

dv = new DataView(ds.Tables[0]);
dv.Sort = "name";
int index = dv.Find("12333");

if(index == -1)
{
Response.Write("no result");
}
else
{
GridView1.DataSource = dv;
GridView1.DataBind();
//Response.Write(dv[index]["Product_id"].ToString() + " " + dv[index]["Product_Name"].ToString());
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}


12333 is the value that i want to search in the dataview which has been populated by dataset but it cannot find any result.
Please help me on this.....
Am i doing something wrong here...?

Thanks and Regards
Akiii




Responses

Posted by: Nishithraj on: 4/28/2011 [Member] Bronze | Points: 50

Up
0
Down

Resolved
The Find method only returns an integer with the index of the DataRowView. But in your case you have to try with FindRows. Something like the following

DataRowView[] rows = dataView.FindRows("uuu"); 


It will return the entire rows that you have filtered. Then only you can bind the array to the datagrid.


Please mark this as answer, if it is.....

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

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

Posted by: Akiii on: 4/28/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi Nishithraj ,

Thanks for your prompt reply......
I have used your code and successfully did what i wanted.....and moreover i have also found another way to do that...its written below.....

dv.RowFilter = "country = 'dsa'";

I can then assign this dataview object that is "dv" to datagrid source.....
This is also working fine.....

but i first tested ur query so.....i will mark it as answer....
Thanks and regards
Akiii

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

Login to post response