Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

GridView OnRowDataBound

Posted on 2010-08-31
10
Medium Priority
?
900 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

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.

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

636 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