dynamic sql generation error occured when trying to update database - sql

I followed your code and I have put it a msgbox to confirm and yes its returns the right row number. However an error occurred here's my code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cb As New OleDb.OleDbCommandBuilder(da) 'command builder to update the database
ds.Tables("inventory_table").Rows(marker).Item(0) = TextBox1.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(1) = TextBox2.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(2) = TextBox3.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(3) = TextBox4.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(4) = TextBox5.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(5) = TextBox6.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(6) = TextBox7.Text 'update dataset
da.Update(ds, "inventory_table") 'this is where the updating of database takes place
MsgBox("Data updated")
End Sub
Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
TextBox1.Text = DataGridView1.SelectedRows(0).Cells(0).Value
TextBox2.Text = DataGridView1.SelectedRows(0).Cells(1).Value
TextBox3.Text = DataGridView1.SelectedRows(0).Cells(2).Value
TextBox4.Text = DataGridView1.SelectedRows(0).Cells(3).Value
TextBox5.Text = DataGridView1.SelectedRows(0).Cells(4).Value
TextBox6.Text = DataGridView1.SelectedRows(0).Cells(5).Value
TextBox7.Text = DataGridView1.SelectedRows(0).Cells(6).Value
marker = Me.DataGridView1.SelectedRows(0).Index
MsgBox(marker)
End Sub
The error says:
dynamic sql generation for the updatecommand is not supporyed against
a selectcommand that does not return any key column information
and the line da.update(ds, "inventory_table") is highlighted.

In the double click event set a global variable:
_rowIndex = DataGridView1.SelectedRows(0).Index
Then
ds.Tables("inventory_table").Rows(_rowIndex).Item(1) = TextBox1.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(2) = TextBox2.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(3) = TextBox3.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(4) = TextBox4.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(5) = TextBox5.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(6) = TextBox6.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(7) = TextBox7.Text

Related

Vb + Custom Url with parameters

How i do to custom a url with parameters ?
In my example i have 3 parameters
Textbox1.Text = "Ikea"
Textbox2.Text = "Table"
Textbox3.Text = "Black"
The final url is need to be like:
https://localhost/objects?color=Black&manufacturer=Ikea&type=Table
in Textbox4.Text.
So how i do to add the previous values of the previous Textbox ?
Currently i have that :
Private Sub FinalScrapper_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "Ikea"
TextBox2.Text = "Table"
TextBox3.Text = "Black"
TextBox4.Text = ("https://localhost/objects?color=Black&manufacturer=Ikea&type=Table")
'The correct parameters are something like that ?
'TextBox4.Text = ("https://localhost/objects?color=TextBox3.Text&manufacturer=TextBox1.Text&type=TextBox2.Text")
End Sub
Thanks you.
I used Interpolated Strings as suggested in the comments and the following worked for me:
Private Sub FinalScrapper_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "Ikea"
TextBox2.Text = "Table"
TextBox3.Text = "Black"
Dim manufacturer = TextBox1.Text
Dim type = TextBox2.Text
Dim color = TextBox3.Text
Dim s1 = $"https://localhost/objects?color={color}&manufacturer={manufacturer}&type={type}"
TextBox4.Text = s1
End Sub

Add rows in datagridview in vb.net from SQL Server

I am facing a problem here, the below code have no error, but I would like to fetch data from SQL Server and add rows in datagridview rather than show in the textbox and then add to datagridview.
Private Sub TextBox2_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
'searchcproduct
con = connection()
con.Open()
cmd.Connection = con
cmd.CommandText = " select * from product where plucode='" & TextBox2.Text & "' or barcode='" & TextBox2.Text & "'"
Dim dbreader As SqlDataReader = cmd.ExecuteReader
If dbreader.HasRows = False Then
TextBox2.Text = ""
TextBox2.Focus()
itemnotfound.Show()
PlayBackgroundSoundFile1()
ElseIf dbreader.HasRows = True Then
While dbreader.Read()
TextBox4.Text = dbreader.Item("plu_description")
TextBox5.Text = dbreader.Item("unitprice")
TextBox7.Text = dbreader.Item("plu_qty")
TextBox8.Focus()
End While
End If
End If
'TextBox2.Text = ""
End Sub
Private Sub Button20_Click(sender As System.Object, e As System.EventArgs) Handles Button20.Click
Dim rowNumber As Integer = DataGridView1.Rows.Add()
Try
DataGridView1.Rows.Item(rowNumber).Cells(0).Value = Me.TextBox2.Text ' i want to display sql rather than textbox
DataGridView1.Rows.Item(rowNumber).Cells(1).Value = Me.TextBox4.Text
DataGridView1.Rows.Item(rowNumber).Cells(2).Value = Me.TextBox5.Text
DataGridView1.Rows.Item(rowNumber).Cells(3).Value = Me.TextBox8.Text
DataGridView1.Rows.Item(rowNumber).Cells(4).Value = Val(Me.TextBox5.Text) * Val(Me.TextBox8.Text)
If Me.DataGridView1.Rows.Count > 0 Then
Me.Label2.Text = FormatNumber(totalsales(), 2).ToString()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "check")
End Try
TextBox2.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox8.Text = ""
TextBox7.Text = ""
End Sub
After calling ExecuteReader, create a DataTable and call its Load method, passing your data reader. You can then assign that DataTable to the DataSource property of grid to display all the data, e.g.
Dim myDataReader = myCommand.ExecuteReader()
Dim myDataTable As New DataTable
myDataTable.Load(myDataReader)
myDataGridView.DataSource = myDataTable

Error on data mismatch doesn't stop

I can keep getting 'data type mismatch on citerio expression'
Because TimeOfOrder is a data/time in the access, i tried converting it to a string. It didnt work as well.
So then i tried removing that field to find the source of the problem the same error still appears.
This is my code so far:
Private Sub Lists_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lists.SelectedIndexChanged
Dim Sql As String
Dim data As dbase.OleDbDataReader
Dim cmd As dbase.OleDbCommand
conn.Open() ' start the connection
Sql = "select * from corder where OrderID = """ & Lists.Text & """"
cmd = New dbase.OleDbCommand(Sql, conn) ' how the commad is run on this connection
data = cmd.ExecuteReader
data.Read()
TextBox1.Text = data("TableNumber")
TextBox2.Text = data("Status")
TextBox3.Text = data("TimeOfOrder".ToString("f"))
textbox4.text = data("Orders")
conn.Close()
End Sub
You can modify like this. OrderID is read as text from Lists.Text so it is "ready" to be concatenated with the SQL string as is:
Private Sub Lists_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lists.SelectedIndexChanged
Dim Sql As String
Dim data As dbase.OleDbDataReader
Dim cmd As dbase.OleDbCommand
conn.Open() ' start the connection
Sql = "select * from corder where OrderID = " & Lists.Text & ""
cmd = New dbase.OleDbCommand(Sql, conn) ' how the commad is run on this connection
data = cmd.ExecuteReader
data.Read()
TextBox1.Text = data("TableNumber")
TextBox2.Text = data("Status")
' Cast/format the field value, not the field name.
TextBox3.Text = data("TimeOfOrder").ToString("f")
textbox4.text = data("Orders")
conn.Close()
End Sub

Identifier expected? visual basic

im almost done with this and then that... hopefully someone will be able to help me (Really hope so atleast, cus i need to be done with this:P) (need more text cus there is to much code):
|error is there|>
Private Sub FlatButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlatButton1_Click. <|error is there|
Dim Conn As New MySqlConnection("Not going to publish this")
If FlatTextBox1.Text = "" Then
MsgBox("No username specified")
FlatTextBox2.Text = ""
Else
If FlatTextBox2.Text = "" Then
MsgBox("No password specified")
FlatTextBox1.Text = ""
Else
Try
Me.Text = "Logging in..."
Conn.Open()
Dim sqlquery As String = "SELECT * FROM Testing WHERE Username = '" & FlatTextBox1.Text & "';"
Dim data As MySqlDataReader
Dim adapter As New MySqlDataAdapter
Dim command As New MySqlCommand
command.CommandText = sqlquery
command.Connection = Conn
adapter.SelectCommand = command
data = command.ExecuteReader
While data.Read()
If data.HasRows() = True Then
If data(2).ToString = FlatTextBox2.Text Then
Me.Text = "Logged in!"
My.Settings.Username = FlatTextBox1.Text
MsgBox("Welcome " + data(1).ToString)
Home.Show()
Me.Close()
If data(3).ToString = "1" Then
My.Settings.Admin = "Yes"
Else
My.Settings.Admin = "No"
End If
End If
Else
MsgBox("Failed Login")
End If
End While
Catch ex As Exception
End Try
End If
End If
End Sub
End Class
Replace these Lines
Private Sub FlatButton1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles FlatButton1_Click
With
Private Sub FlatButton1_Click(sender As Object, e As EventArgs) Handles FlatButton1_Click

How to use System.IDisposable.Dispose?

Im getting a warning in my object which transfers value from one form to another
The warning said Button1 must call System.IDisposable.Dispose on Object Form1 before references to it are of scope
This is my code on my button1:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Form1 As New Form1
Form1.a1 = TextBox1.Text
Form1.a2 = TextBox2.Text
Form1.a3 = TextBox3.Text
Form1.a4 = TextBox4.Text
Form1.a5 = TextBox5.Text
Form1.a6 = TextBox6.Text
Form1.a7 = TextBox7.Text
Form1.a8 = TextBox8.Text
Form1.a9 = TextBox9.Text
Form1.a10 = TextBox10.Text
Form1.Show()
Form1.SetPrice()
Me.Close()
End Sub
How to use System.IDisposable.Dispose?
It sounds like the listed code is not in Form1. Whatever form it's in is instantiating a Form1, but not disposing it. Just wrap Form1 in a Using statement:
Using Form1 As New Form1
Form1.a1 = TextBox1.Text
Form1.a2 = TextBox2.Text
Form1.a3 = TextBox3.Text
Form1.a4 = TextBox4.Text
Form1.a5 = TextBox5.Text
Form1.a6 = TextBox6.Text
Form1.a7 = TextBox7.Text
Form1.a8 = TextBox8.Text
Form1.a9 = TextBox9.Text
Form1.a10 = TextBox10.Text
Form1.Show()
Form1.SetPrice()
End Using
Me.Close()