Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

How to use a selected value from GridView


I have a GridView which is populated from a select statement. My problem is that i need to grab hold of a column from within the GridView to use in  a select statement. I have a button on the GridView which i want the user to click. and on the click event of that button it needs to grab the ID of the selected row and pass that value to my statement.

Currently the only way i can get it to half work is to hard code the event with an id.

any ideas?
1 Solution
leap29Author Commented:
Sorry neither of these are much help. could either of you explain?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

IF you can give the snippet what you have tried. we can really help you
leap29Author Commented:
hi , thanks

Below is the code for my gridview. basically i have a button on the gridview that when it is clicked runs an event in the code behind which runs a sql statement to go and fetch a word doc from the sql server based upon the ID of the row that was clicked.

I have tried to use DataKeys but im not entirely sure im going about it in the correct manor.


<asp:GridView ID="GridView1"  runat="server" 
            DataSourceID="SqlDataSource1" DataKeyNames="docid" onselectedindexchanged="GridView1_SelectedIndexChanged1" >
 <asp:TemplateField HeaderText="Show CV" ShowHeader="False">
 <asp:Button runat="server" Text="Show" CommandName="Select" CausesValidation="False" id="Button3"  OnClick="GridView1_SelectedIndexChanged"  />
 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        System.Data.SqlClient.SqlConnection objConn = new System.Data.SqlClient.SqlConnection();
        objConn.ConnectionString = "Data Source=LOCAL;Password=PASSWORD;User ID=USERNAME;Initial Catalog=NAME OF DB";
        System.Data.SqlClient.SqlCommand objCmd = new System.Data.SqlClient.SqlCommand("NAME OF MY SP", objConn);
        objCmd.CommandType = System.Data.CommandType.StoredProcedure;
       System.Data.SqlClient.SqlParameter test = objCmd.Parameters.Add("@docid",System.Data.SqlDbType.Char);
       test.Direction = System.Data.ParameterDirection.Input;
       test.Value = GridView1.DataKeys[0].Value;
    // GridView1.datakey(Index).(datakeyname value docid).Tostring;
        // Request.QueryString["docid"].ToString(); 
        //string docuid = "@docid";
        //Connection and Parameters
        SqlParameter param = null;
        SqlDataReader reader =
        if (reader.HasRows)
            string doctype = reader["doctype"].ToString();
            string docname = reader["docname"].ToString();
            Response.Buffer = false;
            Response.ContentType = doctype;
                     "attachment; filename=" + docname);
            //Code for streaming the object while writing
            const int ChunkSize = 1024;
            byte[] buffer = new byte[ChunkSize];
            byte[] binary = (reader["document"]) as byte[];
            MemoryStream ms = new MemoryStream(binary);
            int SizeToWrite = ChunkSize;
            for (int i = 0; i < binary.GetUpperBound(0) - 1; i = i + ChunkSize)
                if (!Response.IsClientConnected) return;
                if (i + ChunkSize >= binary.Length)
                    SizeToWrite = binary.Length - i;
                byte[] chunk = new byte[SizeToWrite];
                ms.Read(chunk, 0, SizeToWrite);

Open in new window

Rupesh PCommented:
The ID you meant is the ID gets from the database rt?

See the following line with the CommandArgument property.

<asp:Button runat="server" Text="Show" CommandName="Select" CausesValidation="False" id="Button3"   CommandArgument='<%# Eval("Id") %>'  OnClick="GridView1_SelectedIndexChanged"  />

In the GridView1_SelectedIndexChanged
use like
Int Id = Convert.ToInt32(((Button)sender).CommandArgument);

leap29Author Commented:
hi, thanks for this. i getting the following error

The type or namespace name 'Int' could not be found (are you missing a using directive or an assembly reference?)
leap29Author Commented:
sorry actual error is

System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now