Had a doubt with DataReader? [Resolved]

Posted by Rajeshk under ASP.NET on 1/5/2013 | Points: 10 | Views : 1042 | Status : [Member] | Replies : 8
hi all,

i heard that datareader is sequential and we can read data in forword direction only but i need a requirement like if i want to read all the table data at a time from db using datareader and bind with gridview to display data what i need to do plz give me an example with your explanation?

Thanks&Regards
rajeshkommireddy@gmail.com



Responses

Posted by: muralikrishnasurap-12704 on: 1/8/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi Rajeshk
try this part of code
SqlCommand cmd = new SqlCommand("SQl Query", SQlConnectionObject);
SqlDataReader dr = cmd.ExecuteReader();
gridview.DataSource = dr;
gridview.DataBind();

Best Regards
Murali Krishna.S

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

Posted by: Saratvaddilli on: 1/8/2013 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
Hi rajesh ,
Murali krishna answer was good apart from that we trythis also

 string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "Your Query";
cmd.Connection = con;
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}


Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

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

Posted by: Saratvaddilli on: 1/6/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
make me clear one thing,
you want to get data from db and bind using datareader or you just want to filter and bind to gridview

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

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

Posted by: Elephant12 on: 1/8/2013 [Member] Starter | Points: 25

Up
0
Down
Datareader is to support the SetPosition/GetPosition interface and returning UMC_ERR_FAILED is not an option any more.

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

Posted by: Rajeshk on: 1/8/2013 [Member] Starter | Points: 25

Up
0
Down
i want to display the whole table data in gridview using DataReader(simply it has to read entire table but not one by one records)

Thanks&Regards
rajeshkommireddy@gmail.com

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

Posted by: Rajeshk on: 1/9/2013 [Member] Starter | Points: 25

Up
0
Down
hi ,
onething guys will it work with both windows&web apps?

Thanks&Regards
rajeshkommireddy@gmail.com

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

Posted by: Saratvaddilli on: 1/9/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
ya with out fail

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

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

Posted by: Satyapriyanayak on: 1/23/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="Button3" runat="server" onclick="Button3_Click"
Text="Using Datareader" />
</form>
</body>
</html>


using System;
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.OleDb;
public partial class _Default : System.Web.UI.Page
{
OleDbConnection con;
OleDbCommand com;
OleDbDataAdapter oda;
DataSet ds;
DataTable dataTable;


protected void Button3_Click(object sender, EventArgs e)
{
con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\\mydb.mdb");
com = new OleDbCommand("Select * from emp", con);
con.Open();
OleDbDataReader reader;
reader = com.ExecuteReader();
GridView1.DataMember = "emp";
GridView1.DataSource = reader;
GridView1.DataBind();
reader.Close();
con.Close();
}
}


If this post helps you mark it as answer
Thanks

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

Login to post response