Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi friends,

I am writing the code in vb.net where I am passing the same value from datagrid control's column in two different masked text boxes which are used for date input.

txtDate.Text = Format(CDate(dtgrdBranch.CurrentRow.Cells(4).Value), "dd/MM/yyyy")
txtColorInDate.Text = Format(CDate(dtgrdBranch.CurrentRow.Cells(4).Value), "dd/MM/yyyy")


Now the scenario is that, txtdate.text works properly and displays date perfectly, but same value while getting passed in the txtcolorindate control gives me "
Object reference not set to an instance of an object.
" (NullReferenceException was unhandled) error.


Please help me. this bug is delaying the work very badly and I couldn't find any solution since more than 7 days.

What I have tried:

I have tried to change the variable name. Also tried to split the value of
dtgrdBranch.CurrentRow.Cells(4).Value
into string array and pass that concatenated string into txtcolorindate masked text box. but the error messages remains same.
Posted
Comments
Richard MacCutchan 21-Nov-23 8:56am    
The error message is clear; you are trying to use a variable that has not been initialised to a valid reference. But it is not clear which variable causes the error. If the two lines of code are one after the other as shown above then it must be txtColorInDate. You should check by using the debugger. Although why you did not use the debugger 7 days ago is something of a mystery.
[no name] 21-Nov-23 12:06pm    
Retrieve the grid value "once" by storing it in a variable; then use the variable to assign. (My guess is you typed the "second" getter wrong; contrary to what you're showing).

1 solution

I'd suggest to get datagridviewcell's value into variable once, then use it.
On the other hand, forget about old VB function CDate. Use proper .Net function: DateTime.TryParse Method (System) | Microsoft Learn[^]

VB.NET
Dim dd As DateTime
If DateTime.TryParse(dtgrdBranch.CurrentRow.Cells(4).Value.ToString(), dd) Then
    txtDate.Text = dd.ToString("dd/MM/yyyy")
    txtColorInDate.Text = dd.ToString("dd/MM/yyyy")
End If
 
Share this answer
 
v2
Comments
Richard Deeming 22-Nov-23 4:02am    
TryParse requires a string, whereas the Value property is an object. :)
Maciej Los 22-Nov-23 15:36pm    
Good point. Thank you, Richard.
Member 13706784 23-Nov-23 1:32am    
Thank you so much Maciej Los and Richard Deeming.
I have tried this
Dim dd As DateTime
If DateTime.TryParse(dtgrdBranch.CurrentRow.Cells(4).Value.ToString(), dd) Then
txtDate.Text = dd.ToString("dd/MM/yyyy")
txtColorInDate.Text = dd.ToString("dd/MM/yyyy")
End If

But again the same issue I am encountering. txtDate.text shows the value stored in dd whereas txtColorInDate.text throws the same error NullReferenceException was unhandled.

I really can't understand that why the same value is not getting passed in two same kind of masked text box objects.
Please help
Member 13706784 23-Nov-23 1:32am    
Thank you so much Maciej Los and Richard Deeming.
I have tried this
Dim dd As DateTime
If DateTime.TryParse(dtgrdBranch.CurrentRow.Cells(4).Value.ToString(), dd) Then
txtDate.Text = dd.ToString("dd/MM/yyyy")
txtColorInDate.Text = dd.ToString("dd/MM/yyyy")
End If

But again the same issue I am encountering. txtDate.text shows the value stored in dd whereas txtColorInDate.text throws the same error NullReferenceException was unhandled.

I really can't understand that why the same value is not getting passed in two same kind of masked text box objects.
Please help

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