Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["database_con"].ConnectionString);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select userid from user_detail", con);
DataSet ds = new DataSet();
da.Fill(ds);


i fill dataset now i want to fetch record from dastaset using following query :
select column1,column2 from dataset

How to do this??
Posted

you can use for loop :
C#
for (int i=0;i<ds.tables[0].rows.count;i++)>
{
string value=ds.Tables[0].Rows[i]["Col1"].Tostring();
string vlue2=ds.Tables[0].Rows[i]["Col2"].Tostring();

}
 
Share this answer
 
ds.table[0].AsDataView().ToTable(false,string[] columnnames)
 
Share this answer
 
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select column_name from table_name", con);
DataSet ds = new DataSet();
da.Fill(ds);
 
Share this answer
 
string userid;
for (int i=0;i<=ds.tables[0].rows.count-1;i++)
{
userid=ds.Tables[0].Rows[i]["userid"].Tostring();
}
 
Share this answer
 
You are fetching a single column "userid" by your adapter:

SqlDataAdapter da = new SqlDataAdapter("select userid from user_detail", con);

If you want to use it somewhere then apply a foreach loop and fetch the "userid".

C#
foreach(DataRow row in ds.Tables[0].Rows) {
   int uid = Convert.ToInt(row["userid"]);
}
 
Share this answer
 
You can use a code like this:
C#
foreach(DataRow row in ds.Tables[0].Rows) {
   string col1 = row["Col1"].ToString();
}
 
Share this answer
 

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