Error: No value given for one or more required parameters. What does this mean? How do I fix? - vb.net

I'm creating a program in which users can see a table from an access database in a DataGridView.
However, when pressing "btnDisplay" the program crashes and highlights this line:
da.Fill(ds, "tblOrders")
The error reads: "No value given for one or more required parameters"
What does this mean and how do I fix it?
Here is the code:
Imports System.Data.OleDb
Public Class frmViewTables
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\SAC1 Database.mdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
Private Sub btnDisplayDataGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayDataGrid.Click
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("Select * from [tblOrders] where Username = #username", MyConn)
cm.Parameters.Add(New OleDbParameter("#username", OleDbType.VarChar, 255, frmLogin.SuccessfulLoginUsername))
cm.Parameters("#username").Value = frmLogin.SuccessfulLoginUsername
da.Fill(ds, "tblOrders")
Dim view As New DataView(tables(0))
source1.DataSource = view
dgvDynamic.DataSource = view
End Sub

Things seem a little messed up. I am not sure about the rest of it but this should solve your parameter problem.
Dim source1 As New BindingSource
Dim ds = New DataSet
Dim tables = ds.Tables
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\SAC1 Database.mdb")
Dim da As OleDbDataAdapter = New OleDbDataAdapter()
Dim cmd As New OleDbCommand("Select * from [tblOrders] where Username = #username", cn)
cmd.Parameters.Add("#username", OleDbType.VarChar, 255).Value = frmLogin.SuccessfulLoginUsername
da.SelectCommand = cmd
da.Fill(ds, "tblOrders")
Dim view As New DataView(tables(0))
source1.DataSource = view
dgvDynamic.DataSource = view

Related

Retrieving data between two dates from Access database using VB.NET

I want to display data from a database in a datagridview. I am getting error on da.Fill(ds, "SAMPLE") with "data type mismatch error". Please see screenshot.
My date formats are 'short date' both datetimepicker and database values.
Imports System.Data.OleDb
Public Class Form1
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Recto D Sanchez Jr\Documents\sample.accdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("Select * from SAMPLE where [LOGDATE] between '" & DateTimePicker1.Text & "' And '" & DateTimePicker2.Text & "'", MyConn)
da.Fill(ds, "SAMPLE")
Dim view As New DataView(tables(0))
source1.DataSource = view
DataGridView1.DataSource = view
End Sub
End Class
error screenshot
Since your DateTimePicker controls already deal with proper DateTime values you might have better luck using a parameterized query like this:
MyConn = New OleDbConnection(connString)
Dim cmd As New OleDbCommand("SELECT * FROM [SAMPLE] WHERE [LOGDATE] Between ? And ?", MyConn)
cmd.Parameters.AddWithValue("?", DateTimePicker1.Value.Date)
cmd.Parameters.AddWithValue("?", DateTimePicker2.Value.Date)
da = New OleDbDataAdapter(cmd)
ds = New DataSet
da.Fill(ds, "SAMPLE")
It would save you the trouble of dealing with different date formats, delimiting date literals, and other potential pitfalls.
Try
da = New OleDbDataAdapter("Select * from SAMPLE where [LOGDATE] between #" & DateTimePicker1.Value.ToString("MM/dd/yyyy HH:mm:ss") & "# And #" & DateTimePicker2.Value.ToString("MM/dd/yyyy HH:mm:ss") & "#", MyConn)

Filling Datagrid from separate form without removing data

I select a value in the combo-box in the form Addsubject().
This takes all the values from my database with the same value and adds them to the DGV.
Only issue is when i try to add a second value from the combo-box it replaces the first.
Form addsubject() has a button that when clicked takes the data and places it in the form1 dgv.
Private Sub btnAddSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSub.Click
Dim comboboxvalue As String
comboboxvalue = "'" & CBClass.SelectedItem & "'"
Dim sql As String
sql = "Select * From class Where ClassCode=" & comboboxvalue
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\ProjectDatabase.mdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter(sql, MyConn)
da.Fill(ds, "Class")
Dim view As New DataView(tables(0))
source1.DataSource = view
Form1.dgv.DataSource = view
End Sub
Images of the issue
Pre-button click
Post-Click
New value and button click

Adding more rows to a datagridview in vb.net

Private Sub btnAddSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSub.Click
Dim comboboxvalue As String
comboboxvalue = "'" & CBClass.SelectedItem & "'"
Dim sql As String
sql = "Select * From class Where ClassCode=" & comboboxvalue
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=H:\ProjectDatabase.mdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter(sql, MyConn)
da.Fill(ds, "Class")
Dim view As New DataView(tables(0))
source1.DataSource = view
Form1.dgv.DataSource = view
End Sub
I can currently add one piece of data to the datagrid. When I try to add a second piece of data it replaces the current data stored.
How do I make it so that when I add more data it goes to a new line.
The source code you provided looks a lot like you are just refreshing the data source for the DGV on every btnAddSub click, by assigning to form1.dgv.datasource each time. If you want to simply append new rows to the DGV you will need to manipulate the DGV.Rows collection by adding DataGridViewRow objects to it.

check values of column in sql

I am trying to collect values from a database, and if they are X value sett he background colour to green.
Basically, I have a Rota system, and if the user is working, then change the background colour. The select * will only bring back 1 row ever.
Imports System.Data.SqlClient
Imports System.Data.OleDb
Public Class Form4
Dim Con As SqlConnection
Dim cmd As New OleDbCommand
Dim sqlstring As String
Dim connstring As String
Dim ds As DataSet
Dim da As SqlDataAdapter
Private Sub Form4_Load(sender As Object, e As EventArgs)
connstring = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Assignment.mdf;Integrated Security=True;Connect Timeout=30"
Con = New SqlConnection(connstring)
Con.Open()
Dim strSQL As String = "SELECT * from Users"
Dim da As New SqlDataAdapter(strSQL, Con)
Dim ds As New DataSet
da.Fill(ds, "Users")
With cboname
.DataSource = ds.Tables("Users")
.DisplayMember = "Name"
.ValueMember = "Id"
.SelectedIndex = 0
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Con As SqlConnection
Dim cmd As New OleDbCommand
Dim sqlstring As String
Dim connstring As String
Dim ds As DataSet
Dim da As SqlDataAdapter
connstring = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Assignment.mdf;Integrated Security=True;Connect Timeout=30"
Con = New SqlConnection(connstring)
Con.Open()
sqlstring = ("SELECT * FROM Rota WHERE UserId ='" & cboname.SelectedIndex & "' and ID ='" & dtp1.Value & "'")
da = New SqlDataAdapter(sqlstring, Con)
ds = New DataSet
da.Fill(ds, "Rota")
End Sub
End Class
After this, I understand I need to get a few IF statements, but I am unsure on how to construct them.
To check a value in a DataSet use first get a DataTable inside of it, then a DataRow, then check one of the field values:
ds.Tables(0).Rows(0)("{field name}");
So to change the color based on some value:
If ds.Tables(0).Rows(0)("{field name}") = "Red" Then
textbox1.BackColor = Color.Red
End If
Some other comments:
A DataSet may be a bit heavy for getting one value (unless you're binding to a control). You can just use ADO.NET objects and use ExecuteScalar)
It's safer to use parameters instead of concatenating SQL statements (prevents SQL Injection and errors from special characters)
You can refactor your code to put the connection string in a single location rather than duplicating it across methods.

Not able to bind gridview in vb.net in desktop application

I have a gridview in which I am assigning the dataset as datasource in my program.
My Form_Load() event is:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cn As New SqlConnection("Data Source=HCL-43AF369E5A0;Initial Catalog=Exam;Integrated Security=True")
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet
cn.Open()
cmd = New SqlCommand("Select * from Contact", cn)
da = New SqlDataAdapter(cmd)
ds = New DataSet
da.Fill(ds)
cn.Close()
DataGridView1.DataSource = ds
End Sub
What is the error in my code? I have debugged the code and found that the dataset fetches the data but not able to bind it to gridview.
Please help.
You have to assign a Datatable as DataGridView1.DataSource. Not the DataSet. Use ds.Tables() property.
formal ways is
Dim cn As New SqlConnection("Data Source=HCL-43AF369E5A0;Initial Catalog=Exam;Integrated Security=True")
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet
cn.Open()
cmd = New SqlCommand("Select * from Contact", cn)
da = New SqlDataAdapter(cmd)
ds = New DataSet
da.Fill(ds) // edit here like da.fill(ds, "table name")
cn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "table name"
// table name will be any not the same as in your database
Your dataset is actually a data which you get by executing your query like "select * from tablename"
even you can have more than one table in a dataset so its formal method to give datamember name.