I've got a problem finding the data for one particular table. I've output the SQL statement and tested it in a query in Access and that query finds the relevant data, but in the code the dataset returns zero rows.
Private m_comm As OleDbCommand
Private m_SQL As String
Private m_ds As New DataSet
Private m_dt As New DataTable
Private m_da As OleDbDataAdapter
Private m_bd As OleDbCommandBuilder
' bit of code to connect to database
m_conn = ...
m_comm = New OleDbCommand
m_comm.Connection = m_conn
m_SQL = "SELECT * FROM [TableName];"
m_ds = New DataSet
m_dt = New DataTable
m_da = New OleDbDataAdapter(m_SQL, m_conn)
Dim n As Integer = m_da.Fill(m_ds)
At this point n = 0
This is the only table this happens with. The Table contains 1 row with two fields.
I've been trying to figure out what might be happening, but I need another set of eyes in case I'm missing the obvious. I have other tables with the same configuration and the SQL for them work so it's not the 1 row that is the problem....
Related
I am currently trying to code a database project for a musicians record log. I have been struggling to join 3 tables together in a single search of the database and I am getting this error:
No value given for one or more required parameters.
I have been searching online for why this is but cannot find anything which quite fixes my code. I have tried copying almost word for word other syntax but still get this error. Any help would be much appreciated, my database is as follows:
Program(ProgramID(PK), LengthOfProgram, UserName, NameOfProgram)
ProgramList(PieceListID(PK), PieceID(FK), ProgramID(FK))
Repertoire(PieceID(PK), NameOfPiece, Composer)
Those are the fields needed from repertoire.
Here is my code for when the the program is selected from the list box.
Public dtbConnecter As New OleDbConnection
Dim fileLocator As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source = " + Application.StartupPath + "\Musician Record Log.accdb"
Private Sub lstPro_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstPro.SelectedIndexChanged
Dim programID As String
Dim programIDLength As Integer
Dim selectedString As String = lstPro.GetItemText(lstPro.SelectedItem)
programIDLength = selectedString.IndexOf(":")
programID = selectedString.Substring(0, programIDLength)
MsgBox(programID)
Dim SQL As String = "Select Program.ProgramName, Program.ProgramLength, Repertoire.NameOfPiece, Repertoire.Composer From ((ProgramList INNER JOIN Repertoire ON ProgramList.PieceID = Repertoire.PieceID) INNER JOIN Program ON ProgramList.ProgramID = Program.ProgramID) WHERE (Program.ProgramID = #Param1)"
Dim da As New OleDbDataAdapter
Dim dtbTable As New DataTable
Dim ds As New DataSet
Dim dtbCommand As New OleDbCommand
dtbCommand.Parameters.Add("#Param1", OleDbType.VarChar).Value = programID
dtbCommand.Connection = dtbConnecter
dtbCommand.CommandType = CommandType.Text
dtbCommand.CommandText = SQL
da.SelectCommand = dtbCommand
da.Fill(dtbTable)
'Here i would then use the table's information to update my form, however first i'm trying to get this to work.
da.Dispose()
da = Nothing
End Sub
I also import System.Data, System.Data.OleDb and System.Drawing.Imaging at the top.
It was just a stupid mistake, the names of the fields in the database are NameOfProgram and LengthOfProgram but in the SQL query I used ProgramName and ProgramLength, thanks for all the help in ensuring the SQL query was correct and I did need to correct the data type but the main problem was the wrong identifier.
Here is some simple sample code that demonstrates how I am getting results back from SQL Server:
Dim dtbTable As System.Data.DataTable
Dim sdaDataAdapter As New System.Data.SqlClient.SqlDataAdapter()
sdaDataAdapter = New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM A; SELECT * FROM B", Connection)
dtbTable = New System.Data.DataTable()
dtbTable.Locale = System.Globalization.CultureInfo.InvariantCulture
sdaDataAdapter.Fill(dtbTable)
sdaDataAdapter = Nothing
objCommandBuilder = Nothing
In .Net, table A will come out TABLE and Table B will come out TABLE1... I want them to hold their real table names.
How do I get the real table names of the tables that come in from the SQL Query ? All the column names are correct, but the tables come out TABLE, TABLE1, TABLE2... I obviously know how to set them in my code or parse the table names and set them in code... I am wondering why .Net looses the table names in the first place and if there is a way for them to not be turned into ambiguous names.
Why would the writers of the SQLClient drivers not think we care about table names?
Any ideas from anyone on a way to get around this without having to set Table Name manually?
Here are two approaches - map the tables to DataTables and load the DataSet one table at a time w/desired name.
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
Dim Connection As New SqlConnection(OISConnectString)
Connection.Open()
Dim da As New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM FIS..FIS_Log; SELECT * FROM FIS..FIS_Log_CA", Connection)
Dim ds As New DataSet
da.Fill(ds)
Debug.Print(ds.Tables(0).TableName) ' Table
Debug.Print(ds.Tables(1).TableName) ' Table1
Dim dtLog As DataTable = ds.Tables(0)
Dim dtLog_CA As DataTable = ds.Tables(1)
' work with DataTables by name
Stop
'reset
ds = Nothing
da = Nothing
ds = New DataSet()
da = New SqlDataAdapter("SELECT * FROM FIS..FIS_Log", Connection)
da.Fill(ds, "FIS_Log")
da = New SqlDataAdapter("SELECT * FROM FIS..FIS_Log_CA", Connection)
da.Fill(ds, "FIS_Log_CA")
Debug.Print(ds.Tables(0).TableName) ' Table
Debug.Print(ds.Tables(1).TableName) ' Table1
dtLog = ds.Tables("FIS_Log")
dtLog_CA = ds.Tables("FIS_Log")
Connection.Close()
End Sub
You might find TableAdapterManager useful depending on your platforms
I have two linked tables like this
Here myDataset.cat_table is already filled at form load,used for other purpose, and me getting data from myDataset.prod_table bydataview by the below script.
Dim myDataView As New DataView
myDataView.Table = Me.myDataSet.prod_table
For Each myRow As DataRow In myDataView.ToTable.Rows
Dim myId as integer
Dim myCatid as integer
Dim myCatName as object
Dim myprodDetails as object
myId = myRow("id")
myCatid = myRow("catid")
myCatName = ?
myprodDetails = myRow("details")
Next
Here in Dataview method, is it possible to get "myCatName" from the linked table? Me not looking for a separate query (by left join), because again i have to load query. And "Merge table" works for identical tables only i guess, So searching for other possible logics
Yours faithfully
Murulimadhav
Hopefully, these two tables are not just data sets but if they can possibly be real database tables from sql, my suggestion is that you can easily manipulate the data of linked tables by SQL query. P.S - Import the following references.
Imports System.Data.SqlClient
Private strConn As String
Private sqlConn As New SqlConnection
Private sqlcmd As New SqlCommand
Private Sub getdatatable()
strConn = "Data Source= Server_Name;Initial Catalog= DB_Name;User ID= User_name;Password= Password"
sqlConn = New SqlConnection(strConn)
sqlConn.Open()
Dim sqlcmd As New SqlCommand("SELECT p.id, c.catname, p.products, p.details FROM cat_table c, prod_table p WHERE c.catid = p.catid", sqlConn)
Dim myreader As SqlDataReader
myreader = sqlcmd.ExecuteReader
myreader.Read()
Do While myreader.HasRows
'datagridview.Rows.Add(myreader.Item("id").ToString, myreader.Item("catname").ToString, . . . and so on as your desire table columns that you want to show on datagridview)
Do While myreader.Read
'datagridview.Rows.Add(myreader.Item("id").ToString, myreader.Item("catname").ToString, . . . same coding as above Do while looping)
Loop
myreader.NextResult()
Loop
End Sub
Trigger this method on Form_load or button click or whatever as you like. Before you do this type of showing in datagridview, you must pre-define the respective columns first in datagridview. I hope I will help you a little bit.
Dim myDataView As New DataView
myDataView.Table = Me.myDataSet.prod_table
For Each myRow As DataRow In myDataView.ToTable.Rows
Dim myId As Integer
Dim myCatid As Integer
Dim myCatName As Object
Dim myprodDetails As Object
myId = myRow("id")
myCatid = myRow("catid")
Dim myCatPosi As Integer = cat_tableBindingSource.Find("id", myCatid)
If myCatPosi >= 0 Then
cat_tableBindingSourceBindingSource.Position = myCatPosi
myCatName = cat_tableBindingSourceBindingSource.Current("title").ToString
End If
myprodDetails = myRow("details")
Next
I have the following code to alter the data, now I actually want to write said data back to the TESTS_TMP table in the Access database, how do I do that? I thought the adapter update did that but it doesn't?
Dim dataSet As DataSet = New DataSet
Using connection As New OleDbConnection(FileLocations.connectionStringNewDb)
connection.Open()
Dim adapter As New OleDbDataAdapter()
adapter.SelectCommand = New OleDbCommand("SELECT * FROM TESTS_TMP;", connection)
Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(adapter)
adapter.Fill(dataSet)
'MsgBox(dataSet.Tables(0).Rows.Count)
'Code to modify the data in the DataSet here.
Dim id As Integer = 300
For i As Integer = 0 To dataSet.Tables(0).Rows.Count - 1
dataSet.Tables(0).Rows(i).Item(0) = id
id = id + 1
Next
' Without the OleDbCommandBuilder this line would fail.
'builder.GetUpdateCommand()
'adapter.Update(dataSet)
Try
adapter.Update(dataSet.Tables(0))
dataSet.AcceptChanges()
Catch x As Exception
' Error during Update, add code to locate error, reconcile
' and try to update again.
End Try
End Using
MsgBox(dataSet.Tables(0).Rows(1).Item(0))
You're calling AcceptChanges. and it is marking all of the rows as being unmodified, so they are never updated in the database. Remove this call.
I am working in VB.NET and I have a simple requirement:
I have added a MDB file to a DataSet and it contains 21 tables.
I have a DataGridView and a ComboBox on my form.
I was able to get the ComboBox populated with the table names available in the DataSet by iterating through dataset.Tables.
Now I want to the user to be able to select the table name from the ComboBox and then populate the contents of that table.
I tried the following code:
Datagridview1.DataSource = dataset1
Datagridview1.DataMember = dataset1.tables(combobox1.selecteditem)
Datagridview1.Refresh()
But I only got the column headers. Then I read that I need a TableAdapter to populate the DataSet with that table. But if I use the TableAdapter then I won't be able to populate the table in a generic way.
Currently if I have to populate TableA then I will have to create an instance of Dataset1TableAdapters.TableA and then use it's .Fill property to populate the table. I will also have to use "Dataset1TableAdapters.TableB`. Is there a generic method to populate any table in the DataSet?
The following worked for me:
Datagridview1.DataSource = dataset1.tables(combobox1.selecteditem)
C# but easily convertible to VB.Net (I suppose):
String connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\test.mdb""";
String tableName = combobox1.SelectedItem.ToString();
String sqlSELECT = String.Format("select * from [{0}]", tableName);
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter();
da.SelectCommand = new System.Data.OleDb.OleDbCommand(sqlSELECT, new System.Data.OleDb.OleDbConnection(connectionString));
DataGridView1.DataSource = dataSet1;
DataGridView1.DataMember = dataSet1.Tables(tableName);
da.Fill(dataSet1, tableName);
I was just sketching a proof that "it can be done in a generic way" to your problem, you should elaborate and maybe come with something more elegant (and safe).
Private Sub BindData()
With customersDataGridView
.AutoGenerateColumns = True
.DataSource = customersDataSet
.DataMember = "Customers"
End With
End Sub
Just to clarify this answer. For Future Reference.
Public Class Form1
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\test.accdb;")
Private Sub Button1_Click
Dim da As New OleDbDataAdapter
Dim table As String = ComboBox1.SelectedItem.ToString
Dim sql As String = String.Format("select * from [{0}]", table)
da.SelectCommand = New OleDbCommand(sql, con)
DataGridView1.DataSource = DataSet.Tables(table)
da.Fill(DataSet, table)
End Sub
End Class
Not trying to Ressurect, but i thought the working version of this VB Script should be posted.
I would like to point out that the dataset is held by Visual Studio, hence the strange source string.