Invalid cast Eception unhandled by user code in vb.net - vb.net

I run the site following errors occured:
System.InvalidCastException was unhandled by user code
Message="Conversion from string "kumar" to type 'Short' is not valid."
Imports System.Data.OleDb
Namespace wbdsproject
Partial Class frmprofile
Inherits System.Web.UI.Page
Dim proCon As OleDbConnection
Dim proCmd As OleDbCommand
Dim prodr As OleDbDataReader
Dim username As String
Dim userid As Int16
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
'Put user code to initialize the page here
' maillink.NavigateUrl = "mailto:saranya#gmail.com"
Dim proselqry As String
username = Session("loginuser")
lblusername.Text = username
userid = Request.QueryString("userid")
'Response.Write(userid)
proCon = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Discussion Forum\database-WBDS\SampleForum.mdb")
proCon.Open()
If Session("loginuser") = "" Then
Response.Redirect("frmlogin.aspx")
End If
proselqry = "select * from tblprofile where uid='" & userid & "'"
proCmd = New OleDbCommand(proselqry, proCon)
proCmd.ExecuteNonQuery()
prodr = proCmd.ExecuteReader
If prodr.Read Then
lbldob.Text = prodr("dob")
lblquali.Text = prodr("quali")
lblinterest.Text = prodr("interest")
lbladdress.Text = prodr("address")
maillink.Text = prodr("emailid")
maillink.NavigateUrl = "mailto:" & prodr("emailid")
lblzip.Text = prodr("zip")
lblphno.Text = prodr("phno")
End If
End Sub
Private Sub btnmodify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmodify.Click
Response.Redirect("frmregmodify.aspx?user=" & username & "&userid=" & userid & "&page=frmprofile")
End Sub
Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
End Namespace
at Microsoft.VisualBasic.CompilerServices.Conversions.ToShort(String Value)

You're trying to cast a String returning function into a Int16 in this line
userid = Request.QueryString("userid")
Apparently, your return is not numeric. You can either change that from the source if you have control over it or change change the userid type to string.
I would suggest using a user defined function to test if the requesting querty string is numeric before casting to an Int16 datatype.

Related

Reference to a non-shared member requires an object reference for showing/hiding forms

I have 3 forms, the form2 contains some info that I wanted to appear in a data grid view in form3, I searched online how to do it, so am not really that well-versed or fully understand the changes and or codes I wrote, as long as it worked. The problem now is that buttons from other forms 1 & 2 that supposedly hides and or shows other forms, have errors, it's a project and I would greatly appreciate your help, thx.
form1 (error 1, in bold)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
gin.Click
Dim nun As String = "apple"
Dim pw As Single = 7
Dim name As String
Dim wd As Double
name = name.Text
wd = word.Text
If (name = nun And wd = pw) Then
Me.Hide()
**try**.Show() ' error here
Else
MsgBox("Denied", vbOKOnly + MsgBoxStyle.Critical, "TRY AGAIN") End If
End Sub
form2 (code for data grid view)
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Save.Click
view.dgvView.CurrentRow.Cells(1).Value = Id.Text
view.dgvView.CurrentRow.Cells(2).Value = fruit.Text
view.dgvView.CurrentRow.Cells(3).Value = veggie.Text
view.dgvView.CurrentRow.Cells(4).Value = pasta.Text
view.dgvView.CurrentRow.Cells(5).Value = drink.Text
End Sub
form 3 (where error 2 is, the one in bold and the code above is the code that I don't fully understand )
Public Class sky
Private Sub dgvView_CellContentClick(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles sky.CellContentClick
If e.ColumnIndex = 1 Then
Dim entry As New sky(Me)
entry.ShowInTaskbar = False
entry.ShowDialog()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles blue.Click
Me.Close()
**sky**.Show()
End Sub
End Class
frmEntry is not a variable, it is a type (a class name). You can use the variable entry created with Dim entry As New frmEntry(Me) to access the form object. But since this variable is a local variable, it is only accessible inside of the Sub that declared it. Declare it on the class level (outside of the Subs), so that other Subs can access it.
Public Class frmView
Dim entry As frmEntry ' <===== declare here
Private Sub FirstSub()
entry = New frmEntry(Me) ' <==== Create and assign here
entry.ShowInTaskbar = False
entry.Show()
End Sub
Private Sub SecondSub()
entry.Close() ' <=== Reference it from somewhere else
End Sub
End Class
Note that a class (and therefore also a Form) is a reference type. Other than a value type like Integer, a variable of a reference type does not store the object it is assigned, but a reference to this object. See the difference:
Dim a, b As Integer
Dim f1, f2 As frmTest
a = 5
b = a
b = 10
' Now a = 5 and b = 10
f1 = new frmTest()
f1.Text = "Test 1"
f2 = f1
f2.Text = "Test 2"
' Now f1.Text = "Test 2" and f2.Text = "Test 2" because there is only one Form
' object referenced by the two variables.

ADODB vb.net ComboBox value to Textbox working on the last index only

I'm having a problem here in vb.net where I need to get the value of the combobox value to textbox. but it only shows the last index of the combobox. /
//here is my code
Private Sub cboname_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboname.SelectedIndexChanged
strSql = "SELECT * FROM tblCashier WHERE ProductCode= '" & cboname.Text & "' "
Do Until myRecord.EOF
txtname.Text = myRecord.Fields("Product").Value
myRecord.MoveNext()
Loop
Call executeQuery2(strSql)
Call getRecord()
End Sub
// ADODB CONNECTIONS
Dim strSql As String
Dim myRecord As New ADODB.Recordset
// COMBO BOX VALUES
Sub fillcombo()
strSql = "SELECT * FROM tblCashier"
Do While Not myRecord.EOF
cboname.Items.Add(myRecord.Fields("ProductCode").Value)
myRecord.MoveNext()
Loop
cboname.Refresh()
Call executeQuery(strSql)
Call getRecord()
End Sub
// FORM_LOAD
Private Sub myPOS_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call openConnection()
Call getRecord()
Call fillcombo()
End Sub
This sort of thing should be done something like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using adapter As New OleDbDataAdapter("SQL query here", "connection string here")
Dim table As New DataTable
adapter.Fill(table)
BindingSource1.DataSource = table
With ComboBox1
.DisplayMember = "ColumnNameToDisplay"
.ValueMember = "PrimaryKeyColumnName"
.DataSource = BindingSource1
End With
TextBox1.DataBindings.Add("Text", BindingSource1, "OtherColumnNameToDisplay")
End Using
End Sub
The TextBox will then automatically update as the selection in the ComboBox changes.

how to create log in password in vb encrypted

Hi i have been searching the internet for 3 weeks now on how to create a log in interface with encrypted password . I was a to encrypt the text box for password but I dont know how to implement it . I am using linq to sql dbml to connect to my data based on text only I was able to get it can create but I want it to be more secure and more professionally looking with encrypted one. By the way I used wizard for creating database not hard coded it that is the way I know how to do it. I am totally noob in programming any help will do. Thanks
Public Class User_Log_In_v7
Dim admin As New GeneralsDataContext
Private Sub User_Log_InBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.User_Log_InBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.User_Log_In_DataSet)
End Sub
Private Sub User_Log_In_v7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'User_Log_In_DataSet.User_Log_In' table. You can move, or remove it, as needed.
'Me.User_Log_InTableAdapter.Fill(Me.User_Log_In_DataSet.User_Log_In)
End Sub
Private Sub btnLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogIn.Click
Try
Dim check = From Storage In admin.User_Log_Ins _
Where UsernameTextBox.Text = Storage.Username And PasswordTextBox.Text = Storage.Password
If Not check.Count = 0 Then
Membership_Information.Show()
Me.Hide()
UsernameTextBox.Text = ""
PasswordTextBox.Text = ""
Else
MsgBox("Please check username or password and try again", MsgBoxStyle.Exclamation, "")
UsernameTextBox.Text = ""
PasswordTextBox.Text = ""
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
This is the code I can from another tutorial for encrypting
Imports System.Security.Cryptography
Imports System.Text
Public Class Form1
Dim DES As New TripleDESCryptoServiceProvider
Dim MD5 As New MD5CryptoServiceProvider
'hash function
Function MD5Hash(value As String) As Byte()
Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
End Function
'Encryption
Function Encrypt(input As String, Key As String) As String
DES.Key = MD5Hash(Key)
DES.Mode = CipherMode.ECB
Dim buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(input)
Return Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length))
End Function

Visual basic / visual studio 2010 windows form loads blank

I am new to visual basic so hopefully this is a simple question. I have a menu with buttons to call different forms. The forms are designed and have labels and text fields and buttons and so on. From the main menu I have tried calling the forms two different ways. One way the forms open and look correct and function. The other way the form opens as a small blank square with no fields. Ultimately I want to create a set of List objects when the main menu opens and pass them back and forth to the other forms for input and processing. I'm using parallel Lists as a temporary database for a simple school lab. I just don't see what is wrong with the way I am calling the form. I haven't even bothered worrying about passing the List objects properly yet.
Public Class frmMain
Dim arrGames As New List(Of String)
Dim arrDates As New List(Of String)
Dim arrPrices As New List(Of Decimal)
Dim arrSeats As New List(Of Integer)
Private Sub btnEnterGames_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterGames.Click
'NewEnter.Visible = True
Dim frmEnter As New NewEnter(arrGames, arrDates, arrPrices, arrSeats)
frmEnter.ShowDialog()
End Sub
Private Sub btnReports_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReports.Click
'Reports.Visible = True
Dim frmReports As New Reports(arrGames, arrDates, arrPrices, arrSeats)
frmReports.Visible = True
End Sub
Private Sub btnSellTickets_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSellTickets.Click
'SellTickets.Visible = True
Dim frmSell As New SellTickets(arrGames, arrDates, arrPrices, arrSeats)
frmSell.Visible = True
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Close()
End Sub
End Class
This is the code for the form NewEnter. I have the New routine which accepts the 4 Lists and basically does nothing else. Doing the "'NewEnter.Visible = True" in the main menu will load the form correctly but I have to comment out the New sub routine in the forms or there is an error.
Public Class NewEnter
Private _arrGames As List(Of String)
Private _arrDates As List(Of String)
Private _arrPrices As List(Of Decimal)
Private _arrSeats As List(Of Integer)
Sub New(ByVal arrGames As List(Of String), ByVal arrDates As List(Of String), ByVal arrPrices As List(Of Decimal), ByVal arrSeats As List(Of Integer))
' TODO: Complete member initialization
' _arrGames = arrGames
' _arrDates = arrDates
' _arrPrices = arrPrices
' _arrSeats = arrSeats
End Sub
Private Sub btnSaveGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveGame.Click
Dim arrGames As New List(Of String)
Dim arrDates As New List(Of String)
Dim arrPrices As New List(Of Decimal)
Dim arrSeats As New List(Of Integer)
Dim strGame As String
Dim strPrice As String
Dim strSeats As String
Dim intSeats As Integer
Dim decPrice As Decimal
Dim bolGameErr As Boolean
Dim bolDateErr As Boolean
Dim bolPriceErr As Boolean
Dim bolSeatErr As Boolean
strGame = txtGame.Text
strPrice = txtPrice.Text
strSeats = txtSeats.Text
'~~~~~~~~~~~~verify a game is entered
If String.IsNullOrEmpty(strGame) Or String.IsNullOrWhiteSpace(strGame) Then
bolGameErr = True
Else
'~~~~~~~~~~~~verify price is numeric
If IsNumeric(strPrice) Then
decPrice = strPrice
'~~~~~~~~~~~~~~~verify seats are numeric
If IsNumeric(strSeats) Then
intSeats = Convert.ToInt32(strSeats)
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ add elements to array lists
arrGames.Add(New String(strGame))
arrDates.Add(dtpDate.Text)
arrPrices.Add(New Decimal(decPrice))
arrSeats.Add(intSeats)
lblSaveSuccessful.Visible = True
ClearInput()
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ add elements to array lists
Else
bolSeatErr = True
End If
Else
bolPriceErr = True
End If
End If
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Check flags for input errors
If bolDateErr = True Then
lblErr.Text = "Invalid date"
lblErr.Visible = True
End If
If bolGameErr = True Then
lblErr.Text = "Must enter a game name"
lblErr.Visible = True
txtGame.Focus()
End If
If bolDateErr = True And bolGameErr = True Then
lblErr.Text = "Must enter a game name and valid date"
lblErr.Visible = True
txtGame.Focus()
End If
If bolPriceErr = True Then
lblPriceErr.Visible = True
txtPrice.Text = ""
txtPrice.Focus()
End If
If bolSeatErr = True Then
lblSeatErr.Visible = True
txtSeats.Text = ""
txtSeats.Focus()
End If
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Check flags for input error
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Display output
Dim i As Integer
i = 0
lblData.Text = arrGames.Count.ToString
Do While i < arrGames.Count
lblData.Text = Convert.ToString(arrGames(i)) & " on " & Convert.ToString(arrDates(i)) & " Price: " & _
Convert.ToString(arrPrices(i)) & " Available Seats: " & Convert.ToString(arrSeats(i))
i += 1
Loop
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Display output
lblData.Visible = True
End Sub
Private Sub ClearInput()
'lblErr.Visible = False
'lblPriceErr.Visible = False
'lblSeatErr.Visible = False
txtGame.Text = ""
txtPrice.Text = ""
txtSeats.Text = ""
txtGame.Focus()
End Sub
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Me.Visible = True
'Me.BackColor = Color.BurlyWood
'Me.ResumeLayout()
'Me.Activate()
'Me.Focus()
'Me.Show()
'Me.lblGameHdr.Visible = True
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Close()
End Sub
End Class
Add InitializeComponent() to your constructor class.
This is added by default to the New (constructor) function of all Visual Basic forms. It requires it to set-up the UI components on the form.

Public variable used for form opening not feeding through to from

Having some issues getting a form to populate based on a variable determined in current form.
I have a search result form that has a datagrid with all results, with an open form button for each row. When the user clicks this, the rowindex is used to pull out the ID of that record, which then feeds to the newly opened form and populates based on a SQL stored procedure run using the ID as a paramter.
However, at the moment the variable is not feeding through to the form, and am lost as to why that is. Stored procedure runs fine if i set the id within the code. Here is my form open code, with sci
Public Class SearchForm
Dim Open As New FormOpen
Dim data As New SQLConn
Public scid As Integer
Private Sub Search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sql As New SQLConn
Call sql.SearchData()
dgvSearch.DataSource = sql.dt.Tables(0)
End Sub
Private Sub dgvSearch_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSearch.CellContentClick
Dim rowindex As Integer
Dim oform As New SprinklerCardOpen
rowindex = e.RowIndex.ToString
scid = dgvSearch.Rows(rowindex).Cells(1).Value
TextBox1.Text = scid
If e.ColumnIndex = 0 Then
oform.Show()
End If
End Sub
End Class
The form opening then has the follwing:
Private Sub SprinklerCard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Populate fields from SQL
Try
Call Populate.SprinklerCardPopulate(ID)
cboInsured.Text = Populate.dt.Tables(0).Rows(0).Item(1)
txtAddress.Text = Populate.dt.Tables(0).Rows(0).Item(2)
txtContactName.Text = Populate.dt.Tables(0).Rows(0).Item(3)
txtContactPhone.Text = Populate.dt.Tables(0).Rows(0).Item(4)
txtContactEmail.Text = Populate.dt.Tables(0).Rows(0).Item(5)
numPumps.Value = Populate.dt.Tables(0).Rows(0).Item(6)
numValves.Value = Populate.dt.Tables(0).Rows(0).Item(7)
cboLeadFollow.Text = Populate.dt.Tables(0).Rows(0).Item(8)
cboImpairment.Text = Populate.dt.Tables(0).Rows(0).Item(9)
txtComments.Text = Populate.dt.Tables(0).Rows(0).Item(10)
Catch ex As Exception
MsgBox(ex.ToString & "SCID = " & ID)
End Try
End Sub
Set the ID variable in the form before you open it.
If e.ColumnIndex = 0 Then
oform.ID = scid
oform.Show()
End If