how to link dropdown menu to other class - sql

i have a problem where i have to link dropdown menu to other class, which the class is sqlcommand
so my code a bit much like this
Form1.vb
Public Class Input
Dim CallSQL As New SQL Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Visible = True
If ListBox1.SelectedValue = "A" Then
-------"I DONT KNOW HOW TO COMMAND HERE "--
End If
If ListBox1.SelectedValue = "B" Then
-------"I DONT KNOW HOW TO COMMAND HERE "---------
End If
End Sub
End Class
and i need to match the dropdown from here
SQL.vb
Public Class SQL
Private Sub GetRoadDataFromDatabase(station)
ExecuteSqlCommand("exec '" & station & "', 'A'", "TableA")
End Sub
Public Sub GettrainDataFromDatabase(station)
ExecuteSqlCommand("exec '" & station & "', 'B'", "TableB")
End Sub
so when user choose A, executesql data for road, and when choose B execute data for train. please help me.this suppose to be a simple task. but since im new in vb.bet i cant figure it out

You want to execute GetRoadDataFromDatabase if user selects option 'A' and GetTrainDataFromDatabase if user selects B. Simply create instance of SQL class outside of IF/ELSE statements and call relevant method in IF/ELSE:
Public Class Input
Dim CallSQL As New SQL Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Visible = True
Dim sql AS SQL =new SQL() 'Please check this if it is valid VB.Net statement
If ListBox1.SelectedValue = "A" Then
'Supposing you have got station already
sql.GetRoadDataFromDatabase(station)
End If
If ListBox1.SelectedValue = "B" Then
'Supposing you have got station already
sql.GettrainDataFromDatabase(station)
End If
End Sub
End Class
EDIT:- Not making methods public
As you have stated that you don't want to make the two functions public, an option is to write a gateway function to accept parameters and then call the desired private method.
Public Class SQL
Private Sub GetRoadDataFromDatabase(station)
ExecuteSqlCommand("exec '" & station & "', 'A'", "TableA")
End Sub
'THOUGH YOUR THIS FUNCTION IS PUBLIC IN QUESTION
Private Sub GettrainDataFromDatabase(station)
ExecuteSqlCommand("exec '" & station & "', 'B'", "TableB")
End Sub
Public Sub CallDBFunction(station as string, code as string)
If code == "A" Then
GetRoadDataFromDatabase(station)
Else
GetTrainDataFromDatabse(station)
End If
End Sub
End Class

Related

Passing information between two forms from treeview

I have a sub that brings a second form with a treeview on it that is populated from an array. I want to click on an item on the array and pass the key and text back to the sub and close the second form.
I feel like this should be easy but I cannot figure this out. The array is passed to the treeview as follows.
For j = 0 To NoFlowsheets - 1
Form2.TreeView1.Nodes.Add("Flowsheet" & CStr(j), ColumnNames(j, 0))
For k = 0 To j_max - 1
If ColumnNames(j, k) <> "NAME_EMPTY DO_NOT_USE_THIS_NAME" Then
Form2.TreeView1.Nodes(j).Nodes.Add("Flowsheet" & CStr(j), ColumnNames(j, k))
End If
Next k
Next j
Form2.ShowDialog()
after this a form pops up with the treeview. I want the user to click on one of the items in the tree view and pass it back to the sub
This is a very basic example, but it shows how you can pass something into a form, and how to get back what the user selected/entered. You can easily modify this to pass in your array, and pass back the selected value(s).
On the first form (where you open the child form), you add code like this:
Public Class frmStart
Private Sub btnAskUser_Click(sender As Object, e As EventArgs) Handles btnAskUser.Click
Dim frmAskUserAboutThemselves As New frmQuestion(19, "John Doe")
frmAskUserAboutThemselves.ShowDialog(Me)
If frmAskUserAboutThemselves.WasRecordSaved = True Then
lblStatus.Text = "Name: " & frmAskUserAboutThemselves.ValueThatUserSelectedOnTheFormName & vbCrLf & "Age: " & frmAskUserAboutThemselves.ValueThatUserSelectedOnTheFormAge
Else
lblStatus.Text = "The user did not enter/select any values."
End If
frmAskUserAboutThemselves.Dispose()
Beep()
End Sub
End Class
On the child form (where you ask the user to select something), you add code like this:
Public Class frmQuestion
#Region " Override Windows Form Designer Generated Code "
Public Sub New(Optional ByVal iAge As Integer = 0, Optional ByVal sName As String = "")
MyBase.New()
m_iPassedInPersonAge = iAge
m_sPassedInPersonName = sName
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
#End Region
#Region " Form Level Variables "
Private m_iPassedInPersonAge As Integer = 0
Private m_sPassedInPersonName As String = ""
Private m_bWasRecordSaved As Boolean = False
#End Region
#Region " Form Level Functions "
Public ReadOnly Property WasRecordSaved() As Boolean
Get
Return m_bWasRecordSaved
End Get
End Property
Public ReadOnly Property ValueThatUserSelectedOnTheFormName() As String
Get
Return m_sPassedInPersonName
End Get
End Property
Public ReadOnly Property ValueThatUserSelectedOnTheFormAge() As Integer
Get
Return m_iPassedInPersonAge
End Get
End Property
#End Region
#Region " Normal Page Code "
Private Sub frmQuestion_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtName.Text = m_sPassedInPersonName
NumericUpDownAge.Value = m_iPassedInPersonAge
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
m_bWasRecordSaved = False
Me.Close()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
m_iPassedInPersonAge = NumericUpDownAge.Value
m_sPassedInPersonName = txtName.Text.Trim
m_bWasRecordSaved = True
Me.Close()
End Sub
#End Region
End Class
If you are unsure what controls i placed on each form, just ask, but it should be pretty easy to figure that out.

How to jump to a row of a DataView based on partial match search criteria?

Currently, my application uses the RowFilter property on an expression to search for a user-defined string within a DataView. Currently my code looks something like this:
Public Class MyClass
Private custView As DataView
Private Sub form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dsDataSet = <"DataAccessLayer call to SQL Stored Procedure">
custView = New DataView(dsDataSet.Tables(0))
custView.Sort = "Column Name"
Me.C1FlexGrid1.DataSource = custView
End Sub
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
Dim searchText As String = txtSearch.Text
Dim expression As String = "Column Name LIKE '" + searchText + "%'"
custView.RowFilter = expression
Me.C1FlexGrid1.DataSource = custView
End Sub
End Class
My goal is to modify the behavior of this such that instead of filtering out rows that do not meet the search results, it will keep all rows present but jump to the first instance of a partial match as the user types in the search box. If DataView.Find() supported wildcards I would be set, but unfortunately it doesn't.
The solution I've come up with is to use some iteration logic. However this is done on the object the DataView has been bound to and not the DataView itself. Although this code can be modified to do exactly that.
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
'Unselect all
Me.C1FlexGrid1.Select(-1, -1, True)
If txtSearch.Text <> "" And [column index] <> -1 Then
'Typed text
Dim s As String = txtSearch.Text.Trim.ToLower
Dim column As Int32 = [column index] + 1
'Recurse until match in first column is found
For i As Integer = 0 To Me.C1FlexGrid1.Rows.Count - 1
If C1FlexGrid1.GetData(i, column) <> Nothing Then
If C1FlexGrid1.GetData(i, column).ToString.ToLower.StartsWith(s) Then
Me.C1FlexGrid1.Select(i, column, True)
Exit Sub
End If
Else
MsgBox("Error message", vbOKOnly, "NO MATCHES")
'Reset search criteria
Call ResetSearch()
End If
Next
MsgBox("Error message", vbOKOnly, "NO MATCHES")
End If
End Sub

Invalid cast Eception unhandled by user code in 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.

How do you change the value in my.settings in a form when you enter numbers in a textbox in VB.Net

I was wondering if you could help me? My question is that, is there a way of changing the value in my.Settings in a form if you enter a number/decimal in a textbox and click a button and then update in the settings to be then changed in another from which is linked to my.Settings in a variable?!
Form 1:
Public Class frmConverter
Dim input As String
Dim result As Decimal
Dim EUR_Rate As Decimal = My.Settings.EUR_Rates
Dim USD_Rate As Decimal = 1.6
Dim JYP_Rate As Decimal = 179.65
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
input = txtInput.Text
Try
If ComboBox1.Text = "£" Then
Pounds()
ElseIf ComboBox1.Text = "€" Then
Euros()
ElseIf ComboBox1.Text = "$" Then
Dollars()
ElseIf ComboBox1.Text = "¥" Then
Yen()
End If
Catch es As Exception
MsgBox("Error!")
End Try
End Sub
Private Sub btnSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSettings.Click
Me.Hide()
frmExchange.Show()
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
txtInput.Text = ""
lblResult.Text = ""
End Sub
Function Pounds()
If ComboBox1.Text = "£" And ComboBox2.Text = "€" Then
result = (input * EUR_Rate)
lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text
ElseIf ComboBox1.Text = "£" And ComboBox2.Text = "$" Then
result = (input * USD_Rate)
lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text
ElseIf ComboBox1.Text = "£" And ComboBox2.Text = "¥" Then
result = (input * JYP_Rate)
lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text
End If
Return 0
End Function
Form 2:
Public Class frmExchange
Private Sub frmExchange_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
My.Settings.EUR_Rates = (txtinput.Text)
My.Settings.Save()
My.Settings.Reload()
End Sub
Public Sub SetNewRate(ByVal rate As Decimal)
txtinput.Text = rate.ToString
End Sub
Private Sub btnchange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnchange.Click
If ComboBox1.Text = "€" Then
My.Settings.USD_Rates = (txtinput.Text)
frmConverter.SetNewRate(txtinput.Text)
End If
End Sub
End class
It sounds like you are trying to use My.Settings as some sort of set of global reference variables. Thats not what they are for, not how they work and not what they are.
First, turn on Option Strict as it looks like it may be Off. This will store the decimal value of a textbox to a Settings variable which is defined as a Decimal:
My.Settings.USD_Rates = CDec(SomeTextBox.Text)
Thats all it will do. It wont save the value and it wont pass it around or share it with other forms and variables.
My.Settings.Save 'saves current settings to disk for next time
My.Settings.Reload 'Load settings from last time
This is all covered on MSDN. There is no linkage anywhere. If you have code in another form like this:
txtRate.Text = My.Settings.USD_Rates.ToString
txtRate will not automatically update when you post a new value to Settings. There are just values not Objects (see Value Types and Reference Types). To pass the new value to another form:
' in other form:
Public Sub SetNewRate(rate As Decimal)
' use the new value:
soemTextBox.Text = rate.ToString
End Sub
in form which gets the change:
Private Sub btnchangeRate(....
' save to settings which has nothing to do with passing the data
My.Settings.USD_Rates = CDec(RateTextBox.Text)
otherForm.SetNewRate(CDec(RateTextBox.Text))
End Sub
You may run into problems if you are using the default form instance, but that is a different problem.
You botched the instructions. The 2 procedures are supposed to go in 2 different forms - one to SEND the new value, one to RECEIVE the new value. With the edit and more complete picture, there is an easier way.
Private Sub btnSettings_Click(...) Handles btnSettings.Click
' rather than EMULATE a dialog, lets DO a dialog:
'Me.Hide()
'frmExchange.Show()
Using frm As New frmExchange ' the proper way to use a form
frm.ShowDialog
' Im guessing 'result' is the xchg rate var
result = CDec(frm.txtInput.Text)
End Using ' dispose of form, release resources
End Sub
In the other form
Private Sub btnchange_Click(....)
' do the normal stuff with Settings, if needed then:
Me.Hide
End Sub

How to Pass MethodName as Parameter of a Procedure in VBNET

I want to create a Procedure that its parameter is also a procedure. Is it possible?
I created some procedures to be used as parameters below:
Private Sub Jump(xStr as string)
Msgbox xStr & " is jumping."
End Sub
Private Sub Run(xStr as string)
Msgbox xStr & " is jumping."
End Sub
this procedure should call the procedure above:
Private Sub ExecuteProcedure(?, StringParameter) '- i do not know what to put in there
? ' - name of the procedure with parameter
End Sub
usage:
ExecuteProcedure(Jump, "StringName")
ExecuteProcedure(Run, "StringName")
I believe the following code is an example for what you need.
Public Delegate Sub TestDelegate(ByVal result As TestResult)
Private Sub RunTest(ByVal testFunction As TestDelegate)
Dim result As New TestResult
result.startTime = DateTime.Now
testFunction(result)
result.endTime = DateTime.Now
End Sub
Private Sub MenuItemStartTests_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemStartTests.Click
Debug.WriteLine("Starting Tests...")
Debug.WriteLine("")
'==================================
' Add Calls to Test Modules Here
RunTest(AddressOf Test1)
RunTest(AddressOf Test2)
'==================================
Debug.WriteLine("")
Debug.WriteLine("Tests Completed")
End Sub
The entire article can be found at http://dotnetref.blogspot.com/2007/07/passing-function-by-reference-in-vbnet.html
Hope this helps.