Solved

GridView OnRowDataBound

Posted on 2010-08-31
10
850 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert string to date 3 40
Why, and when, to use Windows Workflow 1 20
Showdialog 8 21
Visual Studio editor? 1 18
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now