hey
i have a datagridview and i am validating 3 columns, the user has to enter data in to those 3 columns,

else if ((!string.IsNullOrEmpty(dgvActions.Rows[dgvAAddR - 1].Cells[0].Value.ToString())) && (!string.IsNullOrEmpty(dgvActions.Rows[dgvAAddR - 1].Cells[1].Value.ToString())) && (!string.IsNullOrEmpty(dgvActions.Rows[dgvAAddR - 1].Cells[2].Value.ToString())))

when the user hasn't entered any value in one of this cells there is an NUll reference exception occuring
how can i avoid this and check whether the columns are not null nad empty,
thanx

Recommended Answers

All 5 Replies

If the null exception is occurring regardless of information, perhaps you're looking at the wrong cell or not looking at the actual value? If you're using VC#, put a break in and go through it step by step to see what the actual values and so forth are. Toss a few variables into the mix so you know what is what before it hits the else if:
string CellValue0 = (string.IsNullOrEmpty(dgvActions.Rows[dgvAAddr - 1].Cells[0].Value.ToString());

And just as a little warning, if I'm understanding you correctly, those fields are required for the user to fill in. !string.IsNullOrEmpty() will return true if they are not empty.

Apologies if I misunderstood the question or problem.

If the null exception is occurring regardless of information, perhaps you're looking at the wrong cell or not looking at the actual value? If you're using VC#, put a break in and go through it step by step to see what the actual values and so forth are. Toss a few variables into the mix so you know what is what before it hits the else if:
string CellValue0 = (string.IsNullOrEmpty(dgvActions.Rows[dgvAAddr - 1].Cells[0].Value.ToString());

And just as a little warning, if I'm understanding you correctly, those fields are required for the user to fill in. !string.IsNullOrEmpty() will return true if they are not empty.

Apologies if I misunderstood the question or problem.

hey
i am looking at the correct cell and if there is no value entered by the user or if the data is not in the database the exception occurs, so the exception will occur in one of the columns if there is no data in that column

thanx

Have you tried removing the ToString() yet? Could always take the cheap way out and wrap it up into a try..catch().

Have you tried removing the ToString() yet? Could always take the cheap way out and wrap it up into a try..catch().

do you know how??

else if (dgvActions.Rows[dgvAAddR - 1].Cells[0].Value != null && dgvActions.Rows[dgvAAddR - 1].Cells[1].Value != null && dgvActions.Rows[dgvAAddR - 1].Cells[2].Value != null)

Or try-catch()

try{
else if ((!string.IsNullOrEmpty(dgvActions.Rows[dgvAAddR - 1].Cells[0].Value.ToString())) && (!string.IsNullOrEmpty(dgvActions.Rows[dgvAAddR - 1].Cells[1].Value.ToString())) && (!string.IsNullOrEmpty(dgvActions.Rows[dgvAAddR - 1].Cells[2].Value.ToString()))
}catch{
  // This will trigger if any kind of error occurs in the try{} section.
}

Try-catch docs: http://msdn.microsoft.com/en-us/library/0yd65esw(VS.80).aspx

commented: Very willing to help :) +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.