Solved

GridView OnRowDataBound

Posted on 2010-08-31
10
870 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 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