vb.net Select Case error - vb.net

When i want define values in Select Case i got error:
'Value' is not declared. It may be inaccessible due to its protection level.
When Dim Value As Object is outside Select Case - No error. My target is get Value different on special numbers. For example:
Select Case Integer
Case 1
Dim Value As New UserControl1
Case 2
Dim Value As New UserControl2
Case Else
Dim Value As New UserControl3
End Select

Try this, assuming all 3 user control types derive from the base UserControl object:
Dim Value as UserControl
Select Case Integer
Case 1
Value = New UserControl1
Case 2
Value = New UserControl2
Case Else
Value = New UserControl3
End Select

It is not passable to declare a variable inside a scope and access it outside the scope such as as select case statement. However you problem is easily solved by separating the declaration and initialization. This enables you to use the variable outside the select case as the variable is in the higher scope. The variable is declared as System.Windows.Controls.UserControl as this is the most specific common type.
Dim Value As UserControl
Select Case Integer
Case 1
Value = New UserControl1
Case 2
Value = New UserControl2
Case Else
Value = New UserControl3
End Select

Related

DBNull Exception from datatable row

I am a beginner and stuck at one place. So basically I need to set date value to one of the datarow. But when ever date is empty string I need to set it to "Now". But I am not able to do so.
I am getting exception as System.Data.StrongTypingException' "The value for column 'dteEntry' in table is DBNull
Here is what I have
Dim strTimeStamp as String = "" ' code taken out for brevity
' dteEntry is of Date datatype
Dim newRow As ABC.Dataset.dtCardRow = ret.NewdtCardRow()
If (Date.TryParse(strTimeStamp, newRow.dteEntry)) Then
else
newRow.dteEntry = Now
End If
This is a strongly typed DataSet which auto generates code like the NewdtCardRow method that you have used above. Every property that is nullable has also a method to check if it's null, it's name is derived from the name of the property, something like IsEntryDate_Null, which you can use to check if the value is NULL. Otherwise you get an exception if you read that property as you do in Date.TryParse.
But in this case you can prevent this exception by using a local variable for Date.Parse instead of passing the property itself:
newRow.dteEntry = DateTime.Now ' writing doesnt cause this exception
Dim entryDate As Date
If Date.TryParse(strTimeStamp, entryDate) Then
newRow.dteEntry = entryDate ' writing doesnt cause this exception
End If

Check if ArrayList contains an object with a property that equals a specific value

So I have an application in VB.net that is pulling data from a table and inserting it into an arraylist to be used later. What I want to do is before adding the object to the arraylist, I want to check that arraylist to see if the object exists, but I want to be able to check based off a particular property of that object.
Here is an example of what I am talking about:
Lets say Im pulling info from a table with the following columns:
InvoiceNo|DateCharged|Quantity|TotalCharge
I have a SQL statement that pulls info from a table and then I use a data reader to go through the info. My Code looks somewhat like this:
Dim dbobjM As New clsDbobjManual()
If dbobjM.Exec_SQL_DR("SELECT InvoiceNo, DateCharged, Quantity, TotalCharges From Invoices") = 0 Then
If dbobjM.DataReader.HasRows Then
Dim invoicelist As New ArrayList(5000)
Dim invoiceno As String = String.Empty
Do While dbobjM.DataReader.Read()
invoicelist.Add(New Invoice(dbobjM.DataReader.GetInt32(0), dbobjM.DataReader.Value(1), dbobjM.DataReader.GetInt32(2), dbobjM.DataReader.GetFloat(3)))
Loop
End If
End if
(Exec_SQL_DR is a function in the clsDbobjManual class that check to make sure the SQL is in the proper syntax first and checks that records are returned otherwise it returns an error)
Basically what I want to do is before I add a new object to the arraylist I want to check if an object already exists in the list where the InvoiceNo is a particular value, or the value pulled from the table each time to make sure there is no duplicates. I want one object in the list for each InvoiceNo.
Im looking for something like:
If Not invoicelist.Contains(Object where InvoiceNo = dbobjM.DataReader.GetInt32(0)) Then
invoicelist.Add
End If
But I cant seem to find what I need, any help is greatly appreciated
There is no need to use the outdated ArrayList: a List will serve you better. Please see ArrayList vs List<> in C# if you need reasons - the advantages for a list apply to VB.NET too.
Without seeing your clsDbobjManual or Invoice classes, I ended up writing the minimal code to do what you're after, which is basically the check for invoices.Any(Function(i) i.InvoiceNo = inv.InvoiceNo), which you can do if you have the data in a List(Of Invoice).
Please note that I assumed that the appropriate data types have been used in the database - you should use the Decimal type for money as otherwise you can end up with significant rounding errors, and a date should be stored as DateTime, not as a string.
Imports System.Data.SqlClient
Module Module1
Class Invoice
Property InvoiceNo As Integer
Property DateCharged As DateTime
Property Quantity As Integer
Property TotalCharges As Decimal
Sub New()
' empty constructor
End Sub
Sub New(invoiceNo As Integer, dateCharged As DateTime, quantity As Integer, totalCharges As Decimal)
Me.InvoiceNo = invoiceNo
Me.DateCharged = dateCharged
Me.Quantity = quantity
Me.TotalCharges = totalCharges
End Sub
End Class
Function LoadData() As List(Of Invoice)
Dim invoices As New List(Of Invoice)
Dim connStr As String = "your connection string"
Dim sql = "SELECT InvoiceNo, DateCharged, Quantity, TotalCharges From Invoices"
Using sqlConn As New SqlConnection(connStr)
Using sqlCmd As New SqlCommand(sql, sqlConn)
Dim reader As SqlDataReader = sqlCmd.ExecuteReader()
While reader.Read()
Dim inv As New Invoice(reader.GetInt32(0), reader.GetDateTime(1), reader.GetInt32(2), reader.GetDecimal(3))
If Not (invoices.Any(Function(i) i.InvoiceNo = inv.InvoiceNo)) Then
invoices.Add(inv)
Else
' there is a duplicate invoice number
End If
End While
End Using
End Using
Return invoices
End Function
Sub Main()
Dim uniqueInvoices As List(Of Invoice) = LoadData()
' uniqueInvoices now contains the data
End Sub
End Module
If you had a lot of invoice entries to go through, you would likely be better off writing an SQL query to do that.
If you actually just want to find duplicate invoice numbers, you could use the SQL
SELECT [InvoiceNo]
FROM testTable
GROUP BY [InvoiceNo]
HAVING COUNT([InvoiceNo]) > 1
Finally, please ensure that you are using Option Strict On so that you don't make accidental data type errors - they can drastically slow down your program and lead to erroneous results.
You can use linq to select the objects that matches your condition.
Dim result = (From invoiceitem As Invoice
In invoicelist
Where invoiceitem.InvoiceNo = dbobjM.DataReader.GetInt32(0)
Select invoiceitem).ToList()
If Not result.Count > 0 Then
invoicelist.Add(New Invoice(dbobjM.DataReader.GetInt32(0), dbobjM.DataReader.Value(1), dbobjM.DataReader.GetInt32(2), dbobjM.DataReader.GetFloat(3)))
End If

Dataview filtering vb.net

Wondering if I am doing this filter right with the dataview, it keeps throwing me this error
Additional information: Filter expression '80' does not evaluate to a Boolean term.
but here is the code
Dim table = DataSet1.Tables("network")
table.DefaultView.RowFilter = TextBox1.Text
DataGridView1.DataSource = table
To filter something on a DataVIew you need to specify the column on which the filter should be applied, the operator for the comparison and the value that you want to use for the filtering. It seems that you have given only the value "80".
For example, assuming the column of interest is named "NumberOfPieces" and you have typed 80 in the textbox
Dim table = DataSet1.Tables("network")
table.DefaultView.RowFilter = "NumberOfPieces = " & TextBox1.Text
DataGridView1.DataSource = table
This will filter the view with all the rows that have the value (a numeric value) equals to 80 in the column "NumberOfPieces". You could use other operators like Greater/Lesser Than ( >= <= ) or more complex construct that are well detailed in the MSDN page about the Expression property of the DataColumn object

VBA Variable Scope in a Case Statement

Wondering how variables work when used in a Case statement. It seems like they are declared in the first Case, regardless of whether that Case is relevant.
'The following code will throw an error
Select Case team
Case "Philadelphia Eagles"
dim record as String
Case "Dallas Cowboys"
dim record as String
End Select
Even if 'team' isn't Philadelphia Eagles, it claims I've already declared the variable 'record'
I was under the impression that anything in a case statement was completely skipped over if that case wasn't relevant.
'The following code works
Select Case team
Case "Philadelphia Eagles"
dim record as String
Case "Dallas Cowboys"
record = "8-8"
End Select
Just want to confirm that I am understanding the Case statement correctly here.
Thank you!
Josh
Variable declarations (Dim statements) are resolved at the start of the execution of the program, which is why your Dim record statements weren't "skipped" over in your example.
You should put your variable declarations once at the top of your code, just after you start the subroutine or function. You cannot use Dim on the same variable twice. If the variable is an array which you need to resize, you can use ReDim [Preserve] instead.
Sub Subname()
Dim record as String
Select Case team
Case "Philadelphia Eagles"
record = "16-0"
Case "Dallas Cowboys"
record = "8-8"
End Select
End Sub

Is it possible to define same variable twice in classic ASP?

Is it somehow possible to declare same variable in .asp file twice? Example below does not look very clever, but this is just an example and I have to sort it out.
Dim number : number = 1
Select Case number
Case 1
Dim a
Case 2
Dim a
End Select
Theoretically you can of course declare a variable twice, the problem is, asp will throw an error, if the variable is declared in the same scope.
whatever you want to achieve, keep in mind, you can (almost) always access the variables in parent scope, thus rendering a double declaration useless.
Dim number : number = 1
Dim a
Select Case number
Case 1:
a = "whatever"
Case 2:
a = "something different"
End Select
response.write a