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

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

How to use a selected value from GridView

Hi,

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?
0
leap29
Asked:
leap29
1 Solution
 
YogeshupCommented:
0
 
leap29Author Commented:
Sorry neither of these are much help. could either of you explain?
0
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.

 
pr_rCommented:
IF you can give the snippet what you have tried. we can really help you
0
 
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.

thanks

<asp:GridView ID="GridView1"  runat="server" 
            DataSourceID="SqlDataSource1" DataKeyNames="docid" onselectedindexchanged="GridView1_SelectedIndexChanged1" >
 <Columns>
 <asp:TemplateField HeaderText="Show CV" ShowHeader="False">
<ItemTemplate>
 <asp:Button runat="server" Text="Show" CommandName="Select" CausesValidation="False" id="Button3"  OnClick="GridView1_SelectedIndexChanged"  />
</ItemTemplate>                      
</asp:TemplateField>           
</Columns>
 </asp:GridView>
 
 
 
 
 
 
 
 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";
        objConn.Open();
 
        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 =
          objCmd.ExecuteReader(CommandBehavior.CloseConnection);
        if (reader.HasRows)
        {
            reader.Read();
            //
            string doctype = reader["doctype"].ToString();
            string docname = reader["docname"].ToString();
   
            //
            Response.Buffer = false;
            Response.ClearHeaders();
            Response.ContentType = doctype;
            Response.AddHeader("Content-Disposition",
                     "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);
                Response.BinaryWrite(chunk);
                Response.Flush();
            }
            Response.Close();
 
        }
    }

Open in new window

0
 
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);

0
 
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?)
0
 
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.
0

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