Search content within the blog

Wednesday, November 19, 2008

Reading two Tables Data Using DataTableReader and NextResult

ASPX Page
<asp:GridView runat="server" ID="gvReaderData" AutoGenerateColumns="true"></asp:GridView>


protected void Page_Load(object sender, EventArgs e)
{

//fill first table
DataTable OrderDetails = new DataTable("OrderDetails");
String Sql = "Select top 5 email from Loobi_member";
SqlCommand cmd = new SqlCommand(Sql);
SqlConnection objConnection = new SqlConnection();
objConnection.ConnectionString = "Data Source=Loobi;Initial Catalog=Loobi;User ID=sa; Password=sa";
cmd.Connection = objConnection;
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(OrderDetails);

//fill second table
DataTable LoginDetails = new DataTable("LoginDetails");
String Sql1 = "Select top 5 loginid from Loobi_Login";
SqlCommand cmd1 = new SqlCommand(Sql1);
SqlConnection objConnection1 = new SqlConnection();
objConnection1.ConnectionString = "Data Source=Loobi;Initial Catalog=Loobi;User ID=sa; Password=sa";
cmd1.Connection = objConnection1;
SqlDataAdapter adap1 = new SqlDataAdapter(cmd1);
adap1.Fill(LoginDetails);

DataTableReader objReader=new DataTableReader(new DataTable[]{OrderDetails,LoginDetails});
if (objReader.HasRows)
{
do
{
Response.Write("<br>");
Response.Write("Reading Table contents Started");
Response.Write("<br>");
while (objReader.Read())
{
Response.Write(objReader[0].ToString());
}
Response.Write("<br>");
Response.Write("Table contents completed");
Response.Write("<br>");
} while (objReader.NextResult());

}
else
{
Response.Write("No Data");
}

//Binding Data To Grid named gvReaderData
DataTableReader objEmailsReader = new DataTableReader(OrderDetails);
DataTable tblEmails = new DataTable();
tblEmails.Load(objEmailsReader);
gvReaderData.DataSource = tblEmails;
gvReaderData.DataBind();



}

No comments:

Post a Comment