?
Solved

GridView OnRowDataBound

Posted on 2010-08-31
10
Medium Priority
?
891 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 6

Accepted Solution

by:
Aksh earned 1000 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

800 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