Adding new records to a DataTable - sql

I am having trouble inserting new rows into a database with multiple tables. Currently I have my database connected to my Visual Basic project. What I have so far is this:
Imports System.Data.SqlClient
Public Class MainForm
Private m_cn As New SqlConnection()
Private m_DA As SqlDataAdapter
Private m_CB As SqlCommandBuilder
Private m_DataTable As New DataTable
Private m_IntRowPosition As Integer = 0
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles Me.Load
m_cn.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename = C:\Users\pattanasio\Desktop\Visual Basic Projects\DPG Project Hours Database\Mestek Database.mdf;Integrated Security=True;Connect Timeout=30"
m_cn.Open()
m_DA = New SqlDataAdapter("SELECT * FROM Employee_Table", m_cn)
m_CB = New SqlCommandBuilder(m_DA)
m_DA.Fill(m_DataTable)
End Sub
Private Sub btnInsertIntoDatabase_Click(sender As Object, e As EventArgs) Handles btnInsertIntoDatabase.Click
Dim drNewRow As DataRow = m_DataTable.NewRow()
For m_IntRowPosition = 0 To lstScannedNames.Items.Count()
If m_DataTable.Rows.Count = 0 Then
drNewRow("emp_id") = m_IntRowPosition + 1
drNewRow("emp_name") = lstScannedNames.Items.Item(m_IntRowPosition)
m_DataTable.Rows.Add(drNewRow)
m_DA.Update(m_DataTable)
m_IntRowPosition = m_DataTable.Rows.Count - 1
End If
Next m_IntRowPosition
End Sub
End Class
When I run my program and click that button, I get an error on the drNewRow("emp_name") line saying that the column emp_name does not belong to the table. The full error says it is An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll I'm also wondering why the error isn't thrown on the line before where I reference the emp_id column. My guess is that I'm getting this error because my DataTable object does not know which data table in the database to reference. Also, it may be worth noting that emp_id is the primary key for 1 of my 5 tables and is also the foreign key in 3 other tables. I feel like I'm close but I've been stuck for a few hours. Sorry if my terminology isn't spot on, I'm quite new at this.
My Database looks like this.
Here is the pastebin link to my code: http://pastebin.com/UiYXEZMG
Ok, it is definitely worth noting that when I look through the Autos window to see the table my drNewRow is inserting into, and then use the text visualizer, it shows the 4 columns I originally had. 1 column for emp_id and 3 other columns for first, middle, and last name. However, in the database designer, I deleted 2 of the name columns and renamed the remaining one to emp_name, without touching emp_id. This is definitely the cause of my problem but I'm not sure how to fix it. So in the database designer .xsd file it shows my table how I would like it to be. But in the Autos window it shows that it is using the old table. Also if I wanted to work with a different table, would I just have to SELECT a different table in my SqlDataAdapter?

Related

Datagridview - Oracle Update error "Dynamic SQL generation failed."

I'm using Datagridview to show me joined records from 2 tables. Data that is showing is from one of the tables + data that are in joined table (Table3). SQL query returns results in Datagridview (works fine in Oracle too), but update fails with "Dynamic SQL generation failed. Either no base tables were found or more than one base table was found". Here is my table design:
Table1:
ID_TABLE1
ITEM_NAME
ITEM_DESCRIPTION
Table3: (this is a joined view for Table1 and Table2)
ID_TABLE3
ID_TABLE1_FK
ID_TABLE3_FK
VALIDITY
DATE_CONNECTION
My code (exactly as Oracle recommends):
Public Class Form2
Private da As OracleDataAdapter
Private cb As OracleCommandBuilder
Private ds As DataSet
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Saving.Enabled = False 'this deals with error with updating (from Oracle site)
Dim SQL As String = "SELECT ID_TABLE1, ID_TABLE3, SERIAL_NO, ITEM_NAME, ITEM_DESCRIPTION, VALIDITY, DATE_CONNECTION from TABLE1, TABLE2 WHERE TABLE3.ID_TABLE1_FK=" & Form1.DataGridView1.CurrentRow.Cells(0).Value.ToString
Try
Oracleconn() 'connection to my DB
Dim cmd = New OracleCommand(SQL, Oracleconn)
cmd.CommandType = CommandType.Text
da = New OracleDataAdapter(cmd)
cb = New OracleCommandBuilder(da)
ds = New DataSet()
da.Fill(ds)
My_DGV.DataSource = ds.Tables(0)
Saving.Enabled = True
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'No closing of connection here because of working with Dataset (Oracle suggestion)
End Try
End Sub
Private Sub Saving
da.Update(ds.Tables(0))
Saving.Enabled = True
End Sub
End Class
So, Is my SQL query wrong or what ? Any help would be much appreciated !
P.S.: In actual case only column "VALIDITY" from Table3 will be allowed to change for users, so I need to update only that field.
This is too complicated for me, looks like Oracle-provided suggestion for working with Datasets just isn't easy when you want to perform Update on join table records. So I tried a different approach and It worked for me. Since what I need is to update only 1 column from SQL query which returns to Datagridview I did this :
For Each row As DataGridViewRow In My_.Rows
cmd.Parameters.Add(New OracleParameter("validity", row.Cells(6).Value))
cmd.CommandText = "UPDATE TABLE3 SET VALIDITY= : validity WHERE ID_TABLE1_FK='" & row.Cells(1).Value & "'"
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Next
If anyone knows the answer to my original question - so how to do same with just da.Update(ds.Tables(0)), then please let me know. I reckon that SQL query needs to be properly changed, using JOIN method.

How to write query based on datagridview results?

Hi I have a query which populates a datagridview which all works correctly. When the datagrid has been populated it has the following columns
UserID
UserName
UserDepot
I have a button which when pressed I would like to run another query based on the information in column UserID and whatever row is selected. I am having difficulty writing this query as I dont know what command I need to write in order to call this information
the datagrid is called dg_usersearch so I was expecting to use something like dg_usersearch.selectedvalue but selectedvalue isnt a member of datagridview does anyone know what command I should be using?
The query for filling the datagrid is as follows
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'creates a list of all incidents as per the search parameters
Dim dt As New DataTable
Dim query As String = "select [userid],[username],[userdepot] from [tbluser]"
Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MyDATASOURCE")
Using command As New OleDbCommand(query, connection)
Using adapter As New OleDbDataAdapter(command)
connection.Open()
adapter.Fill(dt)
connection.Close()
End Using
End Using
End Using
'checks to make sure query has results
If dt.Rows.Count > 0 Then
dgusersearch.DataSource = dt
'if no results display the following message
ElseIf dt.Rows.Count = 0 Then
MsgBox(Prompt:="No results for search request, please try again")
End If
Answered by #Plutronix above,
myDGV.SelectedRows(0).Cells(0).Value.ToString()

Executing delete command on MS-Access through VB.NET does not delete the records

Just trying to delete all rows of an Access table:
Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click
'Clear old records from extract table
Dim sqlConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\CLI_CRVM.accdb")
Dim cmd As New System.Data.OleDb.OleDbCommand()
Dim intRC As Integer
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "DELETE * FROM [extract] ;"
cmd.Connection = sqlConnection
sqlConnection.Open()
intRC = cmd.ExecuteNonQuery()
sqlConnection.Close()
MsgBox(intRC)
End Sub
I do not get any errors but it doesn't delete the rows either. There are currently 10 rows in the table and the MsgBox shows 10 each time the button is clicked and there are still 10 rows in the table afterwards.
What am I missing?
Try without the asterisk:
DELETE FROM [extract];
You don't generally supply any columns in a DELETE. You only supply columns that you want to match a row against .. via a WHERE clause afterwards.
There are certain dialects of SQL that let you specify thresholds on what you delete in that part of the query - however, I don't believe Access' dialect does (I could be wrong).

data grid view update ,Edit and delete in vb.net windows form that using multiple table to populate datagridview

I am new to windows Forms applications...
I am working in VB.NET windows forms and dealing with DataGridView...
I have populated that DataGridView from two Table
Please tell me How to Add , Delete and Edit/Update the records of the DATAGridview back to the DataBase.
I am using SQLSERVER 2008 DataBase
I have two tables 1->CompanyMaster_tbl in this having Two fields . Cid and CompanyName,
Cid is the primary key of this table
2->DepartmentMaster_tbl in this having 4 fields. dtid,dtname,dtphon,dtmail,Cid.
dtid is the primary key,and Cid is the foreign key..
My data gridview look like this:
Dim adapter As SqlDataAdapter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'NOTE: I removed the Using statements to ease your tests/understanding, but you shouldn't let the connections opened (or, at least, set connection-release part somewhere)
Dim con As SqlConnection = New SqlConnection() 'SET THE CONNECTION STRING
con.Open()
adapter = New SqlDataAdapter("select c.CompanyName,d.dtName,d.dtPhone,d.dtEmail from CompanyMaster_tbl c join DepartmentMaster_tbl d on c.Cid=d.cId", con)
Dim dt As DataTable = New DataTable()
adapter.Fill(dt) 'Filling dt with the information from the DB
gv.DataSource = dt 'Populating gv with the values in dt
End Sub
in update button i wrote code like this:
Dim dt1 As DataTable = DirectCast(gv.DataSource, DataTable)
adapter.Update(dt1)
but after editing anything in gridview,,i click the update button,,but i am getting error in this row
da.Update(dt1)
Error:
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
Thanks in advance
If you want to keep some synchronisation between the DataGridView and the DB, you shouldn't add columns/rows manually, but rely on the DataSource property. Sample code adapted to your case:
Using con As SqlConnection = New SqlConnection() 'You have to set the CONNECTION STRING
con.Open()
Using adapter As SqlDataAdapter = New SqlDataAdapter("select c.CompanyName,d.dtName,d.dtPhone,d.dtEmail from CompanyMaster_tbl c join DepartmentMaster_tbl d on c.Cid=d.cId", con)
Dim dt As DataTable = New DataTable()
adapter.Fill(dt) 'Filling dt with the information from the DB
gv.DataSource = dt 'Populating gv with the values in dt
End Using
End Using
The code above extracts all the information you want from the DB, puts it into a DataTable (dt) and then feeds it to the DataGridView as DataSource. gv has now all the values from dt; also any change in dt is reflected in gv and vice versa (this coupling is almost perfect, at least, when updating values; there might be some problems while deleting rows/columns or changing their basic configuration). You might even keep adapter as a global variable (outside the Using statement) and rely on it to update the DB regularly (via adapter.Update(dt), for example).
Quite a few alternatives; but, in any case, relying on a DataSource is certainly better under your conditions.

Data type mismatch in criteria expression ERROR Coming

This is my code for add data.but no data can insert to database newly because error coming
and say Data type mismatch in criteria expression.Why is that?????????
Imports System.Data.OleDb
Public Class Form1
Dim con As OleDbConnection
Dim myAdapter As OleDbDataAdapter
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\DELL\Documents\Visual Studio 2012\Projects\WindowsApplication9\WindowsApplication9\NIBM.accdb")
con.Open()
myAdapter = New OleDbDataAdapter("INSERT INTO Driverdetails values(#DriverNo,#DriverName,#DriverAge,#DriverAddress)", con)
myAdapter.SelectCommand.Parameters.AddWithValue("#Driver No", TextBox1.Text)
myAdapter.SelectCommand.Parameters.AddWithValue("#Driver Name", TextBox2.Text)
myAdapter.SelectCommand.Parameters.AddWithValue("#Driver Age", TextBox3.Text)
myAdapter.SelectCommand.Parameters.AddWithValue("#Driver Address", TextBox4.Text)
myAdapter.SelectCommand.ExecuteNonQuery()
con.Close()
End Sub
Most likely because you've introduced spaces into the parameter names: #Driver No
Added Access requires the field-names to be specified with the INSERT INTO statement (when used to insert a single record). MSDN reference
You can use the INSERT INTO statement to add a single record to a
table using the single-record append query syntax as shown above. In
this case, your code specifies the name and value for each field of
the record. You must specify each of the fields of the record that a
value is to be assigned to and a value for that field. When you do not
specify each field, the default value or Null is inserted for missing
columns. Records are added to the end of the table.