For smart Primates & ROBOTS (oh and ALIENS ).

Showing posts with label itextSharp C#. Show all posts
Showing posts with label itextSharp C#. Show all posts

Tuesday, June 28, 2016

Object must be of type String while sorting datagridview

Object must be of type String

You are working with DataGridView with  several columns. You want to make sort of one column name "marks". then you will simply use below one line of code:

dataGridView1.Sort(dataGridView1.Columns["marks"], ListSortDirection.Ascending); 


But you may get error message as below:

System.ArgumentException was unhandled
  Message="Object must be of type String."
  Source="mscorlib"
  StackTrace:
       at System.String.CompareTo(Object value)
       at System.Collections.Comparer.Compare(Object a, Object b)
       at 
       .......................................
       .......................................
       at System.Threading.ThreadHelper.ThreadStart()

  InnerException: 

Let me try to understand you why the error message "Object must be of type String." You are getting when sorting "marks".

Very first time You are getting data from database and storing in the your datagridview something like this, note that string marks:

string marks = ds.Tables[0].Rows[grid_sr_no]["marks"].ToString();
dataGridView1.Rows[i].Cells["marks"].Value = mark;


after some processing in the seconds time you are string marks as intereger like this:

int marks = Convert.ToInt32(ds1.Tables[0].Rows[0]["marks"].ToString());
dataGridView1.Rows[i].Cells["marks"].Value = marks;


Then after if you use sorting code as below:
dataGridView1.Sort(dataGridView1.Columns["marks"], ListSortDirection.Ascending); 

You will get error message:
System.ArgumentException was unhandled
 Message="Object must be of type String."


so, to overcome this problem only use similar datatype while storing in datagridview.
alternatively you can use below code:
dataGridView1.Rows[i].Cells["marks"].Value = marks.ToString();






Share:

Sunday, January 04, 2015

itextSharp.text.pdf.badpasswordException PdfReader not opened with owner password

Problem:

If you are using iTestShap in Dot net application and generating PDF with owner passward. After that you are trying to open that password protected in again in Dot net code then you may face the error or exception of itextSharp.text.pdf.badpasswordException PdfReader not opened with owner password.

Solution:

This is because you are not supplying the password while you opening that pdf file in itextsharp using PdfReader.

Example:

Creating Pdf file using owner password:

PdfWriter docWriter = PdfWriter.GetInstance(doc, new FileStream(pdfFileName, FileMode.Create));
byte[] OWNER = System.Text.Encoding.UTF8.GetBytes("mypassword");
docWriter.SetEncryption(null, OWNER, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);

Now you are trying to open that file:

PdfReader pdfReader = new PdfReader(pdfFileName);

Correct code:

byte[] OWNER = System.Text.Encoding.UTF8.GetBytes("mypassword");
PdfReader pdfReader = new PdfReader(pdfFileName, OWNER);

So conclusion is that be sure you are supplying the password while opening the password protected file in PdfReader in C# or VB.net code:
Share:

Ads Inside Post

Powered by Blogger.

Archive