Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. populate GridView with stored procedure

populate GridView with stored procedure

Scheduled Pinned Locked Moved ASP.NET
databasehelptutorial
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    foryou
    wrote on last edited by
    #1

    HI! I do not know how to populate my GridView with stored procedure I have this code but it doesn't works she returned result or error message

    public void BindData()
    {
    SqlConnection myConnection = new SqlConnection(ConnectionString);

        SqlCommand myCommand = new SqlCommand("PS\_NV2", myConnection);
        myCommand.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
        DataSet ds = new DataSet();
        myAdapter.Fill(ds, "TAble");
        myConnection.Open();
        myCommand.ExecuteNonQuery();
        GridView2.DataSource = ds;
        try
        {
           GridView2.DataBind();
        }
        catch
        {
            GridView2.PageIndex = -1;
            GridView2.DataBind();
        }
        myConnection.Close();
    

    correct me please.Thanks

    S 1 Reply Last reply
    0
    • F foryou

      HI! I do not know how to populate my GridView with stored procedure I have this code but it doesn't works she returned result or error message

      public void BindData()
      {
      SqlConnection myConnection = new SqlConnection(ConnectionString);

          SqlCommand myCommand = new SqlCommand("PS\_NV2", myConnection);
          myCommand.CommandType = CommandType.StoredProcedure;
          SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
          DataSet ds = new DataSet();
          myAdapter.Fill(ds, "TAble");
          myConnection.Open();
          myCommand.ExecuteNonQuery();
          GridView2.DataSource = ds;
          try
          {
             GridView2.DataBind();
          }
          catch
          {
              GridView2.PageIndex = -1;
              GridView2.DataBind();
          }
          myConnection.Close();
      

      correct me please.Thanks

      S Offline
      S Offline
      Satish Mahapatra
      wrote on last edited by
      #2

      execute the executenonquery before putting into adapter.

      F 1 Reply Last reply
      0
      • S Satish Mahapatra

        execute the executenonquery before putting into adapter.

        F Offline
        F Offline
        foryou
        wrote on last edited by
        #3

        Thank you Satish Mahapatra. it doesn't works :( this code works fine for a datagrid but not for the GridView I do not know if the error can come from here for datagrid i have DataGrid1.CurrentPageIndex = 0; I have replaced by : GridView2.PageIndex =0 ; thanks.

        S 1 Reply Last reply
        0
        • F foryou

          Thank you Satish Mahapatra. it doesn't works :( this code works fine for a datagrid but not for the GridView I do not know if the error can come from here for datagrid i have DataGrid1.CurrentPageIndex = 0; I have replaced by : GridView2.PageIndex =0 ; thanks.

          S Offline
          S Offline
          Satish Mahapatra
          wrote on last edited by
          #4

          can u send me whole code and where exactly ur getting the error with error description.

          F 1 Reply Last reply
          0
          • S Satish Mahapatra

            can u send me whole code and where exactly ur getting the error with error description.

            F Offline
            F Offline
            foryou
            wrote on last edited by
            #5

            I have no erreur no message erreur if i click sur the button I havent result. my code c#: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; private void Page_Load(object sender, System.EventArgs e) { } public void BindData() { SqlConnection myConnection = new SqlConnection(ConnectionString); SqlCommand myCommand = new SqlCommand("PS_NV2", myConnection); myCommand.CommandType = CommandType.StoredProcedure; SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); DataSet ds = new DataSet(); myAdapter.Fill(ds, "table"); myConnection.Open(); myCommand.ExecuteNonQuery(); GridView2.DataSource = ds; try { GridView2.DataBind(); } catch { GridView2.PageIndex = 0; GridView2.DataBind(); } myConnection.Close(); } private string ConnectionString { get { string connectionString = (@"Data Source=.\SQLEXPRESS;UID=sa;Password=**3*;Initial Catalog=mybd"); return connectionString; } } protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { Response.Write(e.NewSelectedIndex); } protected void GridView2_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow row = GridView2.SelectedRow; TextBox4.Text = row.Cells[2].Text; } protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e) { GridView2.EditIndex = e.NewEditIndex; BindData(); } protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView2.EditIndex = -1; BindData(); } protected void Button1_Click1(object sender, EventArgs e) { BindData(); GridView2.Visible = true; } } DataKeyNames="c1" OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowEditing="GridView2_RowEditing" OnSelectedIndexChanged="GridView2_SelectedIndexChan

            S 1 Reply Last reply
            0
            • F foryou

              I have no erreur no message erreur if i click sur the button I havent result. my code c#: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; private void Page_Load(object sender, System.EventArgs e) { } public void BindData() { SqlConnection myConnection = new SqlConnection(ConnectionString); SqlCommand myCommand = new SqlCommand("PS_NV2", myConnection); myCommand.CommandType = CommandType.StoredProcedure; SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); DataSet ds = new DataSet(); myAdapter.Fill(ds, "table"); myConnection.Open(); myCommand.ExecuteNonQuery(); GridView2.DataSource = ds; try { GridView2.DataBind(); } catch { GridView2.PageIndex = 0; GridView2.DataBind(); } myConnection.Close(); } private string ConnectionString { get { string connectionString = (@"Data Source=.\SQLEXPRESS;UID=sa;Password=**3*;Initial Catalog=mybd"); return connectionString; } } protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { Response.Write(e.NewSelectedIndex); } protected void GridView2_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow row = GridView2.SelectedRow; TextBox4.Text = row.Cells[2].Text; } protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e) { GridView2.EditIndex = e.NewEditIndex; BindData(); } protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView2.EditIndex = -1; BindData(); } protected void Button1_Click1(object sender, EventArgs e) { BindData(); GridView2.Visible = true; } } DataKeyNames="c1" OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowEditing="GridView2_RowEditing" OnSelectedIndexChanged="GridView2_SelectedIndexChan

              S Offline
              S Offline
              Satish Mahapatra
              wrote on last edited by
              #6

              i told u to put that executenonquery statement before putting into adaptare.now u just debug the code and see in the binddata method what it returns in the dataset and tell me.

              F 1 Reply Last reply
              0
              • S Satish Mahapatra

                i told u to put that executenonquery statement before putting into adaptare.now u just debug the code and see in the binddata method what it returns in the dataset and tell me.

                F Offline
                F Offline
                foryou
                wrote on last edited by
                #7

                Thank you Satish Mahapatra it works I forgot a field in my stored procedure.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups