how to retrive data from database into asp.net page
I desgin home page in asp.net, in home page i want to display some data which is stored in sql database. how can i retrive these data from database into my asp.net page.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Anuja PawarPosted Dec 27, 2011, 3:30 AM
<body>
DataSet Example
body>
Write this code behind
String ConnStr = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=java2suser;Password='password';";
String SQL = "SELECT ID, FirstName FROM Employee "
+ "WHERE ID IS NOT NULL";
SqlDataAdapter TitlesAdpt = new SqlDataAdapter(SQL, ConnStr);
DataSet Titles = new DataSet();
// No need to open or close the connection
// since the SqlDataAdapter will do this automatically.
TitlesAdpt.Fill(Titles);
Output.Text = "
foreach (DataRow Title in Titles.Tables[0].Rows)
{
Output.Text += "
Output.Text += "
Output.Text += "
+ "
Output.Text += "
}
Output.Text += "
Satyapriya NayakPosted Dec 27, 2011, 3:30 AM
With the help of Data control you can get the records.Here is an example to display records from database into Gridview.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Gridview_update_other_page
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void BindGrid()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select eid,ename,eaddress from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
con.Close();
ds = new DataSet();
sqlda.Fill(ds, "employee");
GridView1.DataSource = ds;
GridView1.DataMember = "employee";
GridView1.DataBind();
}
}
}
Thanks