IndexOutOfRangeException was unhandled VBNET - vb.net

I have a form that retrieves the CurrentYearyear_now and NextYearyear_next from the database. It gives me the error 'IndexOutOfRangeException was unhandled' bcoz I think there is no row to be retrieved. And I wanted to do is, even though there's no data from the table, I can still load the forms and give me a null value to be displayed. I tried everything I know to make it work but I cant resolve the problem. Is there anyway to recode this.
Sub LoadSchoolYear()
Dim conn1 As New MySqlConnection("server=localhost; userid=root; password=root; database=uecp_cens")
Dim dAdapter1 As New MySqlDataAdapter("SELECT year_now, year_next FROM uecp_cens.tblschoolyear", conn1)
Dim dTable1 As New DataSet
Try
dAdapter1.Fill(dTable1, "uecp_cens")
lblYearNow.Text = dTable1.Tables("uecp_cens").Rows(0).Item(0).ToString
lblYearNext.Text = dTable1.Tables("uecp_cens").Rows(0).Item(1).ToString
Catch ex As MySqlException
MsgBox(ex.Message)
Finally
conn1.Dispose()
End Try
End Sub
Any help is appreciated.

If there is no row in the table as you've mentioned you get that error. So check Rows.Count:
Dim yearNow As String = Nothing
Dim yearNext As String = Nothing
If dTable1.Tables("uecp_cens").Rows.Count > 0 Then
yearNow = dTable1.Tables("uecp_cens").Rows(0).Item(0).ToString()
yearNext = dTable1.Tables("uecp_cens").Rows(0).Item(1).ToString()
End If
lblYearNow.Text = yearNow
lblYearNext.Text = yearNext
If instead the field is NULL in the DB it is DBNull.Value and you get an exception if you use Rows(0).Item(0).ToString(). Then use DataRow.IsNull to check it:
If dTable1.Tables("uecp_cens").Rows.Count > 0 Then
Dim row = dTable1.Tables("uecp_cens").Rows(0)
yearNow = If(row.IsNull(0), Nothing, row.Item(0).ToString())
yearNext = If(row.IsNull(1), Nothing, row.Item(1).ToString())
End If

Related

Can't display Data in ComboBox Control of DropDownStyle (DropDownList)

I have the following requirement,
I have a ComboBox Control (DropDownList Style) which user has to select a given value, but can not edit. Then I save it to a Database Table, and it's working fine.
(dataRow("it_discount_profile") = Trim(cmbDisProfile.Text))
But when I try to show the same Data in the same ComboBox by retrieving it from the Database, it won't show.
(cmbDisProfile.Text = Trim(tempTb.Rows(0).Item("it_discount_profile")))
When I change the ComboBox to "DropDown Style", it works. But then the User can edit it.
Am I missing something here or is it like that? Any advice will be highly appreciated.
Im filling it in runtime using a procedure.
Private Sub filldisProfiles()
Dim sqlString As String = "SELECT discount_profile FROM tb_discount_profiles"
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(sqlString)
cmbDisProfile.DataSource = tempTb
cmbDisProfile.DisplayMember = "discount_profile"
End Sub
Ok. Actually, Im trying to migrate one of my old project from VB to VB.Net. VB.Net is little new to me. Im using a self built classto reduce codes in other places. Im attaching the class below.
My actual requirement is to populate the combo box from a table. I like to do it in run time. I don't want users to edit it. If they want to add a new value, they have separate place (Form) for that. I think im doing it in a wrong way. If possible please give a sample code since I'm not familiar with the proposed method.
Public Function myFunctionFetchTbData(ByVal inputSqlString As String) As DataTable
Try
Dim SqlCmd As New SqlCommand(inputSqlString, conn)
Dim dataAdapter As New SqlDataAdapter(SqlCmd)
Dim fetchedDataSet As New DataSet
fetchedDataSet.Clear()
dataAdapter.Fill(fetchedDataSet)
Dim fetchedDataTable As DataTable = fetchedDataSet.Tables(0)
Return fetchedDataTable
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Function
' this sub will update a table
Public Sub MyMethodUpdateTable(ByVal sqlString As String, ByVal tbToUpdate As DataTable)
Dim SqlCmd As New SqlCommand(sqlString, conn)
Dim dataAdapter As New SqlDataAdapter(SqlCmd)
Dim objCommandBuilder As New SqlClient.SqlCommandBuilder(dataAdapter)
dataAdapter.Update(tbToUpdate)
End Sub
Public Function MyMethodfindRecord(ByVal strSearckKey As String, ByVal tableName As String, ByVal strColumnName As String) As Boolean
Try
Dim searchSql As String = "SELECT * FROM " & tableName & " WHERE " & strColumnName & "='" & strSearckKey & "'"
'Dim searchString As String = txtCategoryCode.Text
' searchOwnerCmd.Parameters.Clear()
' searchOwnerCmd.Parameters.AddWithValue("a", "%" & search & "%")
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(searchSql)
If tempTb.Rows.Count = 0 Then
Return False
Else
Return True
End If
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Function
Public Function myFunctionFetchSearchTB(ByVal inputSqlString As String) As DataTable
Try
Dim SqlCmd As New SqlCommand(inputSqlString, conn)
Dim dataAdapter As New SqlDataAdapter(SqlCmd)
Dim fetchedDataSet As New DataSet
fetchedDataSet.Clear()
dataAdapter.Fill(fetchedDataSet)
Dim fetchedSearchTB As DataTable = fetchedDataSet.Tables(0)
Return fetchedSearchTB
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Function
OK. If I understood correctly, you have a problem in retrieving Data [String] from a Database Table into a ComboBox of DropDownStyle [DropDownList].
How do you fill/populate your ComboBox with Data From Database Table ?
In this link, Microsoft docs state, that:
Use the SelectedIndex property to programmatically determine the index
of the item selected by the user from the DropDownList control. The
index can then be used to retrieve the selected item from the Items
collection of the control.
In much more plain English
You can never SET ComboBox.Text Value while in DropDownList by code, which you already knew by testing your code, but you need to use DisplayMember and ValueMember or SelectedIndex.
ComboBox1.SelectedIndex = ComboBox1.FindStringExact(Trim(tempTb.Rows(0).Item("it_discount_profile")))
Please consider populating your ComboBox Control from Database Table using (Key,Value) Dictionary collection, here is an example
Thank you guys for all the advice's. The only way it can be done is the way u said. I thought of putting the working code and some points for the benefit of all.
proposed,
ComboBox1.SelectedIndex = comboBox1.FindStringExact(Trim(tempTb.Rows(0).Item("it_discount_profile")))"
does not work when u bind the datatable to the combobox. The values should be added to the combo box in run time if the above "SelectedIndex" method to work. The code to add items to the combobox is as follows(myClassTableActivities is a class defined by myself and its shown above),
Private Sub filldisProfiles()
Dim sqlString As String = "SELECT discount_profile FROM tb_discount_profiles"
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(sqlString)
Dim i As Integer = 0
For i = 0 To tempTb.Rows.Count - 1
cmbDisProfile.Items.Add(Trim(tempTb.Rows(i).Item("discount_profile")))
Next
End Sub
After adding we can use the following code to display the data on combobox (DropDownList).
Private Sub txtItCode_TextChanged(sender As Object, e As EventArgs) Handles txtItCode.TextChanged
Try
Dim sqlString As String = "SELECT * FROM tb_items where it_code='" & Trim(txtItCode.Text) & "'"
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(sqlString)
If Len(txtItCode.Text) < 4 Then
cmdAdd.Enabled = False
cmdDelete.Enabled = False
cmdSave.Enabled = False
Else
If tempTb.Rows.Count > 0 Then
With tempTb
txtItName.Text = Trim(tempTb.Rows(0).Item("it_name"))
cmbDisProfile.SelectedIndex = cmbDisProfile.FindStringExact(Trim(tempTb.Rows(0).Item("it_discount_profile")))
cmbProfitProfile.SelectedIndex = cmbProfitProfile.FindStringExact(Trim(tempTb.Rows(0).Item("it_profit_profile")))
cmbItCategory.SelectedIndex = cmbItCategory.FindStringExact(Trim(tempTb.Rows(0).Item("it_category")))
cmbFinanCategory.SelectedIndex = cmbFinanCategory.FindStringExact((tempTb.Rows(0).Item("it_finance_category")))
End With
cmdAdd.Enabled = False
cmdDelete.Enabled = True
cmdSave.Enabled = True
Else
cmdAdd.Enabled = True
cmdDelete.Enabled = False
cmdSave.Enabled = False
txtItName.Text = ""
Call fillItCategories()
Call fillProProfile()
Call filldisProfiles()
Call fillFinCat()
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

Importing a DataRow to a new DataTable results in error. VB.NET

I've been put on an older project vb.net project. I am trying to query a datatable and import the resulting DataRow into a newly created DataTable.
Code:
Private Sub FilterSelectedByString(ByVal str As String, ByVal mID As Integer)
Try
dsTargetEmployees = MemberShipManager.GetMessageTargets(_page.Client, mID)
dtTargets = dsTargets.Tables("MessageTargets")
Dim dt As DataTable = Nothing
For Each dr As DataRow In dtTargets.Select(str)
dt.ImportRow(dr)
Next
lstSelected.Items.Clear()
lstSelected.DataSource = dt
lstSelected.DataTextField = "EmployeeName"
lstSelected.DataValueField = "EmployeeID"
lstSelected.DataBind()
Catch ex As Exception
//error code
End Try
End Sub
This results in an "object not set to an instance of an object" error in the for loop. I've tried creating the data columns in the table manually, but that also results in the same error.
Solved my own problem. Copying the original table into the created table then clearing it built the correct structure.
Private Sub FilterSelectedByString(ByVal str As String, ByVal mID As Integer)
Try
dsTargetEmployees = MemberShipManager.GetMessageTargets(_page.Client, mID)
dtTargets = dsTargets.Tables("MessageTargets")
Dim dt As DataTable = dtTargets.Copy()
dt.Clear()
For Each dr As DataRow In dtTargets.Select(str)
dt.ImportRow(dr)
Next
lstSelected.Items.Clear()
lstSelected.DataSource = dt
lstSelected.DataTextField = "EmployeeName"
lstSelected.DataValueField = "EmployeeID"
lstSelected.DataBind()
Catch ex As Exception
//error code
End Try
End Sub

Is it possible to assembly loadfrom Microsoft.Office.Interop.Word.dll

When i try this
oAssembly = Assembly.LoadFrom("C:\dynamicdlls\Microsoft.Office.Interop.Word.dll")
oType = oAssembly.GetType("Microsoft.Office.Interop.Word.Application")
Try
oObject = Activator.CreateInstance(oType)
Dim doc As Object = oObject.Documents.Open("C:\worddoc\test.docx")
Dim count As Integer = doc.Words.Count
For i As Integer = 1 To count
Dim text As String = doc.Words(i).Text
Console.WriteLine("Word {0} = {1}", i, text)
Next
oObject.Quit()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
I get "Cannot create an instance of an interface".
I Know there is a better alternative to this, I am just exploring this method and would like to know if this is possible and how would one do it.

Form Hide not working

Something strange, This form is opened from another from but I want the form to close and return to the initial form if user input are missing. This is not happening, WHY ?
Try
Dim din As Int32 = (Form1.ComboBox1.SelectedValue)
Dim dt As String = Form1.Label1.Text
Dim dt2 As String = Form1.Label2.Text
If din = Nothing Or dt = Nothing Or dt2 = Nothing Then
MessageBox.Show("Please Select a Staff and Date Range", "Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.Hide()
Form1.Show()
Else
' MsgBox(din & "/" & dt & "/" & dt2)
DataGridView1.DataSource = getTable(din, DateTime.Parse(dt), DateTime.Parse(dt2))
DataGridView1.AutoResizeColumns()
Me.Text = "Selected Staff: " & CStr(Form1.ComboBox1.Text)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
You use the Text property of Labels
Dim din As Int32 = (Form1.ComboBox1.SelectedValue)
Dim dt As String = Form1.Label1.Text
Dim dt2 As String = Form1.Label2.Text
If din = Nothing Or dt = Nothing Or dt2 = Nothing Then
but they will never be Nothing. If you try to set Nothing to a Text property of a Control, it will use an empty string instead. I think that is your main problem.
Also, you convert the SelectedValue property to Integer and check it for Nothing. Note that this will be true if the SelectedValue is 0, since it's the default value of Integer.
You should use a debugger and step through your code to be able to solve such problems.
Thanks guys, I finally figured out that I am better off checking the user selections in the original form rather than checking for null values in the called up form.
I appreciate all your input. Thanks.

VB .NET error handling, pass error to caller

this is my very first project on vb.net and i am now struggling to migrate a vba working add in to a vb.net COM Add-in. I think i'm sort of getting the hang, but error handling has me stymied.
This is a test i've been using to understand the try-catch and how to pass exception to caller
Public Sub test()
Dim ActWkSh As Excel.Worksheet
Dim ActRng As Excel.Range
Dim ActCll As Excel.Range
Dim sVar01 As String
Dim iVar01 As Integer
Dim sVar02 As String
Dim iVar02 As Integer
Dim objVar01 As Object
ActWkSh = Me.Application.ActiveSheet
ActRng = Me.Application.Selection
ActCll = Me.Application.ActiveCell
iVar01 = iVar02 = 1
sVar01 = CStr(ActCll.Value)
sVar02 = CStr(ActCll.Offset(1, 0).Value)
Try
objVar01 = GetValuesV(sVar01, sVar02)
'DO SOMETHING HERE
Catch ex As Exception
MsgBox("ERROR: " + ex.Message)
'LOG ERROR SOMEWHERE
Finally
MsgBox("DONE!")
End Try
End Sub
Private Function GetValuesV(ByVal QryStr As Object, ByVal qryConn As String) As Object
Dim cnn As Object
Dim rs As Object
Try
cnn = CreateObject("ADODB.Connection")
cnn.Open(qryConn)
rs = CreateObject("ADODB.recordset")
rs = cnn.Execute(QryStr)
If rs.EOF = False Then
GetValuesV = rs.GetRows
Else
Throw New System.Exception("Query Return Empty Set")
End If
Catch ex As Exception
Throw ex
Finally
rs.Close()
cnn.Close()
End Try
End Function
i'd like to have the error message up to test, but
MsgBox("ERRORE: " + ex.Message)
pops out something unexpected (Object variable or With block variable not set)
What am i doing wrong here??
Thanks
D
It may be because you're doing a
Throw ex
Try using
Throw
instead. This maintains the error stack, instead of generating a new one each time.
Based on your comments, I think the specific problem you're getting is in your Finally block. You're trying to close the recordset and the connection, but if you got an error when you instantiated them, they will not exist; therefore, you get the Object variable or with block error.
(I would put the connection is a using statement anyway).