Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am beginner in C# Windows Application.
I am saving data in sql server 2005.
I have a decimal column namely origrate in table.
I am picking data from datagridview cell.
To save data in table I convert type of datagridview cell value to decimal.
as like :

C#
decimal iorigrate=0M;
if(!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["origrate"].Value.ToString()))
   iorigrate =Convert.ToDecimal(dataGridView1.Rows[i].Cells["origrate"].Value);


but I have following errors

C#
NullReferenceException was unhandled


Kindly tell me where I am wrong.

Please help me.
Posted
Updated 6-Dec-11 15:36pm
v2
Comments
D K N T H 6-Dec-11 21:37pm    
added pre tag

You are converting dataGridView1.Rows[i].Cells["origrate"].Value to string by using ToString().
If the value is null, this will fail with the error you are getting.

Try checking for null before you apply ToString().
Debugging and stepping through code will give you an idea when you are getting this error.
 
Share this answer
 
v2
Change your code from

C#
decimal iorigrate=0M;
if(!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["origrate"].Value.ToString()))
   iorigrate =Convert.ToDecimal(dataGridView1.Rows[i].Cells["origrate"].Value);


to:
C#
decimal iorigrate=0M;
if(!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["origrate"].Value))
   iorigrate =Convert.ToDecimal(dataGridView1.Rows[i].Cells["origrate"].Value);


FYI, use Decimal.TryParse method instead, to prevent getting these kind of errors.

Here for your reference:
http://msdn.microsoft.com/en-us/library/system.decimal.tryparse.aspx[^]

Regards,
Eduard
 
Share this answer
 
see this codes:


C#
 foreach (GridViewRow gridviewrow in grvAuction.Rows)
{
                       
  decimal iorigrate=0M;
//Cells[1] - index of desired cell
 if(!string.IsNullOrEmpty(dataGridView1.Cells[1].Text))
  {
   iorigrate =Decimal.Parse(dataGridView1.Rows[i].Cells[1].Value);

   }
}

hope it helps!

thnks


don't forget to vote:)
 
Share this answer
 

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