Visual Basic - No value given for one or more required parameters - vb.net

I have a DataGridView which displays the contents of a table (with sql query). But whenever I run the code, it displays this error:
No value given for one or more required parameters.
Where did it went wrong? The program ran without displaying any errors.
Here's the code for displaying in the DataGridView:
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BSPDatabase.accdb"
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("Select * from [Transactions] where transMonth=" & monthCombobox.Text & "", MyConn)
da.Fill(ds, "BSPDatabase") 'Change items to your database name
Dim view As New DataView(tables(0))
source1.DataSource = view
TransactionsDataGridView.DataSource = view

I think you are forming bad SQL. The value needs to be in quotes.
da = New OleDbDataAdapter(String.Format("Select * from [Transactions] where transMonth ='{0}'", monthCombobox.text), MyConn)

Related

invalid column name RDLC Report Viewer says while populating data to data source using multiple data sources

I am trying to print an RDLC report that gets data from multiple data sources. The first data source is filled with data while the other data source gives the following error
"Invalid Column Name 'INV123'"
The code for filling the data sources is as follows:
frmInvoiceReport.DataTable_InvoiceTableAdapter.Fill(frmInvoiceReport.SIS_DBDataSet.DataTable_Invoice)
frmInvoiceReport.DataTable_InvoiceInfoTableAdapter1.Fill(frmInvoiceReport.SIS_DBDataSet.DataTable_InvoiceInfo)
'The DataSet created.
Dim myConnection As SqlConnection = New SqlConnection(cs)
Dim myDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM [View_Invoice_Details] Where InvID = " & txtID.Text, myConnection)
Dim myDS As New DataSet
myDA.Fill(myDS, "DataTable_Invoice")
Dim myDa2 As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM [View_InvoiceInfo] Where InvoiceNo = " & txtInvoiceNo.Text, myConnection)
Dim myDs2 As New DataSet
myDa2.Fill(myDs2, "DataTable_InvoiceInfo")
frmInvoiceReport.ReportViewer1.LocalReport.DataSources.Clear()
Dim DtSrc As ReportDataSource = New ReportDataSource("SIS_DBDataSet", myDS.Tables("DataTable_Invoice"))
Dim DtSrc2 As ReportDataSource = New ReportDataSource("SIS_DBDataSet", myDs2.Tables("DataTable_InvoiceInfo"))
frmInvoiceReport.ReportViewer1.LocalReport.DataSources.Add(DtSrc)
frmInvoiceReport.ReportViewer1.LocalReport.DataSources.Add(DtSrc2)
frmInvoiceReport.ReportViewer1.RefreshReport()
frmInvoiceReport.ShowDialog()
I am not sure where the error arises. I have debugged the code and it catches an exception saying that the column name is invalid. I need a quick fix for that.

How to get DevExpress GridControl to show captions instead of field names?

I have a DevExpress GridControl with a master and and detail. On the detail, I want to show one column. The dataset I'm using has two columns, the one I want to show and the foreign key back to the master. In the designer, I've included only the column I want and in design mode it show one column. However, when I run the application I see both columns with the names from the data set. How do I get the detail grid to just show the one column I want?
Here's an example of my code. I've tried it with and without the level tree add and tried different names for the relation. Nothing I've tried uses the formatting I set in the designer for the detail view.
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim da2 As SqlDataAdapter
cmd = MSSQL_CONN.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from vw_backlog with (nolock) where order_id = " & m_order.id
da = New SqlDataAdapter(cmd)
cmd = MSSQL_CONN.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT order_id, backlog_id, special_handling_id, comment_id, message FROM vw_backlog_special_handling_comments with (nolock) WHERE order_id = " & m_order.id
da2 = New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "Backlogs")
da2.Fill(ds, "Messages")
Dim keyColumn As DataColumn = ds.Tables("Backlogs").Columns("backlog_id")
Dim foreignKeyColumn As DataColumn = ds.Tables("Messages").Columns("backlog_id")
ds.Relations.Add("gvSpecialHandlingComments", keyColumn, foreignKeyColumn)
gcBacklog.DataSource = ds.Tables("Backlogs")
gcBacklog.ForceInitialize()
gcBacklog.LevelTree.Nodes.Add("SpecialHandlingComments", gvSpecialHandlingComments)
gvSpecialHandlingComments.PopulateColumns(ds.Tables("Messages"))

Data type mismatch in criteria expression vb.net

I am using VB.Net connecting to MS Access database. I think SQL syntax is wrong because before i add WHERE clause, it works. When i add WHERE clause, i got error ' data type mismatch in criteria expression'. Any help would be appreciated, thank you.
Here is my code
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select Sum([Shark Individual Weight]) From FishCaught Where [OperationID] ='" & TextBoxOpID4.Text & "'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView2.DataSource = ds
Additional info: textboxOpID4 is enabled false
make something like this
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select Sum([Shark Individual Weight]) From FishCaught Where [OperationID] =" & TextBoxOpID4.Text , myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView2.DataSource = ds
I quite use this kind of query when the where clause is a number because if you do put ' in a number column the output would be string which produces the error for example the ColumnID = 5 your code is like this ColumnID = '5' which is wrong especially if it is an AutoNumber field so do not insert a datatype number inside ' so it will not become a string

Populating and editing datagrid from a SQL Server table

I need to populate my datagrid from my a table in SQL Server called Pastel_Companies and then if any changes are made to the datagrid it has to update it to the database.
I am using a default view to populate my datagrid.
Is there another way where I link each column separately so I can resize my columns as they are fixed to what SQL has?
Here is my code:
Dim cn As New SqlClient.SqlConnection(SQL_Loader("", My.Settings.SQL_Win_Auth, My.Settings.SQL_Username, My.Settings.SQL_Password, My.Settings.SQL_Server_Name, My.Settings.SQL_DB_Name))
Dim Cmd As New SqlClient.SqlCommand
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New SqlClient.SqlDataAdapter
Cmd.Connection = cn
cn.Open()
da = New SqlClient.SqlDataAdapter("Select Company_ID, Prefix, DSN, File_Path From Pastel_Companies", cn)
da.Fill(dt)
'DataGridView1.Columns.Add("Company_ID", Prefix.ToString)
DataGridView1.DataSource = dt.DefaultView
cn.Close()
From: http://social.msdn.microsoft.com/Forums/windows/en-US/e444ca84-3319-4dfa-aa31-46f310dd0c13/datagridview-autosize-rowcolumn
'for the rows
DataGridView1.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader)
'and for columns
DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
or you can use for each loop
DataGridView1.Columns(i).Width = x

How to determine the id of a data in vb.net?

I'm currently working on an ms access database in vb.net.
And I need a code that can determine which data I am updating in the database. Because when I try to update the data, the previous data is being cloned and it will generate two data(the updated and the previous data), and the program will also generate a random id number for the updated data, which is not good.
Here is my code for the update button:
'update
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("GH").Rows(INC).Item(1) = TextBox13.Text
ds.Tables("GH").Rows(INC).Item(2) = TextBox14.Text
ds.Tables("GH").Rows(INC).Item(3) = TextBox15.Text
da.Update(ds, "GH")
MsgBox("Data updated")
My code for form load:
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\ACCESS DATABASE\search.mdb"
con.Open()
sql = "SELECT * FROM GH"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "GH")
con.Close()
I'm using system.data.oledb namespace
Here are my declarations:
Dim cmd As OleDbCommand
Dim cn As OleDbConnection
Dim dr As OleDbDataReader
Dim ds As New DataSet
Dim con As New OleDb.OleDbConnection
Dim da As OleDb.OleDbDataAdapter
Your question is not clear to me at all, but if you want to find the last ID of an insert, you would usually check the results of "SELECT ##IDENTITY" immediately after the record is inserted.