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. Can't retrieve data out of a GridView Control

Can't retrieve data out of a GridView Control

Scheduled Pinned Locked Moved ASP.NET
databasecsharpcssasp-netsql-server
4 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.
  • Q Offline
    Q Offline
    Quecumber256
    wrote on last edited by
    #1

    I’m populating a GridView control on an ASP.NET web page using a SQL Server 2005 stored procedure. The following subroutine is how I populate the GridView Control: Protected Sub Load_grdEmployees() 'Purpose: Loads the grid control grdEmployees ' with employee information 'Parameters: None 'Returns: Nothing Dim dr As SqlDataReader Dim cn As SqlConnection = New SqlConnection(MRMFunctions.WebConfigConnString) cn.Open() Try Dim cd As SqlCommand = New SqlCommand("mrmsp_EmployeeList", cn) cd.CommandType = Data.CommandType.StoredProcedure dr = cd.ExecuteReader grdEmployees.DataSource = dr grdEmployees.DataBind() Catch ex As Exception Msg = ex.Message lblErr.Visible = True lblErr.ForeColor = Drawing.Color.Red lblErr.Font.Bold = True lblErr.Text = Msg Finally cn.Close() End Try End Sub The problem I’m having is I don’t know how to read the data stored in the first column on the GridView Control. I use the ID number stored in the first column to perform edits on selected records. I’m not using a SqlDataSource control on this web page. Does anyone have any ideas on how to do this? Thank you, Quecumber256

    M 1 Reply Last reply
    0
    • Q Quecumber256

      I’m populating a GridView control on an ASP.NET web page using a SQL Server 2005 stored procedure. The following subroutine is how I populate the GridView Control: Protected Sub Load_grdEmployees() 'Purpose: Loads the grid control grdEmployees ' with employee information 'Parameters: None 'Returns: Nothing Dim dr As SqlDataReader Dim cn As SqlConnection = New SqlConnection(MRMFunctions.WebConfigConnString) cn.Open() Try Dim cd As SqlCommand = New SqlCommand("mrmsp_EmployeeList", cn) cd.CommandType = Data.CommandType.StoredProcedure dr = cd.ExecuteReader grdEmployees.DataSource = dr grdEmployees.DataBind() Catch ex As Exception Msg = ex.Message lblErr.Visible = True lblErr.ForeColor = Drawing.Color.Red lblErr.Font.Bold = True lblErr.Text = Msg Finally cn.Close() End Try End Sub The problem I’m having is I don’t know how to read the data stored in the first column on the GridView Control. I use the ID number stored in the first column to perform edits on selected records. I’m not using a SqlDataSource control on this web page. Does anyone have any ideas on how to do this? Thank you, Quecumber256

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Quecumber256 wrote:

      The problem I’m having is I don’t know how to read the data stored in the first column on the GridView Control.

      + You can get reference to the first cell of a row to get the value. + Another option is to store the key values using the DataKeyNames[^] property and you can access the value from the DataKeys collection.

      Q 1 Reply Last reply
      0
      • M minhpc_bk

        Quecumber256 wrote:

        The problem I’m having is I don’t know how to read the data stored in the first column on the GridView Control.

        + You can get reference to the first cell of a row to get the value. + Another option is to store the key values using the DataKeyNames[^] property and you can access the value from the DataKeys collection.

        Q Offline
        Q Offline
        Quecumber256
        wrote on last edited by
        #3

        I didn't place a SqlDataSource Control on the web page. I think because I don't have this control on the page there are no references to the DataKeyNames and DataKeys properties. I spent the whole day trying to find examples on how to set these properties through code. However, everything I did find required that a SqlDataSource Control be present. My web page requires a greater amount of control than the SqlDataSource will allow. Thank you for your help, Quecumber256

        M 1 Reply Last reply
        0
        • Q Quecumber256

          I didn't place a SqlDataSource Control on the web page. I think because I don't have this control on the page there are no references to the DataKeyNames and DataKeys properties. I spent the whole day trying to find examples on how to set these properties through code. However, everything I did find required that a SqlDataSource Control be present. My web page requires a greater amount of control than the SqlDataSource will allow. Thank you for your help, Quecumber256

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Actually, you can set the DataKeyNames property either in the control definition or in code, the values should be taken from the result data reader. For example:

          GridView1.DataKeyNames = new string[] { "Id" };

          ....

          protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
          {
          object key = GridView1.DataKeys[e.NewEditIndex].Value;
          }

          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