Can't get value in dim with CInt vb.net - asp.net

I'm fairly new to vb.net and can't find why this is not working, dim test is just empty after this line happened. Any possible problems?
Dim test As Integer = CInt(System.Configuration.ConfigurationManager.AppSettings("TermstextFieldTypeId"))

Try this code;
If your TermstextFieldTypeId setting IsNullOrEmpty then get default(0) value else getter setting value.
Dim val = System.Configuration.ConfigurationManager.AppSettings("TermstextFieldTypeId")
Dim test As Integer = CInt(IIf(String.IsNullOrEmpty(val), 0, val))

Related

How to find the value of a dropdownlist using FindControl?

I am working on a website with VB.NET and ASP.NET. I currently have recurring DropDownLists for the user to provide input.
The design is recurring. These DropDownLists get their values from a database table, Everything with the Web interface is working except for writing these recurring values to the database - that is just to give you some background.
I have set the ID's of each DropDownList like so:
FrequencyList.ID = String.Concat("FreqList", DBReader(0))
That is in a loop while reading the DatabaseReader.
This is what I'm having issues with (please note I simplified the code down to make it easier to read:
Dim i As Integer
DBCommand = New SqlCommand()
DBCommand.Connection = DBConnection
DBCommand.CommandType = Data.CommandType.StoredProcedure
DBCommand.CommandText = "StoredProcedureName"
DBConnection.Open()
For i = 1 To AspectTableLength
Dim ParamFrequencyID As SqlParameter = DBCommand.Parameters.Add("#nFrequencyID", SqlDbType.Int)
ParamFrequencyID.Value = FindControl("FreqList" & Convert.ToString(i))
ParamFrequencyID.Direction = ParameterDirection.Input
Next
The FindControl("FreqList" & Convert.ToString(i)) variable is incorrect because it does not access the value - and adding .SelectedItem.Value does not work.
I got help from a developer.
Dim MyControls As ControlCollection = Panel.Controls
Dim Number As Integer 'this is the same as "DBReader(0)"
For Each MyControl As Control In MyControls
If MyControl.ID Is Nothing Then
Else
If MyControl.ID.StartsWith("Span") Then
Number = Replace(MyControl.ID, "Span", "")
Dim Freq As DropDownList = PanelMain.FindControl(“FreqList” & Number)
Dim ParamFrequencyID As SqlParameter = DBCommand.Parameters.Add("#nFrequencyID", SqlDbType.Int)
ParamFrequencyID.Value = Freq.SelectedIndex
ParamFrequencyID.Direction = ParameterDirection.Input
DBCommand.ExecuteNonQuery()
DBCommand.Parameters.Clear()
End If
End If
Next
DBConnection.Close()

Index out of bound of the array in VB

So I'm trying to put a 2D array into 2 arrays but I keep getting index out of the bounds of the array. The reason you might ask of why I'm doing this is so that I can store it in a viewstate object so I don't have to continuously retrieve my data.
The code I've been trying to use is this:
Dim testArray As String() = {}
testArray(0) = dataArray(0, 1)
and if there is a way to store a 2D array in a viewstate, let me know. Thanks!
Oh, I'm sorry, Heres the dataArray code
Private Function getDataArray() As Array
Dim x As Integer
Dim DT As DataTable
Dim TA As New DSOldOrdersTableAdapters.TA
DT = getOldOrders()
ReDim dataArray(3, DT.Rows.Count - 1)
For x = 0 To DT.Rows.Count - 1
dataArray(0, x) = DT.Rows(x).Item("SO")
dataArray(1, x) = DT.Rows(x).Item("Customer")
dataArray(2, x) = DT.Rows(x).Item("ShipBy")
Next
Return dataArray
End Function
Dim testArray As String() = {}
testArray(0) = dataArray(0, 1)
You're declaring testArray and assigning an empty array to it. Because the array is zero-length, there is no index 0, so trying to assign to testArray(0) results in an index out of bounds exception.
You need to ReDim the testArray variable, or change your assignment:
Dim testArray As String() = {}
ReDim testArray(0)
testArray(0) = dataArray(0, 1)
'or
Dim testArray As String() = {dataArray(0,1)}
Declaring
Dim testArray As String() = {}
created a string array with a dimension set to zero
So testArray is useless without a dimensioning of at least one element.
Writing
testArray(0) = dataArray(0, 1)
cause the Index Out Of bound exception
Probably you need an array of the same length of your datatable rows.
But, why all the effort to use an array in this context?.
A List(Of String) is a better solution
Dim testValues = new List(Of String)()
testValues.Add(dataArray(0,1))
In this way you don't need to know how big the testArray should be and you could use the List as an array in every place where it is required to have the array
Dim soValue = testValues(0)
or
testValues.ToArray()
The error you're having would be because, the element you're trying to refer to is not present in the Array. Index of an array starts at 0, so always try to make sure, you're refering the last element to be (maxElements - 1). Otherwise it would cause an error and you'll see this error.

VB.net Null reference on database connection

I know I'm being an idiot here and I just can't work it out. But i'm trying to take some data back from a vb.net database. It's falling over with a Object reference not set to an instance of an object error. And before the code runs it's saying the variable is being used before it's set, but I can't see how. Code:
Private taNotifications As dsDataTableAdapters.NotificationsTableAdapter = New dsDataTableAdapters.NotificationsTableAdapter
Dim notification As dsData.NotificationsDataTable = taNotifications.GetDataByClientID(userrow.UserID)
If notification.Rows.Count > 0 Then
Dim notificationrow As dsData.NotificationsRow
Dim forwardURL As String = notificationrow.ForwardLocation
End If
It falls over on the Dim forwardURL As String = notificationrow.ForwardLocation
The problem is that you have never instantiated the notificationRow inside the if statement. You've declared it, but it doesn't belong to anything. You need to make an assignment or loop through your rows before doing anything with this object:
Dim notificationrow As dsData.NotificationsRow ' this is not instantiated
Dim forwardURL As String = notificationrow.ForwardLocation
What you really want in this case is:
For Each notificationRow As dsData.NotificationRow In notification
Dim forwardURL As String = notificationRow.ForwardLocation
' Do Something
Next
If you only HAVE one row and you know you only have 1 or 0 rows then you could use your if statement by doing:
If notification.Rows.Count > 0 Then
Dim notificationrow As dsData.NotificationsRow = _
CType(notification.Rows(0), dsData.NotificationsRow)
Dim forwardURL As String = notificationrow.ForwardLocation
End If
Edit: In the code above, I originally just had notification.Rows(0). This will produce a DataRow object, but it will not be strongly typed. You need to perform the CType that I added in order to use the custom property ForwardLocation.
You never set notificationrow to anything. Did you mean to set it like this?
Dim notificationrow As dsData.NotificationsRow = CType(notification.Rows(0), dsData.NotificationsRow)

Programatic referencing of literals and object properties

I have advice that this might work for programmatically filling a set of literals from a set of object properties:-
For i As Integer = 1 To noOfTexts
Dim ctl As Literal = DirectCast(FindControl("help" & i), Literal)
If ctl IsNot Nothing Then
ctl.Text = pageData.help(i).trim()
ctl.Visible = True
End If
Next
However, the line: ctl.Text = pageData.help(i).trim() fails because it's not understood that pageData.help(i) should translate to pageData.help1, pageData.help2, etc.
Is there some syntax that would achieve this in VS2010 Asp.net VB?
You can reference properties dynamically using reflection...
Dim PageDataType As Type = GetType(pageData)
Dim PropertyName As String = "help" & i
Dim Property As PropertyInfo = PageDataType.GetProperty(PropertyName)
Dim PropertyValue As String
If Property IsNot Nothing
PropertyValue = Property.GetValue(pageData, Nothing)
End If
Note: Reflection in .NET is slower than direct access, but not enough to make its use impractical

Idatareaders not returning values from database

In my codebehind I have this vb:
Dim reader as idatareader = includes.SelectDepartmentID(PageID)
While reader.Read
Did = reader("departmentid")
GroupingHeading = reader("heading")
Folder = reader("folder")
If reader("OwnBanner") Is DBNull.Value Then
OwnBanner = String.Empty
Else
OwnBanner = reader("ownbanner")
End If
Then in my class I have:
Public Function SelectDepartmentID(ByVal PageID As Integer) As IDataReader
Dim Command As SqlCommand = db.GetSqlStringCommand("sql")
db.AddInParameter(Command, "#pageid", Data.DbType.Int32, PageID)
Dim reader As IDataReader = db.ExecuteReader(Command)
reader.Read()
Return reader
End Function
No Errors are being presented yet nothing is being returned by the reader. Is there an error in my code?
Thanks.
Try removing the
reader.Read()
line from SelectDepartmentID.
You are skipping the first row of the reader. Remove the reader.Read() statement in the SelectDepartmentID function just prior to the return statement.
Any function that returns a reader should make no assumptions about what the calling code will do with it and just return it unmodified.

Resources