0

I am fetching Two Columns from database, one column has data type as varbinary. I am converting the varbinary column to byte[] and then to string. Code is working for console application and keeping the string format. But when i am using the same code in Web Application and displaying the data in GridView, it losing the format. For Ex:

Hello

World

will be displaying as Hello World in Grid View. Here is my code :

    SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=legal; Integrated Security=True");
    SqlCommand cmd = new SqlCommand("usp_output_repayment  @id=1", con);

    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Columns.Add("Category");
    dt.Columns.Add("Description");

    while (dr.Read())
    {
    DataRow dr1 = dt.NewRow();

        dr1["Category"] = dr[0];
        dr1["Description"] = System.Text.ASCIIEncoding.ASCII.GetString((byte[])dr[1]).Normalize();
        dt.Rows.Add(dr1);


    }

    GridView1.DataSource = dt;
    GridView1.DataBind();
7
  • 2
    What are you targetting: Winforms, WPF, ASP..? You should always TAG your questions correctly so one can see it on the questions page! - Also note that DataGridView != GridView Commented Aug 16, 2018 at 20:08
  • @TaW should ASP.NET since OP says i am using the same code in Web Application and displaying the data in GridView. Moreover there is no DataBind() in Winform Commented Aug 16, 2018 at 20:10
  • how about your GridView1 Commented Aug 16, 2018 at 20:10
  • In a web page you should use br tag for a newline. Commented Aug 16, 2018 at 20:10
  • @Rahul: This information should be in a Tag and not hidden in the question! Commented Aug 16, 2018 at 20:10

1 Answer 1

1

When you are working on the asp.net side then you should use DataBound inside the gridview as well as setting AutoGenerateColumns="false"

Now it's my main point of view if you are getting description of whatever format as well then use HtmlEncode="false" for your column that you used

Few Code of GridView,that you will have to utilize as such format described.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Description" HeaderText="Description" HtmlEncode="false" />
        </Columns>
    </asp:GridView>

Error format that I could faced using HtmlEncode="true" or removing it

enter image description here

Tested and got well using HtmlEncode="false" upon following result

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Ali, thanks for the answer. did you make any changes in your code as well?
Don't add columns through code make a list or what you think is better but solution provided as well for your problem using HtmlEncode
it worked for me, With you code and little modification in code as as well, Thanks. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.