1

I have a data binding method to bind a gridview to the database.

 private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    con.Open();
    SqlCommand cmd = new SqlCommand("select bookingId, bookingName, fprice from addCart");

    SqlDataAdapter sda = new SqlDataAdapter();

    cmd.Connection = con;
    sda.SelectCommand = cmd;
    using (DataTable dt = new DataTable())
    {
    sda.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    con.Close();            
} 

As I had previously used a base64 encoding method to encode the bookingId and bookingName, the data displayed in the gridview shows only the encoded values. How do I add in a base64 decoding method such that it displays decoded value in the gridview?

How I used my encoding:

byte[] serviceEncode = System.Text.Encoding.Unicode.GetBytes(serviceLabel.Text);

sqlCmd.Parameters.AddWithValue("@bookingName", SqlDbType.VarChar).Value = Convert.ToBase64String(serviceEncode);

Database value: QgBpAGMAeQBjAGwAZQAmACMAMwAyADsA

I had previously used an html decoding method in my item template. I am wondering if there is any similar known method to decode using base64?

 <ItemTemplate>
 <asp:Label ID="Label1" runat="server" decoding="base64" Text='<%# HttpUtility.HtmlDecode(Eval("bookingName").ToString()) %>'></asp:Label>                   
 </ItemTemplate>
depressedGirl
  • 127
  • 1
  • 13
  • A) You can iterate over the rows of the datatable and set the Base64 decoded values in a new column, before setting the datasource. (B) You can use the row data found event of the grid view to decode the data. – Nisarg Shah Feb 02 '18 at 06:18
  • can I have the exact codes on how to do it? I am quite new to this. @NisargShah – depressedGirl Feb 02 '18 at 06:23
  • change `<%# HttpUtility.HtmlDecode(Eval("bookingName").ToString()) %>` to `<%# System.Text.Encoding.Unicode.GetString(Convert.FromBase64String(Eval("bookingName").ToString()))%>` – levent Feb 02 '18 at 06:47
  • I get this error: : Invalid length for a Base-64 char array or string. after making the above changes @levent – depressedGirl Feb 02 '18 at 06:53

3 Answers3

0

You can use this function :

base64_decode('QgBpAGMAeQBjAGwAZQAmACMAMwAyADsA');

Replace this:

string newbookingname = Encoding.UTF8.GetString(Convert.FromBase64String(dr["bookingName"].ToString()));

With this:

string textDecoded = System.Text.Encoding.UTF8.DecodeBase64(<"Your encoded string">);

you can refer this for that error : How do I encode and decode a base64 string?

SSD
  • 1,373
  • 2
  • 13
  • 20
0

Run a foreach inside the Datatable and update the rows with decoded values

string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("select bookingId, bookingName, fprice from addCart");

SqlDataAdapter sda = new SqlDataAdapter();

cmd.Connection = con;
sda.SelectCommand = cmd;
         using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    foreach (DataRow dr in dt.Rows)
                    {
                        //decode the bookingid and bookingname and update in table
                       if(dr["bookingName"].ToString()!=""){
                        string newbookingname = "";
                    try { 
                    newbookingname = System.Text.Encoding.Unicode.GetString(Convert.FromBase64String(dr["bookingName"].ToString()));
                    }
                    catch(Exception e)
                    {
                        newbookingname = dr["bookingName"].ToString();
                    }
                    dr["bookingName"] = newbookingname;
                       }
                    }

                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
con.Close();  

and in gridview

    <ItemTemplate>
 <asp:Label ID="Label1" runat="server"  Text='<%# Eval("bookingName") %>'></asp:Label>                   
 </ItemTemplate>
shubham
  • 771
  • 7
  • 18
  • May i know what does the Tables in dt.Tables[0].Rows stand for? I get an error when I copied that over into my codes @shubham – depressedGirl Feb 02 '18 at 06:27
  • sorry, it should be just dt.Rows – shubham Feb 02 '18 at 06:28
  • Ok, I received no more error, however I got an error at the dr["bookingId"] part. @shubham the error says cannot convert from object to string – depressedGirl Feb 02 '18 at 06:32
  • sda.Fill(dt); this will be above the foreach. Please check code now – shubham Feb 02 '18 at 06:36
  • I have made the following amendments. Now the new error says: System.FormatException: Invalid length for a Base-64 char array or string, at the line with the encoding method@shubham – depressedGirl Feb 02 '18 at 06:41
  • "I had previously used a base64 encoding method to encode the bookingId and bookingName" , are you sure you encoded both bookingId and bookingName and not just bookingName? – shubham Feb 02 '18 at 06:43
  • wait sorry i did not encode the bookingId, only the bookingName. I had commented out the bookingId line and renamed the second dr["bookingId"] to bookingName @shubham – depressedGirl Feb 02 '18 at 06:46
  • that was what i had done and I still got that error @shubham – depressedGirl Feb 02 '18 at 06:54
  • You might have to check your addCart table if there aren't any nulls or empty in bookingName field. – shubham Feb 02 '18 at 07:03
  • Im very sure there are no null values @shubham – depressedGirl Feb 02 '18 at 07:07
  • its working for me, and the string you gave in your question is returning Bicycle . made changes, can check again – shubham Feb 02 '18 at 07:09
  • I got the same error even though I added in the changes. @shubham – depressedGirl Feb 02 '18 at 07:12
  • Convert.FromBase64String(dr["bookingName"].ToString()); instead of this, write Convert.FromBase64String("QgBpAGMAeQBjAGwAZQAmACMAMwAyADsA"); so you can be sure if its a problem with table data – shubham Feb 02 '18 at 07:16
  • It still shows the encoded value, however I dont get any error, but it replace all my other values to Bicycle too@shubham – depressedGirl Feb 02 '18 at 07:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164385/discussion-between-shubham-and-depressedgirl). – shubham Feb 02 '18 at 07:21
0

~this answer dose not have direct anwser it's just some recommend~

I've got this problem months ago , and i used to convert data first and then use it.

Recommend you to use Entity Framework.

It's more flexible than ADO. Then u can use GUID type for ID's and its really easy to be shown any where.

For using EF , its better to use Database first then coding , by this technology you can create your database inside Visual studio and then EF can make models for you and BOOM all your hard codings are dismissed

And by using controllers you can easily get and set data's from database

Syntax
  • 137
  • 1
  • 6
  • I am very new to all these and I dont get what you mean by ADO, EF Entity Framework. But thanks for your suggestion @A. Razinejad – depressedGirl Feb 02 '18 at 07:30