Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My question is that i want to mask some numbers of credit card number which is of 16 digits.The positions of the numbers which i want to mask are from 7th to 12th the first six numbers and the last four numbers are not masked.The credit card numbers are being stored in boundfield and are of string type.Then i am exporting the web page data into an excel sheet.The process of masking which i want to perform is after the file is exported meaning the card number should show original on web page but when i will download it excel sheet the card number should be masked.I am using Oracle database, Microsoft Visual Studio 2008 as IDE and C# ASP.NET framework 3.5. Thanks in advance and sorry for the question's length.



C#
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    
 protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
protected void BindGridview()
{
DataSet ds = new DataSet();
{
con.Open();
OracleCommand cmd = new OracleCommand("select * from CAPTURED_CARD_REPORTING where CARD_NO is not null", con);
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(ds);
con.Close();
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
}
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && gvDetails.EditIndex == e.Row.RowIndex)
{
{
con.Open();
DropDownList ddlcard = (DropDownList)e.Row.FindControl("ddlreturn_to_customer");
con.Close();
ddlcard.Items.Insert(0, new ListItem("--Select--","-1"));
ddlcard.Items.Insert(1, new ListItem("Yes","Y"));
ddlcard.Items.Insert(2, new ListItem("No","N"));
}
}
}
protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvDetails.EditIndex = e.NewEditIndex;
BindGridview();
}
protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvDetails.EditIndex = -1;
BindGridview();
}
protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvDetails.PageIndex = e.NewPageIndex;
BindGridview();
}
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    
{
string card_no = Convert.ToString(gvDetails.DataKeys[e.RowIndex].Value.ToString());
string branch_code = Convert.ToString(gvDetails.Rows[e.RowIndex].Cells[1].Text);
DropDownList ddlreturn_to_customer = (DropDownList)gvDetails.Rows[e.RowIndex].FindControl("ddlreturn_to_customer");
con.Open();
OracleCommand cmd = new OracleCommand("update CAPTURED_CARD_REPORTING set RETURN_TO_CUSTOMER='" + ddlreturn_to_customer.SelectedValue + "' where CARD_NO='" + card_no + "'  and BRANCH_CODE='" + branch_code + "' ", con);
cmd.ExecuteNonQuery();
lblresult.ForeColor = Color.Green;
lblresult.Text = " Details Updated successfully";
gvDetails.EditIndex = -1;
BindGridview();
con.Close();
}
}
protected DataSet BindDatatable()
{
    OracleDataAdapter adapter = new OracleDataAdapter();
    DataSet ds = new DataSet();
    string oracle = null;
    oracle = "select CARD_NO , BRANCH_CODE , BANK_NAME , RETURN_TO_CUSTOMER from CAPTURED_CARD_REPORTING where CARD_NO is not null";
    OracleConnection connection = new OracleConnection(connetionString);
    connection.Open();
    OracleCommand command = new OracleCommand(oracle, connection);
    adapter.SelectCommand = command;
    adapter.Fill(ds);
    connection.Close();
    gvDetails.DataSource = ds.Tables[0];
    gvDetails.DataBind();
    return ds;
}
public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}
protected void ExportFile(string fileName, string contentType)
{
    //disable paging to export all data and make sure to bind griddata before begin
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
    Response.ContentType = contentType;
    StringWriter objSW = new StringWriter();
    HtmlTextWriter objTW = new HtmlTextWriter(objSW);
    gvDetails.AllowPaging = false;
    BindDatatable();
    gvDetails.Columns[4].Visible = false;
    gvDetails.RenderControl(objTW);
    Response.Write(objSW);
    Response.End();
}
protected void btnExport_Click(object sender, EventArgs e)
{
    string fileName = "ExportToExcel_" + DateTime.Now.ToShortDateString() + ".xls",
    contentType = "application/vnd.ms-excel";

    //call 1st export method with fileName and contentType
    ExportFile(fileName, contentType);

}

protected void gvDetails_SelectedIndexChanged(object sender, EventArgs e)
{

}
}
Posted
Updated 9-Jul-15 22:09pm
v6
Comments
Wendelius 10-Jul-15 2:10am    
When posting questions, never reveal user id's and passwords. I removed them from the post. Actually I'd remove the server names also if I were you!

1 solution

Hi,

In your code, while creating the stream writer,
StringWriter objSW = new StringWriter();
   HtmlTextWriter objTW = new HtmlTextWriter(objSW);


That can be modified as

 using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw);
 
        //To Export all pages
        GridView1.AllowPaging = false;
        this.BindGrid();
 foreach (GridViewRow row in GridView1.Rows)
        {
//here go by the row index you want to modify the text.
}
//your remaining code
gvDetails.AllowPaging = false;
    BindDatatable();
    gvDetails.Columns[4].Visible = false;
    gvDetails.RenderControl(objTW);
    Response.Write(objSW);
    Response.End();
}


Or if looping through is much and you can make use of a excel template, then you can refer this article,

http://www.c-sharpcorner.com/UploadFile/pandeypradip/export-data-into-excel-in-a-pre-defined-template-using-strea/[^]

This is not tested.Its just to give you an idea.
Hope this helps.

Happy coding!
 
Share this answer
 
v2
Comments
Member 11826939 13-Jul-15 23:56pm    
I tried it but it didn't work but thankyou so much for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900