RecordSet and Function from vb6 to vb.net - vb.net

I was wonder how to convert this line of code to vb.net. I changed the dataset to recordset and it is indeed a function. When i convert It gave me this error. The dataset is filled with values from Database Its just that this "!" is the main error can anyone tell me what that means in vb6? The error that came out was"Dataset Cannot Be index because there is no default Properties"
'getting values from database procedure
If priRecordSet!CodeStatus Then
IfCodeStatus = True
Else
IFCodeStatus = False
End If
Return IfCodeStatus

To access a particular column value of a particular row, use
myTableData.Rows(rowNumber)(columnNumber)
or
myTableData.Rows(rowNumber).Item(columnNumber)

Related

Object cannot be cast from DBNull to other types with visual basic code

I have added dummy data to my SQL database.
When I try to run the VB it comes back with a null error.
What I have done is delete the dummy data from the database so it not reading the null value anymore.
Even after deleting it the VB file is still throwing the error like the null values are still there.
I have run a sql script to check and I can confirm it not longer there.
Here is the line of code that throwing the error.
If Date.Now.AddDays(LoadsFilter * -1) > Convert.ToDateTime(myReader(2)) Then
ShowLoad = 0
End If
I'm still quite new to vb so I'm not sure what to do here.
I was thinking of passing a method to read null values but I have already deleted the null value. I'm not sure how to go about this, can anyone help please?
There's no need for any conversion. The data reader has its own methods for null tests and for getting data in the appropriate type, e.g.
If Not myDataReader.IsDBNull(2) Then
Dim dt = myDataReader.GetDateTime(2)
'Use dt here.
End If
You can also use a single line to get a Nullable(Of Date):
Dim dt = If(myDataReader.IsDBNull(2), New Date?, myDataReader.GetDateTime(2))
Note that, if you prefer to use column names rather than indexes, which make your code clearer, then you can use the GetOrdinal method, as those other methods only support indexes:
Dim dt = If(myDataReader.IsDBNull(myDataReader.GetOrdinal("ColumnName")),
New Date?,
myDataReader.GetDateTime(myDataReader.GetOrdinal("ColumnName")))

MS Access 2019: Can't populate a form field using SQL query in VBA

I want to auto-populate a field in my form based on the results of an SQL query. I'm just not sure of the proper syntax to get it to properly read the query. This is what I've got, but it's not returning the value of the query, it's actually just returning the text of the query itself.
Private Sub PurchBatchNo_Enter()
Dim MostRecentPurchBatch As String
MostRecentPurchBatch = "SELECT Max(PurchaseBatchNo) FROM purchases"
Me.PurchBatchNo.Value = MostRecentPurchBatch
End Sub
I'm sure the issue has to do with the quotation marks, but it doesn't work without them either, and I'm not sure how to write it properly.
Thanks for being here for beginners like me!
All your code does is set a variable to a string of characters then attempts to set value of field with that string.
But why would you want to populate field with a value already used in a record? Most likely you need to increment by 1.
To use an SQL statement, would have to open a recordset object then reference field of recordset.
Private Sub PurchBatchNo_Enter()
Dim MostRecentPurchBatch As DAO.Recordset
If IsNull(Me.PurchBatchNo) Then
Set MostRecentPurchBatch = CurrentDb.OpenRecordset("SELECT Max(PurchaseBatchNo) AS MaxBatch FROM purchases")
Me.PurchBatchNo = MostRecentPurchBatch!MaxBatch + 1
End If
End Sub
However, pulling a single value from table is what domain aggregate functions are good for.
Private Sub PurchBatchNo_Enter()
If IsNull(Me.PurchBatchNo) Then Me.PurchBatchNo = DMax("PurchaseBatchNo", "purchases") + 1
End Sub
Instead of using VBA procedure, consider just setting DefaultValue property of textbox bound to PurchBatchNo field with the DMax() expression. As soon as record is initiated by input to another textbox, the PurchBatchNo will populate.
If user should not be able to edit this value, set textbox as Locked Yes and TabStop No and use a different event for the VBA code if you go with VBA.

Using object in Crystal Reports: Error in formula if object is null or nothing

I am using crystal reports with vb.net
I have an API which sends me an object with some fields in it. I have the same fields declared in a datatable with same name and datatypes as in the original object.
Now I have a formula in CR which checks for a field value and displays a text accordingly. Eg: if{objectname.fieldname}=1 then "showthistext" else "showanothertext"
The field is of type integer.
Now this works if the object has some values. But if the object has no records in it, the CR shows error in formula saying "A String is expected here"
Any ideas on how to fix this.
I cannot modify this in the API as the object cannot be changed from integer to string and back.
I just found that using an integer for comparision in formulas is cuasing issues when the object is null.
So I used Cstr on both sides on "=" in the formula if the field of comparision in not a string.
if isnull({objectname.fieldname}) then ""
else (
if cstr({objectname.fieldname})= cstr(1) then "show one"
else if cstr({objectname.fieldname}) = cstr(2) then "show two"
I don't know if this is the best, but it solved the issue for me.
Thanks to the user "Siva" for the help. The response gave some ideas.
First check is null condition then write your formula... it is encountering null hwnce programme is getting terminated.
If IsNull({objectname.fieldname}
Then "showsometext"
Else
(
if{objectname.fieldname}=1 then "showthistext" else "showanothertext"
)

VB.net application using reader HasRows

I am running an SQL Query with data readers in vb.net
reader3 = myCommand3.ExecuteReader
reader3.Read()
If reader3.HasRows Then
FromToDates = reader3.GetString(3)
End If
There are no rows being returned in this particular query, however even though the if reader3.HasRows is being used its still showing an error saying:
Additional information: Data is Null. This method or property cannot be called on Null values.
this is when trying to set the FromToDates variable but there are no rows being returned so it should not even reach this part
I doubt that there are no rows, i assume that there is at least one row where the value is NULL. You could check it with the IsDBNull-method:
If reader3.HasRows AndAlso Not reader3.IsDbNull(3) Then
FromToDates = reader3.GetString(3)
End If
However, the variable name suggests that it's a date, but you are storing it as string. Use the correct type which is date or datetime. If it's actually a datetime use:
Dim FromToDates As Date
If reader3.HasRows AndAlso Not reader3.IsDbNull(3) Then
FromToDates = reader3.GetDateTime(3)
End If
if you are using Reader.read() then i don't think so you need reader,hasrow(). you can do somthing like this
reader3 = myCommand3.ExecuteReader
If reader3.Read()Then
FromToDates = reader3.GetString(3)
End If
and related to your error its look like you are getting NULL value from database and in vb you can not read NULL, i will suggest to use ISNULL(Column_NAME,'') function in your SQL Script.
It seems that even though the row is empty (all DBNull, perhaps), there IS a row object returning from the query. There are several different solutions presented in this thread that all take differing approaches to catching the empty row before it errors!
http://www.vbforums.com/showthread.php?555183-datareader-and-empty-records

Insert Excel Data Into Access, But Cell Contains No Data?

I am iterating through some rows/cells in an Excel sheet and some of the cells are blank. When I try to do the insert, if a cell contains no data, it advises me that that particular parameter has no default value. In Access, I have set the default value to "=Null" but I still receive the error. Do I have to do an If statement as shown below for every parameter in order to prevent errors? I'd like to clean up my code if possible.
If worksheet1.GetValue(i, 14) Is Nothing Then
cmd.Parameters.Add("#Param1", OleDbType.Char).Value = DBNull.Value
Else
cmd.Parameters.Add("#Param1", OleDbType.Char).Value = worksheet1.GetValue(i, 14)
End If
In my experience (albeit C#), yes you do. Interop (which is what I'm assuming you're using although I don't recognise the GetValue() call) usually throws an exception when you try to get data from an empty cell without first checking. The way I've handled it is to wrap the code in a method and just call it each time (sorry, my VB is a little rusty):
Public Function GetCellContents(column As Integer, row As Integer) As String
If worksheet1.GetValue(column, row) Is Nothing Then
Return ""
Else
Return worksheet1.GetValue(column, row)
End If
End Function
And then you can just do:
cmd.Parameters.Add("#Param1", OleDbType.Char).Value = GetCellContents(i, 14);