Thursday, March 15, 2012

Azadeh's really easy grab-everything-from-a-stored-procedure script

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.Configuration;

using System.Collections;

using System.Web.Security;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

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

{

   protected void Page_Load(object sender, EventArgs e)

   {

      BindGrid();

   }

   

   public static string getConnectionString()

   {

      return ConfigurationManager.ConnectionStrings["foo"].ConnectionString;

   }

   

   public DataSet GetAllUserInfo()

   {

      SqlConnection conn = new SqlConnection(getConnectionString());

      SqlCommand cmd = new SqlCommand();

      SqlDataAdapter adapter = new SqlDataAdapter();

      DataSet dsInfo = new DataSet();

      string query = "dbo.spUser";

      try

      {

         cmd.CommandText = query;

         cmd.CommandType = CommandType.StoredProcedure;

         cmd.Connection = conn;

         adapter.SelectCommand = cmd;

         adapter.Fill(dsInfo);

         return dsInfo;

      }

      catch (Exception ex)

      {

         System.Web.HttpContext.Current.Response.Write(ex.Message);

         return null;

      }

      finally

      {

      }

   }

   

   private void BindGrid()

   {

      DataSet dsMembers = new DataSet();

      dsMembers = GetAllUserInfo();

      Grid.AutoGenerateColumns = true;

      Grid.DataSource = dsMembers;

      Grid.DataBind();

   }

}

No comments:

Post a Comment