.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to Fill a Gridview using DataReader object?

Posted By:Syed Shakeer Hussain       Posted Date: August 22, 2009    Points: 10    Category: ADO.NET    URL: http://www.dotnetspark.com  
 

How to Fill a Gridview using DataReader object?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Web.Configuration;



public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{



}



protected void Show_Data(String strssearch)

{

String strcon = WebConfigurationManager.ConnectionStrings["MYSTRING"].ConnectionString;



SqlConnection con = new SqlConnection(strcon);



String tsql = "select * from dbo.memyself where fname = '" + strssearch + "'";



SqlCommand cmd = new SqlCommand(tsql, con);



con.Open();



SqlDataReader reader = cmd.ExecuteReader();



GridView1.DataSource = reader;



GridView1.DataBind();



reader.Close();



con.Close();



}

protected void Button1_Click(object sender, EventArgs e)

    {

       

        Show_Data(txtsearch.Text);

    }

}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend