Error while converting into string


Way to deal with null exception while retrieving the value from grid view.


string str=dgvissue_master.Rows[i].Cells[0].Value.tostring();

where dgvissue_maste=Datagridview

Above Code work fine till threre is data in the cell.But it will fire an error when the data in the cell is empty. Now,How to deal with such kind of error?
Generally .Tostring does't handle the null exception at runtime.
So, in such case we have to use Convert.Tostring for handling of null exception.


string str=convert.tostring(dgvissue_master.Rows[i].Cells[0].Value);

Hope this will help you.


Comments

Author: asdfg25 Feb 2010 Member Level: Gold   Points : 1

obj.ToString() cant holds NULL value
where as Convert.ToString() Can allow NULL values

Author: anup choudhari25 Feb 2010 Member Level: Bronze   Points : 1

The basic difference between them is “Convert" function handles NULLS while
“ToString()" does not it will throw a NULL reference exception error. So as a good coding
practice using “convert" is always safe.

Author: shashi shekhar02 Apr 2010 Member Level: Bronze   Points : 1


if(GridView1.Rows[i].cells[0].value!=null)
{
string value=GridView1.Rows[i].cells[0].value.ToString();
//string value=Convert.ToString(GridView1.Rows[i].cells[0].value);
}
dgvissue_master.Rows[i].Cells[0].Value

Author: anup choudhari02 Apr 2010 Member Level: Bronze   Points : 0

Shashi,
Can i help u???

Anup

Author: Naveen Kumar02 Apr 2010 Member Level: Gold   Points : 0

string str=convert.tostring(dgvissue_master.Rows[i].Cells[0].Value);

Author: nirav07 Apr 2010 Member Level: Gold   Points : 1

i hope below code line works fine for that problem


string ? str =dgvissue_master.Rows[i].Cells[0].Value.tostring();



where ? marks allowed null value also.....

from:nirav patel

Author: balu08 Apr 2010 Member Level: Bronze   Points : 1

string str=convert.tostring(dgvissue_master.Rows[i].Cells[0].Value.Tostring());

apply this like

Author: YuDi08 Apr 2010 Member Level: Gold   Points : 1

try like that


if(dgvissue_master.Rows[i].cells[0].value!=null)
{
string value=dgvissue_master.Rows[i].cells[0].value.ToString();
//string value=Convert.ToString(dgvissue_master.Rows[i].cells[0].value);
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: