Solved

GridView OnRowDataBound

Posted on 2010-08-31
10
875 Views
Last Modified: 2012-05-10
Hello,

I'm trying to use the GridView OnRowDataBound Event in my CodeBehind but for some reason no data is getting added to my GridView control. I need a way to use the OnRowDataBound Event to Decrypt my data to the GridView control.

Is this possible if so can someone please show me or tell me what i"m missing in my code below??

Also, the Decryption does work when using a Label control to Decrypt to but I have 1000 + rows that I need to Decrypt and thought the GridView control would be best for that.

Thanks in advance!!!


HTML MARKUP:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="400px" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="admin_name" HeaderText="Name" />
                <asp:BoundField DataField="admin_address" HeaderText="Name" />
           </Columns>            
        </asp:GridView>



CODEBEHIND:

using EncryptionUtility.APress.ProAspNet.Utility;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Text;

public partial class retrievedata : System.Web.UI.Page
{
    private string EncryptionKeyFile;
    protected void Page_Load(object sender, EventArgs e)
    {
        EncryptionKeyFile = Server.MapPath("key.config");
        SymmetricEncryptionUtility.AlgorithmName = "Rijndael";

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Admissions"].ConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Admissions_RetrieveCollectedInfoValues";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        try
        {
            conn.Open();

            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            GridView1.DataSource = rdr;
            GridView1.DataBind();
        }

        catch
        {
        }

        finally
        {
            conn.Close();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.DataItem != null)
            {
                DataRowView dr = (DataRowView)e.Row.DataItem;

                byte[] Address = (byte[])dr["admin_address"];
                string decryptedData = SymmetricEncryptionUtility.DecryptData(Address, EncryptionKeyFile);
            }
        }
    }
}
0
Comment
Question by:asp_net2
  • 7
  • 3
10 Comments
 
LVL 6

Accepted Solution

by:
Aksh earned 500 total points
ID: 33570607
Hi,

1. You may remove the CommandBehavior.CloseConnection from the constructor of the ExecuteReader(...)
OR
Try using DataSet/DataAdapter instead of DataReader.

2. Make sure your Stored Procedure returns records containing admin_name, admin_address

Check following link:
http://www.eggheadcafe.com/community/aspnet/7/10005634/datasource-of-grid-view-is-data-from-datareader.aspx




0
 
LVL 4

Author Comment

by:asp_net2
ID: 33570663
Hi Aksh,

Thank you for responding first of all. I can REALLY use the help on this one.

1.) I did remove the "CommandBehavior.CloseConnection" from the constructor of the ExecureReader but nothing changed when doing so, is the code for the OnRowDataBound okay to use if this CommandBehavior.CloseConnection is removed??

I can try to implement a DataSet/DataAdapter rather than using the DataReader and will have this done within an hour from now, but do I need to change the OnRowDataBound code if I use a DataSet/DataAdapter??

My SP does return the values.
0
 
LVL 6

Expert Comment

by:Aksh
ID: 33570814
Hi,

The implementation of the DataRowBound is inappropriate.

You first fill the grid with the data then we will tweak to DataRowBound().
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 4

Author Comment

by:asp_net2
ID: 33570955
Hi Aksh,

Ok, I just updated my Page_Load code. Please see below.

private string EncryptionKeyFile;
    protected void Page_Load(object sender, EventArgs e)
    {
        EncryptionKeyFile = Server.MapPath("key.config");
        SymmetricEncryptionUtility.AlgorithmName = "Rijndael";

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Admissions"].ConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Admissions_RetrieveCollectedInfoValues";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        DataTable dt = new DataTable();

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        da.Fill(dt);

        try
        {
            conn.Open();

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        catch
        {
        }

        finally
        {
            conn.Close();
        }
    }
0
 
LVL 6

Expert Comment

by:Aksh
ID: 33571039
Are you able to see the data in the grid?
0
 
LVL 4

Author Comment

by:asp_net2
ID: 33572507
Yes and No. When I run the application I retrieve twol columns in the GridView column one is called admin_name which displays the names in that column correctly. The second column (admin_address) is what I'm having trouble with, each value is displaying "System.Byte[]" instead of decrypting the value.

My Database structure is below.

id   int
admin_name  varchar
admin_address  varbinary
0
 
LVL 4

Author Comment

by:asp_net2
ID: 33576918
Hi Aksh,

Are you able to help with the rest?
0
 
LVL 4

Author Comment

by:asp_net2
ID: 33585990
Hi Aksh,

Are you able to help??
0
 
LVL 4

Author Comment

by:asp_net2
ID: 33590380
Hi Aksh,

Please let me know if you can continue helping. I added the code that you asked but have not heard back from you.
0
 
LVL 4

Author Closing Comment

by:asp_net2
ID: 33678490
question was abandoned....
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question