Too many aurguments to public overloads submenu in vb.net combobox
error in Function in vb.net combobox
Public Function combo(strQuery As String) As List(Of ComboBox)
Dim objlist As New List(Of ComboBox)
con.Open()
Dim dt As New DataTable
For Each dr In ExecuteReader(strQuery).Rows
objlist.Add(New ComboBox(Convert.ToInt32(dr.Item(0).ToString()), dr.Item(1).ToString()))
Next
con.Close()
Return objlist
End Function
<Extension>
Public Function ExecuteReader(strQuery As String) As DataTable
Dim dt As New DataTable
If con.State = ConnectionState.Open Then con.Close()
con.Open()
cmd = New SqlCommand(strQuery, con)
da = New SqlDataAdapter(cmd)
da.Fill(dt)
con.Close()
Return dt
End Function
The ComboBox constructor does not take any parameters. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.-ctor?view=windowsdesktop-6.0
I think your function should read
Private Class ComboBoxItem
Public Property Id As Integer
Public Property Text As String
Public Sub New(Id As Integer, Text As String)
Me.Id = Id
Me.Text = Text
End Sub
End Class
Public Function GetPopulatedCombo(strQuery As String) As ComboBox
Dim Combo As ComboBox = New ComboBox
Combo.DisplayMember = "Text"
Using DT = ExecuteReader(strQuery)
For Each dr In DT.Rows
Combo.Items.Add(New ComboBoxItem(dr.Item(0),dr.Item(1))
Next
End Using
Return Combo
End Function
Related
Combobox population does not appear in DataGridView because of the function of DataTable.
below link the previous code running normally with the combobox population.
please recommend the best solution.
I have also coded in my post so it doesn't miss communication
Thanks
link previous post
Private _dt As DataTable
'update code PopulateComboBox in form load
Public Sub New()
If Me.IsInDesignMode() Then
Return
End If
InitializeComponent()
Me.PopulateComboBox()
fillDataGridView1()
End Sub
Private Function GetAndFillDataTable() As DataTable
Dim dt As New DataTable()
Dim query As String = "SELECT NOD,ITM,CIA,DPR,QTY FROM RSD WHERE QTY > 0 AND PNM=#PNM"
Using con As OleDbConnection = New OleDbConnection(GetConnectionString)
Using cmd As OleDbCommand = New OleDbCommand(query, con)
cmd.Parameters.AddWithValue("#PNM", ComboBox1.SelectedValue)
Using da As New OleDbDataAdapter(cmd)
da.Fill(dt)
da.Dispose()
Dim totalColumn As New DataColumn()
totalColumn.DataType = System.Type.GetType("System.Double")
totalColumn.ColumnName = "Total"
totalColumn.Expression = "[CIA]*[QTY]*(1-[DPR]/100)"
dt.Columns.Add(totalColumn)
Return dt
End Using
End Using
End Using
End Function
Private Sub FillDataGridView1()
If (_dt Is Nothing) Then
_dt = GetAndFillDataTable()
End If
grid.DataSource = _dt
grid.Refresh()
End Sub
Private Sub PopulateComboBox()
Dim dt As New DataTable()
Dim query As String = "SELECT DISTINCT PNM FROM RSD UNION SELECT DISTINCT PNM FROM RSG ORDER BY PNM"
Try
dt = New DataTable
Using con As OleDbConnection = New OleDbConnection(GetConnectionString)
Using sda As OleDbDataAdapter = New OleDbDataAdapter(query, con)
sda.Fill(dt)
Dim row As DataRow = dt.NewRow()
row(0) = ""
dt.Rows.InsertAt(row, 0)
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "PNM"
ComboBox1.ValueMember = "PNM"
End Using
End Using
Catch myerror As OleDbException
MessageBox.Show("Error: " & myerror.Message)
Finally
End Try
'update code combobox selectionchangecommited for fillDataGridView1
Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
fillDataGridView1()
End Sub
You are setting your datasource = _dt in PopulateComboBox however the data table you built in your function is called dt.
Imports System.Data.SqlClient
Public Class SQLCONTROL
Private DBcon As New SqlConnection("Data Source=DESKTOP-DQ7NOIF\SQLEXPRESS;Integrated Security=True")
Private DBcom As New SqlCommand
Public DBDA As SqlDataAdapter
Public DBDT As DataTable
Public Params As New List(Of SqlParameter)
Public RecordCount As Integer
Public Exception As String
Public Sub New()
End Sub
Public Sub New(ConnctionString As String)
DBcon = New SqlConnection(ConnctionString)
End Sub
Public Sub ExecQuery(Query As String)
RecordCount = 0
Exception = ""
Try
DBcon.Open()
DBcom = New SqlCommand(Query, DBcon)
Params.ForEach(Sub(p) DBcom.Parameters.Add(p))
DBDT = New DataTable
DBDA = New SqlDataAdapter(DBcom)
RecordCount = DBDA.Fill(DBDT)
Catch ex As Exception
Exception = "ExecQuery Error" & vbNewLine & ex.Message
Finally
If DBcon.State = ConnectionState.Open Then DBcon.Close()
End Try
End Sub
Public Sub AddParam(Name As String, Value As Object)
Dim NewParam As New SqlParameter(Name, Value)
Params.Add(NewParam)
End Sub
Public Function HasException(Optional Report As Boolean = False) As Boolean
If String.IsNullOrEmpty(Exception) Then Return False
If Report = True Then MsgBox(Exception, MsgBoxStyle.Critical, "Exception:")
Return True
End Function
End Class
the exception message is "A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Nom d'objet 'Utilisateurs' non valide."
thanks
Many database objects (including connections and commands) expose a .Dispose method which means that they may be using unmanaged resources which need to be released. To guarantee that .Dispose is called .net provides Using...End Using.
Any errors will bubble up to the calling code where you can handle them and inform the user. After all, your class could be used in a web app or phone app where MsgBox would not work. Leave informing the user to the User Interface code.
Public Class SQLCONTROL
Private ConStr As String = "Data Source=DESKTOP-DQ7NOIF\SQLEXPRESS;Integrated Security=True"
Public Function IsUserValid(userName As String, password As String) As Boolean
Dim RetVal As Integer
Using DBcon As New SqlConnection(ConStr),
DBcom As New SqlCommand("Select Count(*) From [Utilisateurs] Where UserName = #UserName And Password = #Password;", DBcon)
DBcom.Parameters.Add("#UserName", SqlDbType.NVarChar, 50).Value = userName
DBcom.Parameters.Add("#Password", SqlDbType.NVarChar, 100).Value = password
DBcon.Open()
RetVal = CInt(DBcom.ExecuteScalar)
End Using
If RetVal = 0 Then
Return False
End If
Return True
End Function
'To return a DataTable for a DataGridView
Public Function GetCustomerOrders(Customer As String) As DataTable
Dim dt As New DataTable
Using DBcon As New SqlConnection(ConStr),
DBcom As New SqlCommand("Select * From Orders Where CustomerName = #CustomerName;", DBcon)
DBcom.Parameters.Add("#CustomerName", SqlDbType.NVarChar, 100).Value = Customer
DBcon.Open()
dt.Load(DBcom.ExecuteReader)
End Using
Return dt
End Function
End Class
Usage in Form
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim sql As New SQLCONTROL
If sql.IsUserValid("Mary", "MySecretPassword") Then
'Do something
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim sql As New SQLCONTROL
DataGridView1.DataSource = sql.GetCustomerOrders("General Motors")
End Sub
Naturally, I only guessed at field names, types, and sizes. Check your database.
In a real application you would never store passwords as plain text.
I have a DataGridViewComboBoxColumn that works and populates well. All I want to know is how to add a blank field. This way if the user inadvertently selects something they can select the blank row rather than pressing Ctrl-0.
In my code I tried adding Items.Add(DBNull.Value) but to no avail, as shown in my code. All it does is give me an error which is why it is rem'd out.
Any ideas would be greatly appreciated.
Private Sub FillCombobox(strHeaderText As String, strPropertyName As String, strTableName As String)
Using cboBox As New DataGridViewComboBoxColumn
With cboBox
.HeaderText = strHeaderText
.DataPropertyName = strPropertyName
.DataSource = GetData("Select * From " & strTableName)
.ValueMember = .DataPropertyName
'.Items.Add(DBNull.Value)
End With
dgrMain.Columns.Add(cboBox)
End Using
End Sub
EDIT
Private Function GetData(ByVal sqlCommand As String) As DataTable
Dim da = New OleDbDataAdapter(sqlCommand, strConn)
Dim con As New OleDbConnection(strConn)
con.Open()
Dim command As OleDbCommand = New OleDbCommand(sqlCommand, con)
da.SelectCommand = command
Dim dt As DataTable = New DataTable()
da.Fill(dt)
con.Close()
Return dt
End Function
With this line of code
using (rdr as mysqldatareader = cmd.executereader())
I get an error saying rdr is not declared. When I remove the brackets so that it becomes
Using rdr As MySqlDataReader = cmd.ExecuteReader()
I get error on this line tmpObj.No = rdr("No").ToString()saying No is not a member of the project.Form.Appdata and this line
tmpObj.Template = templa8 and Template is not a member of the project.Form.AppData. Note that I have already put this: Private FPList As New List(Of AppData) at the class level, as a member. Definitely I'm doing something wrong. Any suggestions?
'THIS NEEDS TO BE AT THE CLASS-LEVEL, AS A MEMBER
'Private FPList As New List(Of AppData)
Public Class AppData
Public Sub Update()
RaiseEvent OnChange()
End Sub
Public Event OnChange()
Public FPList As New List(Of AppData)
Public IsEventHandlerSucceeds As Boolean = True
Public IsFeatureSetMatched As Boolean = False
Public FalseAcceptRate As Integer = 0
Public Sub Update()
RaiseEvent OnChange()
End Sub
Public Event OnChange()
Public FPList As New List(Of AppData)
End Class
Private Sub Me_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Init()
StartCapture()
Dim sql As String = "SELECT * FROM new_case_file"
Using conn As New MySqlConnection("**** "), _
cmd As New MySqlCommand(sql, conn)
conn.Open()
Using (rdr As MySqlDataReader = cmd.ExecuteReader())
FPList.Clear()
While (rdr.Read())
Dim tmpObj As New AppData
tmpObj.No = rdr("No").ToString()
Dim fpBytes As Byte() = rdr("FingerPrint")
Using MemStream As New IO.MemoryStream(fpBytes)
Dim templa8 As New DPFP.Template()
templa8.DeSerialize(MemStream)
tmpObj.Template = templa8
End Using
FPList.Add(tmpObj)
End While
rdr.Close()
End Using
End Using
End Sub
You correct that error by adding the following to your AppData class
Private _No As String
Public Property No As String
Get
Return _No
End Get
Set(value as String)
_No = value
End Set
End Property
Private _Template As DPFP.Template
Public Property Template As DPFP.Template
Get
Return _Template
End Get
Set(value as DPFP.Template)
_Template = value
End Set
End Property
but that will not necessarily make your code work.
Seems to me that the combobox will only display string values. I have a table column that is integer that I need to be displayed in the combobox for the user to pick.
Dim cmd As New SqlCommand("Select ID From ATable", con)
Dim ds As New DataSet()
Dim adap As New SqlDataAdapter(cmd)
If con.State = ConnectionState.Closed Then con.Open()
adap.Fill(ds, "ATable")
con.Close()
comboBox1.DisplayMember = "ID"
comboBox1.DataSource = ds.Tables("ATable")
comboBox1.SelectedIndex = -1
combobox is just blank and I'm thinking it's because my fields are integers and not string. How can I make it display my integer values. EDIT: I needed to put the function in my public sub form initializecomponent()
put the function in public sub form after initializecomponent()