Monday, May 8, 2017

Read value from database using dataset with StoredProcedure and bind in GridView.

Introduction:
In this article I will explain how to bind gridview with datareader in asp.net using C#.net

Implement 

Open Your IDE
 File ----New----Project



Click on project----Web

 
change Name What you want---Click Ok Button



Click on Default.aspx page and Drag the gridview from Toolbox.




The connection string in web.config file 
e.g:-
<connectionStrings>
    <add name ="DBM" connectionString ="Data Source=your system name;Initial Catalog=your database name;Integrated Security=True"/>
  </connectionStrings>

Note:- I am using window authentication  so using Integrated Security=True;

Add the following namespace:-

Using Sysytem.Data;Using System.Data.SqlClient; Know time to come you  write code in Page_load
        string strcon = System.Configuration.ConfigurationManager.ConnectionStrings["DBM"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
if(!IsPostback){
            BindGridView();
}
        }
        public void BindGridView()
        {
            //connect to database via dataset & dataadapter
            SqlConnection con = new SqlConnection("server=localhost;Initial Catalog=databasename;uid=;pwd=;");
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("GetstudentnameInOutputVariable", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.Fill(ds);
          gridview1.DataSource =ds;
gridview1.DataBind();      
        }
       
 


------------------Thank you-------------------------

















No comments:

Post a Comment