Search bar not working; column not found? - mysql

Ok. I am making a BIG program that should be released today but sadly i delayed because of this problem while testing. I have administration area to recover people informatio just in case and i noticed search bar dosen't work giving me a error that column is not working. I am using database MySql. Programming in vb.net. The error is: Cannot find column [nusername]. But it does exist. Everything works correctly. And yes i know about the sql injection ignore it. Here is my code but please don't steal my coding:
Imports MySql.Data.MySqlClient
Public Class DeathLairAdminControlPanel
Dim MySqlConn As MySqlConnection
Dim MySqlCmd As MySqlCommand
Dim dbDT As New DataTable
Public Property AUsernameHomePass As String
Private Sub DeathLairAdminControlPanel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LabelALoggedIn.Text = AUsernameHomePass
End Sub
Private Sub ButtonUpdateU_Click(sender As Object, e As EventArgs) Handles ButtonUpdateU.Click
If TextBoxNUsern.Text = "" Or TextBoxNEmail.Text = "" Or TextBoxNPass.Text = "" Or TextBoxNPhone.Text = "" Then
MessageBox.Show("Please don't leave empty areas.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
ElseIf TextBoxNUsern.Text.Length < 4 Then
MessageBox.Show("Username too short.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
ElseIf TextBoxNEmail.Text.Length < 8 Then
MessageBox.Show("Email cant be that short. IT MUST BE VALID!.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
ElseIf TextBoxNPass.Text.Length < 6 Then
MessageBox.Show("Password can't be that short. Make it longer and secure.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
ElseIf TextBoxNPhone.Text.Length < 6 Then
MessageBox.Show("Phone number can't be that short. IT MUST BE VALID.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
ElseIf System.Text.RegularExpressions.Regex.IsMatch(TextBoxNUsern.Text, "^[A-Za-z0-9]+$") And System.Text.RegularExpressions.Regex.IsMatch(TextBoxNPass.Text, "^[A-Za-z0-9.]+$") And System.Text.RegularExpressions.Regex.IsMatch(TextBoxNEmail.Text, "\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*") And System.Text.RegularExpressions.Regex.IsMatch(TextBoxNPhone.Text, "^\+[0-9()-+ ]+$") And System.Text.RegularExpressions.Regex.IsMatch(TextBoxNCity.Text, "^[A-Za-z ]+$") Then
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString =
"server=localhost;userid=root;password=HIDDEN;database=syscore"
Dim MySqlRea As MySqlDataReader
Try
MySqlConn.Open()
Dim Query As String
Query = "update syscore.normaluser set nusername='" & TextBoxNUsern.Text & "',nemail='" & TextBoxNEmail.Text & "',npass='" & TextBoxNPass.Text & "',nphone='" & TextBoxNPhone.Text & "',ncity='" & TextBoxNCity.Text & "' where nusername='" & TextBoxNUsern.Text & "'"
MySqlCmd = New MySqlCommand(Query, MySqlConn)
MySqlRea = MySqlCmd.ExecuteReader
MessageBox.Show("User has been updated.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
MySqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MySqlConn.Dispose()
End Try
End If
loaddatabase_table()
End Sub
Private Sub ButtonDeleteU_Click(sender As Object, e As EventArgs) Handles ButtonDeleteU.Click
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString =
"server=localhost;userid=root;password=HIDDEN;database=syscore"
Dim MySqlRea As MySqlDataReader
Try
MySqlConn.Open()
Dim Query As String
Query = "delete from syscore.normaluser where nusername'" & TextBoxNUsern.Text & "'"
MySqlCmd = New MySqlCommand(Query, MySqlConn)
MySqlRea = MySqlCmd.ExecuteReader
MessageBox.Show("User has been deleted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
TextBoxNUsern.Text = ""
TextBoxNEmail.Text = ""
TextBoxNPass.Text = ""
TextBoxNPhone.Text = ""
TextBoxNCity.Text = ""
MySqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MySqlConn.Dispose()
End Try
loaddatabase_table()
End Sub
Private Sub loaddatabase_table()
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString =
"server=localhost;userid=root;password=HIDDEN;database=syscore"
Dim MySqlDAd As New MySqlDataAdapter
Dim dbDataSet As New DataTable
Dim bSource As New BindingSource
Try
MySqlConn.Open()
Dim Query As String
Query = "select * from syscore.normaluser"
MySqlCmd = New MySqlCommand(Query, MySqlConn)
MySqlDAd.SelectCommand = MySqlCmd
MySqlDAd.Fill(dbDataSet)
bSource.DataSource = dbDataSet
DataGridView1.DataSource = bSource
MySqlDAd.Update(dbDataSet)
TextBoxNUsern.Text = ""
TextBoxNEmail.Text = ""
TextBoxNPass.Text = ""
TextBoxNPhone.Text = ""
TextBoxNCity.Text = ""
MySqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MySqlConn.Dispose()
End Try
End Sub
Private Sub ButtonLDB_Click(sender As Object, e As EventArgs) Handles ButtonLDB.Click
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString =
"server=localhost;userid=root;password=HIDDEN;database=syscore"
Dim MySqlDAd As New MySqlDataAdapter
Dim dbDataSet As New DataTable
Dim bSource As New BindingSource
Try
MySqlConn.Open()
Dim Query As String
Query = "select * from syscore.normaluser"
MySqlCmd = New MySqlCommand(Query, MySqlConn)
MySqlDAd.SelectCommand = MySqlCmd
MySqlDAd.Fill(dbDataSet)
bSource.DataSource = dbDataSet
DataGridView1.DataSource = bSource
MySqlDAd.Update(dbDataSet)
TextBoxNUsern.Text = ""
TextBoxNEmail.Text = ""
TextBoxNPass.Text = ""
TextBoxNPhone.Text = ""
TextBoxNCity.Text = ""
MySqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MySqlConn.Dispose()
End Try
loaddatabase_table()
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.RowIndex >= 0 Then
Dim gridrow As DataGridViewRow
gridrow = Me.DataGridView1.Rows(e.RowIndex)
TextBoxNUsern.Text = gridrow.Cells("nusername").Value.ToString
TextBoxNEmail.Text = gridrow.Cells("nemail").Value.ToString
TextBoxNPass.Text = gridrow.Cells("npass").Value.ToString
TextBoxNPhone.Text = gridrow.Cells("nphone").Value.ToString
TextBoxNCity.Text = gridrow.Cells("ncity").Value.ToString
End If
End Sub
Private Sub TextBoxSdbgrid_TextChanged(sender As Object, e As EventArgs) Handles TextBoxSdbgrid.TextChanged
Dim DV As New DataView(dbDT)
' ERROR HERE:
DV.RowFilter = String.Format("nusername Like '%{0}%'", TextBoxSdbgrid.Text)
DataGridView1.DataSource = DV
End Sub
Private Sub ReportToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ReportToolStripMenuItem.Click
MessageBox.Show("To get support or report do it on skype gangsteris33 we are always online until night.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub LoginToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoginToolStripMenuItem.Click
Form1.Show()
TextBoxNCity.Text = ""
TextBoxNEmail.Text = ""
TextBoxNPass.Text = ""
TextBoxNPhone.Text = ""
TextBoxNUsern.Text = ""
TextBoxSdbgrid.Text = ""
Me.Hide()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub ButtonClearChat_Click(sender As Object, e As EventArgs) Handles ButtonClearChat.Click
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString =
"server=localhost;userid=root;password=HIDDEN;database=sysinfo"
Dim MySqlRea As MySqlDataReader
Try
MySqlConn.Open()
Dim Query As String
Query = "delete from syscore.normaluser"
MySqlCmd = New MySqlCommand(Query, MySqlConn)
MySqlRea = MySqlCmd.ExecuteReader
MessageBox.Show("Chat has been cleaned.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
MySqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MySqlConn.Dispose()
End Try
End Sub
End Class
I hope you can fix it. I been trying to find the reason but no idea. Someone who can edit this please fix this bad code pasting. (website problem not my skill). If someone want to mark this as duplicate then please give me a fix using my code before you block this topic.

Inside the method loaddatabase_table add this line after the dataset fill
Private Sub loaddatabase_table()
Dim dbDataSet As New DataTable ' This is a misleading name.
....
MySqlDAd.Fill(dbDataSet)
' Add this line
dbDT = dbDataSet
End Sub
Now the global variable dbDT points to the effective table loaded from the db, so, when you build the DataView the column nusername is present in the source datatable
Note that the code in the delete button cannot work because you are missing the = between the nusername and the value to delete
Finally, while calling ExecuteReader works also for INSERT/UPDATE/DELETE query, the correct method to use is ExecuteNonQuery to avoid the unnecessary building of a DataReader when you have nothing to read from your command

you should check your connection string or put an error message like this:
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Related

Displaying a value obtained from a mysql query into a VB.net textbox

I am trying to display a value I obatain from a COUNT(*) query in a textbox in vb.net but instead of displaying the obtained value it displays the actual query.
Here is my code:
Imports MySql.Data.MySqlClient
Public Class Statistics
Dim conn As MySqlConnection
Dim command As MySqlCommand
Dim query As String
Dim dadapter As New MySqlDataAdapter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString =
"server=localhost;userid=root;database=librarydatabase"
Dim reader As MySqlDataReader
Try
conn.Open()
query = "SELECT COUNT(*) FROM login where users='" & Username.Text & "'"
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
dadapter.SelectCommand = command
If reader.HasRows Then
reader.Read()
takenout.Text = (query)
End If
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
Here is a screenshot of my program:
When trying to fix the code I change it to replace takenout.Text = (query) with takenout.Text = (reader.read()) and deleting the reader.read()) above and dadapter.SelectCommand = command
Here is the changed code:
Imports MySql.Data.MySqlClient
Public Class Statistics
Dim conn As MySqlConnection
Dim command As MySqlCommand
Dim query As String
Dim dadapter As New MySqlDataAdapter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString =
"server=localhost;userid=root;database=librarydatabase"
Dim reader As MySqlDataReader
Try
conn.Open()
query = "SELECT COUNT(*) FROM login where users='" & Username.Text & "'"
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
If reader.HasRows() Then
takenout.Text = (reader.Read())
End If
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
This code doesn't fix the problem but instead displays true in the textbox when the button is pressed no matter whats in the username textbox.
Here is an image of that:
Use command.ExecuteScalar() instead.
Using command.executescalar() fixes the issue.
Here is a copy of the fixed code if anyone else has a similar issue and wants to see a working example.
Public Class Statistics
Dim conn As MySqlConnection
Dim command As MySqlCommand
Dim query As String
Dim dadapter As New MySqlDataAdapter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString =
"server=localhost;userid=root;database=librarydatabase"
Dim numtakenout As String
Try
conn.Open()
query = "SELECT COUNT(*) FROM login where users='" & Username.Text & "'"
command = New MySqlCommand(query, conn)
numtakenout = Convert.ToString(command.ExecuteScalar())
takenout.Text = numtakenout
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub

check the manual that corresponds to your mariadb server version for the right syntax to use near "user" at line 1

I have a textbox, button, and datagridview in my form. When i click the button, system will grab a table depending on my textbox from the database and show on datagridview.
I getting this error when i click the button. Where am I wrong?
here is my dbconn
Module mod_dbconn
Public conn As MySqlConnection
Public Sub openDB()
Dim dbname As String = scr_sales.btn_dbswitch.Text
Dim server As String = "localhost"
Dim user As String = "root"
Dim password As String = ""
Try
conn = New MySqlConnection
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, user, password, dbname)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
This is my form
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim query As String = "SELECT * FROM '" + TextBox1.Text + "'"
Dim cmd As New MySqlCommand(query, conn)
Dim da As New MySqlDataAdapter(cmd)
Dim dt = New DataTable
Dim cb As MySqlCommandBuilder
cb = New MySqlCommandBuilder(da)
DataGridView1.Refresh()
Try
conn.Open()
da.Fill(dt)
Dim bsource As New BindingSource
bsource.DataSource = dt
Me.DataGridView1.DataSource = bsource
da.Update(dt)
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
You are trying to build a dynamic table select so for the table name you don't need the quotes around the tablename
"SELECT * FROM " + TextBox1.Text + " ;"

Choose from various dates using date time picker

This button filters the datagridview using datetimepicker.
What should I do if I want to filter it between dates?
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "Server = Localhost; database = venuesdb; user id = root; Password = "
Dim SQLDataAdapter As New MySqlDataAdapter
Dim DatabaseDatSet As New DataTable
Dim Bindsource As New BindingSource
Try
MySqlConn.Open()
Dim Query = "Select * From venuesdb.cost where EventDate = ('" & DateTimePicker1.Text & "')"
Command = New MySqlCommand(Query, MySqlConn)
SQLDataAdapter.SelectCommand = Command
SQLDataAdapter.Fill(DatabaseDatSet)
Bindsource.DataSource = DatabaseDatSet
DataGridView1.DataSource = Bindsource
SQLDataAdapter.Update(DatabaseDatSet)
MySqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
MySqlConn.Dispose()
End Sub

EvaluateException was unhandled by user code Cannot find column [year]

i was trying to filter datagridview with a use of a combobox that is populated by values from a database
i'm having below error
EvaluateException was unhandled by user code
Cannot find column [year].
here is my code
Imports MySql.Data.MySqlClient
Public Class ReportTeacher
Dim MySqlConnection As MySqlConnection
Dim dbDataSet As New DataTable
Private Sub ReportTeacher_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MySqlConnection = New MySqlConnection
MySqlConnection.ConnectionString = "server = localhost; port=3307; user id = root; password = 1234; database = mcs;"
Dim SDA As New MySqlDataAdapter
Dim bSource As New BindingSource
Try
MySqlConnection.Open()
Dim query As String
query = "SELECT DISTINCT year FROM mcs.year "
Dim da As New MySqlDataAdapter(query, MySqlConnection)
Dim ds As New DataSet
da.Fill(ds, "mcs.year")
With cmbxyear
.DataSource = ds.Tables("mcs.year")
.DisplayMember = "year"
.ValueMember = "year"
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MySqlConnection.Dispose()
End Try
Try
MySqlConnection.Open()
Dim query As String
query = "select * from mcs.faculties "
Dim Command As New MySqlCommand(query, MySqlConnection)
SDA.SelectCommand = Command
SDA.Fill(dbDataSet)
bSource.DataSource = dbDataSet
DataGridView1.DataSource = bSource
SDA.Update(dbDataSet)
MySqlConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MySqlConnection.Dispose()
End Try
End Sub
Private Sub cmbxyear_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbxyear.SelectedIndexChanged
Dim DV As New DataView(dbDataSet)
DV.RowFilter = String.Format(" year like '%{0}%' ", cmbxyear.SelectedItem)
DataGridView1.DataSource = DV
End Sub
can you help me???
it is just the position where you will call the values in mcs. year...it is after when you called the values of mcs.student and insert it into datagrid

Updating Multiple Selected rows in DGV in VB.NET

I have a DataBound DGV it has 3 Columns namely ID(not pk), Name, and Status. I have 2 Buttons Add and Post. Add, adds Data to the DGV and DataBase(MySql) but in Status, it adds "No". What I want to do is for example I have 3 Rows in my DGV and highlight those 3 and click Post, the value in Status will be changed to "Yes". Here is what I have done so far, but I'm having trouble with the syntax for the UPDATE QUERY. THIS CODE WORKS NOW
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the TestDataSet.testing table. You can move, or remove it, as needed.
Me.TestingTableAdapter.Fill(Me.TestDataSet.testing)
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using cmd
With cmd
MsgBox("Connection Established")
.Connection = _conn
.Parameters.Clear()
.CommandText = "Select Max(TransactionID) from testing"
_conn.Open()
Dim dr As MySqlDataReader
dr = cmd.ExecuteReader()
If dr.Read() Then
If IsDBNull(dr.Item(0)) Then
txtNumber.Text = "1"
Else
txtName.Text = dr(0).ToString() + 1
End If
End If
End With
End Using
End Using
FillGrid()
End Sub
Private Sub FillGrid()
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _comm As New MySqlCommand
With _comm
.CommandText = " SELECT `ID`, `TransactionID`, `Name`, `Posted` FROM `testing`"
.Connection = _conn
End With
Using _adapter As New MySqlDataAdapter(_comm)
Try
_conn.Open()
Dim _ds As New DataSet
_adapter.Fill(_ds)
GVTransaction.DataSource = _ds.Tables(0)
Catch ex As Exception
End Try
End Using
End Using
End Using
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Using conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using cmd
With cmd
MsgBox("Connection Established")
.Connection = conn
.Parameters.Clear()
.CommandText = "INSERT INTO testing(TransactionID, Name, Posted) VALUES (#ID, #iName, #iPosted)"
.Parameters.Add(New MySqlParameter("#ID", txtNumber.Text))
.Parameters.Add(New MySqlParameter("#iName", txtName.Text))
.Parameters.Add(New MySqlParameter("#iPosted", "No"))
End With
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString())
End Try
End Using
End Using
MsgBox("Data Added to the Database")
Me.TestingTableAdapter.Dispose()
Me.TestingTableAdapter.Fill(Me.TestDataSet.testing)
End Sub
Private Sub btnPost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPost.Click
Dim _ID As New List(Of String)
Dim _Name As New List(Of String)
For Each _x As DataGridViewRow In TestingDataGridView.SelectedRows
_ID.Add(_x.Cells("GVTransactionID").Value.ToString())
_Name.Add("'" & _x.Cells("GVName").Value.ToString() & "'")
Next
Dim inClause As String = String.Join(",", _ID.ToArray())
Dim inClause1 As String = String.Join(",", _Name.ToArray())
Dim _sqlUpdate As String = String.Format("UPDATE testing SET Posted = #Posted WHERE TransactionID IN ({0}) AND Name IN ({1})", inClause, inClause1)
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _commm As New MySqlCommand()
With _commm
.CommandText = _sqlUpdate
.Connection = _conn
.CommandType = CommandType.Text
.Parameters.AddWithValue("#Posted", "YES")
End With
Try
_conn.Open()
_commm.ExecuteNonQuery()
FillGrid()
Catch ex As MySqlException
MsgBox(ex.StackTrace.ToString)
End Try
End Using
End Using
End Sub
End Class
Any Helps or tips is greatly appreciated. Please and Thank you
Try this one,
Dim _ID As New List(Of String)
Dim _Name As New List(Of String)
For Each _x As DataGridViewRow In TestingDataGridView.SelectedRows
_ID.Add(_x.Cells("TransactionID").Value.ToString())
_Name.Add("'" & _x.Cells("Name").Value.ToString() & "'")
Next
Dim inClause As String = String.Join(",", _ID.ToArray())
Dim inClause1 As String = String.Join(",", _Name.ToArray())
Dim _sqlUpdate As String = String.Format("UPDATE testing SET Posted = #Posted WHERE TransactionID IN ({0}) AND Name IN ({1})", inClause, inClause1)
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _comm As New MySqlCommand()
With _comm
.CommandText = _sqlUpdate
.Connection = _conn
End With
Try
_conn.Open()
_comm.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString())
End Try
End Using
End Using
I believe you can simply use this
For Each Row As DataGridViewRow In TestingDataGridView.SelectedRows
.CommandText = "UPDATE testing SET Posted = #iChange WHERE ID = " & Row.Cells(0) & ", Name = " & Row.Cells(1)
.CommandType = CommandType.Text
.Parameters.AddWithValue("#iChange", "Yes")
End For
Hope this helps :)