Access and VB.Net

D

djamila

Hi,

I have one problem with VB.Net using Access database that I couldn't
find the solution.

I have a form that represents a task with two buttons: the button
start which we click first to start the task and it will be disabled
just after that. And the button finish which will be enabled when we
click the start button and it represents the end of the task. After
that the two buttons will be disabled.

I use two fields in ma access table: start and finish of type date.
In my program I select the values of start and finish. If start is
null I enable start button and disable finish button. If start is not
null and finish is null I disable the start button and enable the
finish button. In the case that the two values exist, I disable the
two buttons.

So in my form load I have the following program:

Try
objetconnection = New
System.Data.OleDb.OleDbConnection(sconnection)

sql_text = "Select start, finish from User_Project_Task
where login like '" & Variables.login & _
"' and Project_ID = " & Project_ID & " and task_ID = " & Task_Id

Command = New OleDbCommand(sql_text, objetconnection)
objetconnection.Open()
reader = Command.ExecuteReader

If reader.HasRows Then
reader.Read()
start = reader.GetDateTime(0)
finish = reader.GetDateTime(1)
If start <> Nothing Then
If finish <> Nothing Then
bt_finish.Enabled = False
Else
bt_finish.Enabled = True
End If
bt_start.Enabled = False
Else
bt_start.Enabled = True
bt_finish.Enabled = True
End If

End If
reader.Close()
objetconnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try


When I run my program And I have a date value in start field and a
null value in finish field the program gives the following error for
the finish = reader.GetDateTime(1) -> The cast specified is not valid.
So the value is null and can not be read as GetDateTime. So how can I
test the value if I couldn't read it in a variable.

Please, can any one help me for that.

Best regards,

Djamila.
 
C

Cor Ligthert [MVP]

Djamila,

Can you set in top of your program.

Option Strict On

In design time you will than be warned for some errors.

Cor
 
R

RobinS

Are you sure that the first and second columns of your table are date/time
fields AND RE NOT NULL? What if you get those as strings and then look at
them before doing something with them? Then does your code work?

Robin S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top