Fill Dataset with SQL Data in C#.html
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
string ConnectionStringFromWebConfig = @"" + ConfigurationManager.ConnectionStrings["CMSConnectionString"].ConnectionString + "";
SqlConnection cn = new SqlConnection(ConnectionStringFromWebConfig);
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM categories", cn); // create data adapter
DataSet ds = new DataSet();
da.Fill(ds); //fill the dataset with data from database
if (ds.Tables[0].Rows.Count != 0)//Checks if it has any data or not
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)//creates a loop
{
Response.Write("<br />");
Response.Write(ds.Tables[0].Rows[i]["UserName"].ToString());
Response.Write(ds.Tables[0].Rows[i][0].ToString());
Response.Write(ds.Tables[0].Rows[i][1].ToString());
Response.Write(ds.Tables[0].Rows[i][2].ToString());
}
}
else //if no data found in dataset
{
Response.Write("No Data Found");
}
//You can also bind the data to a GridView
GridView1.DataSource = ds; //bind dataset to the grid view in code behind file
GridView1.DataBind();
//<asp:GridView ID="GridView1" runat="server"></asp:GridView>//add a gridview code in aspx or ascx file.