vb.net... What am i doing wrong working with access - vb.net

My probelm is at da.Update(dt). I recieve OleDbException was unhandled. Syntax error in INSERT INTO statement. The weird thing is it ran before when I was only saving 10 items and now it doesn't run at all quite confused. Thanks for any assistance.
Dim dt As New DataTable
Dim ds As New DataSet
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\MyComplete.accdb;Persist Security Info=False;"
con.Open()
MsgBox("here")
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("SELECT * FROM DealerReview", con)
da.Fill(dt)
Dim newRow As DataRow = dt.NewRow
newRow.Item(1) = User
newRow.Item(2) = Associate
newRow.Item(3) = "1"
newRow.Item(4) = Time
newRow.Item(5) = Hold
newRow.Item(6) = GreetingR
newRow.Item(7) = GreetingA
newRow.Item(8) = GreetingO
newRow.Item(9) = GreetingTs
newRow.Item(10) = GreetingG
newRow.Item(11) = holdUpdate
newRow.Item(12) = LookupSize
newRow.Item(13) = DlyD
newRow.Item(14) = SiPrice
newRow.Item(15) = SiDoorPrice
newRow.Item(16) = TBrand
newRow.Item(17) = TModel
newRow.Item(18) = SeveralChoices
newRow.Item(19) = Financing
newRow.Item(20) = Benefits
newRow.Item(21) = Apt
newRow.Item(22) = ITime
newRow.Item(23) = AssociateScore
newRow.Item(24) = hms
newRow.Item(25) = ymd
newRow.Item(26) = ElapsedTime
dt.Rows.Add(newRow)
Dim cb As New OleDbCommandBuilder(da)
cb.GetInsertCommand()
da.Update(dt)
MsgBox("Saved")
con.Close()

This is the error that you get when your Item's count and/or datatype's do not match the column definitions of the table that you are writing to. There are other possibilities, but since you mentioned that you added more items, and you have not shown us the table definition, that seems the most likely possibility.
You cannot just add more items (columns) to a database command, they have to match the table columns that you are reading/writing from/to.

Related

The LEVEL clause includes a reserved word or argument that is misspelled or missing, or the punctuation is incorrect vb.net

Dim connstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Users\vladut.moraru\Desktop\TCO Orders\02_Template_BOM.xls;Extended Properties=""Excel 8.0;HDR=YES;"""
Dim pram As OleDbParameter
Dim dr As DataRow
Dim olecon As OleDbConnection
Dim olecomm As OleDbCommand
Dim olecomm1 As OleDbCommand
Dim oleadpt As OleDbDataAdapter
Dim ds As DataSet
Try
olecon = New OleDbConnection
olecon.ConnectionString = connstring
olecomm = New OleDbCommand
olecomm.CommandText = "Select Level, F_N, Name, Rev from [BOM$]"
olecomm.Connection = olecon
olecomm1 = New OleDbCommand
olecomm1.CommandText = "Insert into [BOM$] " & "(Level, F_N, Name, Rev) values " & "(#FName, #LName, #Age, #Phone)"
olecomm1.Connection = olecon
pram = olecomm1.Parameters.Add("#FName", OleDbType.VarChar)
pram.SourceColumn = "Level"
pram = olecomm1.Parameters.Add("#LName", OleDbType.VarChar)
pram.SourceColumn = "F_N"
pram = olecomm1.Parameters.Add("#Age", OleDbType.VarChar)
pram.SourceColumn = "Name"
pram = olecomm1.Parameters.Add("#Phone", OleDbType.VarChar)
pram.SourceColumn = "Rev"
oleadpt = New OleDbDataAdapter(olecomm)
ds = New DataSet
olecon.Open()
oleadpt.Fill(ds, "BOM")
If IsNothing(ds) = False Then
dr = ds.Tables(0).NewRow
dr("Level") = "Raman"
dr("F_N") = "Tayal"
dr("Name") = 24
dr("Rev") = 98989898
ds.Tables(0).Rows.Add(dr)
oleadpt = New OleDbDataAdapter
oleadpt.InsertCommand = olecomm1
Dim i As Integer = oleadpt.Update(ds, "BOM")
End If
Catch ex As Exception
Finally
olecon.Close()
olecon = Nothing
olecomm = Nothing
oleadpt = Nothing
ds = Nothing
dr = Nothing
pram = Nothing
End Try
I want tot write into tamplate excel with column Level, F_N, Name, Rev and my code generate that error from title. I want to insert some value in my excel but on line oleadpt.Fill(ds, "BOM") give me that error. I put a pic with excel file. The sheet name is BOM
My question is, where i miss to generate that code? I write wrong my columns or my connection?

VB.NET getReturn of dataset always give same string

i have problem with vb.net when i code some script like this
Dim main_class As New query_sync_desktop
cn = MySQLi.MySQLConnectionMobile
Query = "SHOW TABLES"
da = New MySqlDataAdapter(Query, cn)
ds = New DataSet
ds.Clear()
da.Fill(ds, "Tables_in_XXXXX")
For i = 0 To ds.Tables(0).Rows.Count - 1
MyTable = ds.Tables(0).Rows.Item(i).Item(0).ToString()
MsgBox(MyTable)
Next
it's will be show all the database i have, but when i use for other operation "MyTable" always show the same string.
Dim main_class As New query_sync_desktop
cn = MySQLi.MySQLConnectionMobile
Query = "SHOW TABLES"
da = New MySqlDataAdapter(Query, cn)
ds = New DataSet
ds.Clear()
da.Fill(ds, "Tables_in_XXXXX")
For i = 0 To ds.Tables(0).Rows.Count - 1
MyTable = ds.Tables(0).Rows.Item(i).Item(0).ToString()
Call main_class.getSyncron()
Next
Replace this:
MyTable = ds.Tables(0).Rows.Item(i).Item(0).ToString()
with this:
MyTable = ds.Tables(0).Rows(i).Item(0).ToString
Here I assume you just want to see the content of the index 0 for each row.

Getting DevExpress Chart Series Values From Array VB.NET

I was trying to set DevExpress Chart Series from Data inside SQL Table.
Everything went fine except that the chart takes only the last attribute from the last series.
My code is:
con.Open() 'it opens SQL Connection
For i = 0 To (CheckedListBoxControl1.CheckedItems.Count - 1) 'list from ListBox
Lst.Add(CheckedListBoxControl1.CheckedItems(i).ToString) 'Putting Data in Array List
Dim Strl As String = String.Format("Select * from VPRogressCumulative where fname like '{0}' and lname like '{1}' order by id, no, CAST('1.' + date AS datetime)", ComboBox1.Text, Lst(i))
Dim sqlCom As New SqlCommand(Strl)
sqlCom.Connection = con
Dim myDA As SqlDataAdapter = New SqlDataAdapter(sqlCom)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "VPRogressCumulative")
ChartControl1.DataSource = myDataSet
ChartControl1.DataAdapter = myDA
Dim ser As New Series(Lst(i), ViewType.Line)
ChartControl1.Series.Add(ser)
ser.ArgumentDataMember = "VPRogressCumulative.Date"
ser.ValueDataMembers.AddRange(New String() {"VPRogressCumulative.Cumulative"})
Next
con.Close()
I believe my Problem is in:
Dim ser As New Series(Lst(i), ViewType.Line)
ChartControl1.Series.Add(ser)
ser.ArgumentDataMember = "VPRogressCumulative.Date"
ser.ValueDataMembers.AddRange(New String() {"VPRogressCumulative.Cumulative"})
Last two lines are giving the same series new attributes which I wasn't able to resolve the issue.
Your problem is here:
ChartControl1.DataSource = myDataSet
ChartControl1.DataAdapter = myDA
In each iteration of cycle you are creating the new DataSet and it's overrides previous DataSource in your ChartControl1. Your must use Series.DataSource instead of ChartControl.DataSource. Also you can use DataTable instead of DataSet.
Here is example:
'Your code:
'Dim myDataSet As DataSet = New DataSet()
'myDA.Fill(myDataSet, "VPRogressCumulative")
'ChartControl1.DataSource = myDataSet
'ChartControl1.DataAdapter = myDA
'Replace with this:
Dim myDataTable As DataTable = New DataTable("VPRogressCumulative")
myDA.Fill(myDataTable)
Dim ser As New Series(Lst(i), ViewType.Line)
ChartControl1.Series.Add(ser)
'Your code.
'ser.ArgumentDataMember = "VPRogressCumulative.Date"
'ser.ValueDataMembers.AddRange(New String() {"VPRogressCumulative.Cumulative"})
'Replace with this:
ser.DataSource = myDataTable
ser.ArgumentDataMember = "Date"
ser.ValueDataMembers.AddRange(New String() {"Cumulative"})

Separating Chart Series

I currently have a chart element that has 4 Series. My problem is that while they work individually, if I set more than 1 Series it sets the data for all of them to the last DataSet (So Series 1,2 & 3 have the same positions as 4).
Could someone have a look at my broken code and let me know where it's all going wrong? And maybe some pointers on neatening it up... I have never worked with any form of charts before.
Using con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test\Response.mdb;")
Dim MyQuery As String = "SELECT qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood, Count(qry_Response_By_Date_1.Mood) AS CountOfMood FROM qry_Response_By_Date_1 GROUP BY qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood HAVING (((qry_Response_By_Date_1.Mood)='Happy'));"
Using cmd = New OleDbCommand(MyQuery, con)
Dim MyData As New OleDbDataAdapter(MyQuery, con)
Dim MyDataSet As New DataSet
con.Open()
MyData.Fill(MyDataSet, "Table")
ChrtMoodChanges.DataSource = MyDataSet.Tables("Table")
Dim Series1 As Series = ChrtMoodChanges.Series("Series1")
Series1.Name = "Happy"
ChrtMoodChanges.Series(Series1.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series1.Name).YValueMembers = "CountOfMood"
End Using
Dim MyQuery2 As String = "SELECT qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood, Count(qry_Response_By_Date_1.Mood) AS CountOfMood FROM qry_Response_By_Date_1 GROUP BY qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood HAVING (((qry_Response_By_Date_1.Mood)='Neutral'));"
Using cmd = New OleDbCommand(MyQuery2, con)
Dim MyData2 As New OleDbDataAdapter(MyQuery2, con)
Dim MyDataSet2 As New DataSet
MyData2.Fill(MyDataSet2, "Table")
ChrtMoodChanges.DataSource = MyDataSet2.Tables("Table")
Dim Series2 As Series = ChrtMoodChanges.Series("Series2")
Series2.Name = "Neutral"
ChrtMoodChanges.Series(Series2.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series2.Name).YValueMembers = "CountOfMood"
End Using
Dim MyQuery3 As String = "SELECT qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood, Count(qry_Response_By_Date_1.Mood) AS CountOfMood FROM qry_Response_By_Date_1 GROUP BY qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood HAVING (((qry_Response_By_Date_1.Mood)='Sad'));"
Using cmd = New OleDbCommand(MyQuery3, con)
Dim MyData3 As New OleDbDataAdapter(MyQuery3, con)
Dim MyDataSet3 As New DataSet
MyData3.Fill(MyDataSet3, "Table")
ChrtMoodChanges.DataSource = MyDataSet3.Tables("Table")
Dim Series3 As Series = ChrtMoodChanges.Series("Series3")
Series3.Name = "Sad"
ChrtMoodChanges.Series(Series3.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series3.Name).YValueMembers = "CountOfMood"
End Using
Dim MyQuery4 As String = "SELECT qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood, Count(qry_Response_By_Date_1.Mood) AS CountOfMood FROM qry_Response_By_Date_1 GROUP BY qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood HAVING (((qry_Response_By_Date_1.Mood)='Angry'));"
Using cmd = New OleDbCommand(MyQuery4, con)
Dim MyData4 As New OleDbDataAdapter(MyQuery4, con)
Dim MyDataSet4 As New DataSet
MyData4.Fill(MyDataSet4, "Table")
ChrtMoodChanges.DataSource = MyDataSet4.Tables("Table")
Dim Series4 As Series = ChrtMoodChanges.Series("Series4")
Series4.Name = "Angry"
ChrtMoodChanges.Series(Series4.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series4.Name).YValueMembers = "CountOfMood"
End Using
End Using
P.S. the SQL returns the correct data for each... it's just getting it to store the correct data in the charts is my problem.
Everytime you set the ChrtMoodChanges.DataSource you are overwriting the datasource for the chart, thats why you end up with only the last dataset.
I would suggest you merge your datatables before you start binding them to the chart then reference each series to a column in your datasource/datatable
ChrtMoodChanges.Series(Series1.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series1.Name).YValueMember = "CountOfMood_Col1"
....
....
ChrtMoodChanges.Series(Series4.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series4.Name).YValueMember = "CountOfMood_Col4"
So as a suggestion to cleaning up the code, I would do the below
Get the 4 datatables
Merge the datatables with "Actual_Date" as the primary key
add each column to the chart as a new series
Merging Example: Merge columns of different types from datatables into one larger datatable
Hope this helps

Not able read the next record using datareader

I want to retrieve data from multiple tables and want to generate a crystal report. Hence i have created a new table and inserting values in it each time i need to generate the report. So i am using the following code to retrieve the data from those tables.
Code:
Private Sub gen_Report()
Dim dr, dr1, dr2 As OleDb.OleDbDataReader
Dim cmd, cmdDel, comm_inv1, comm_invuser As OleDb.OleDbCommand
If cnnOLEDB.State = ConnectionState.Closed Then
cnnOLEDB.Open()
End If
Dim strDelInsRp As String = ("DELETE FROM Inst_Report")
cmdDel = New OleDb.OleDbCommand(strDelInsRp, cnnOLEDB)
cmdDel.Parameters.AddWithValue("#chlno", cmbChal_no.Text)
cmdDel.ExecuteNonQuery()
Dim strSelIns As String = ("SELECT * FROM Installation_det where Chalan_No=#chlno")
cmd = New OleDb.OleDbCommand(strSelIns, cnnOLEDB)
cmd.Parameters.AddWithValue("#chlno", cmbChal_no.Text)
dr = cmd.ExecuteReader
Try
Do While dr.Read = True
mach_srno = dr("Machine_SrNo")
tft_srno = dr("TFT_SrNo")
chl_no = dr("Chalan_No")
usernm = dr("User_Name")
ins_dt = dr("Date_Of_Installation")
war_perd = dr("Warranty_Period")
war_till = dr("Warranty_Valid_Till")
Dim strSelInv1 As String = ("SELECT * FROM INVOICE_ONE where LAY_NO='VDC' AND CHL_NO=#chn_no ")
comm_inv1 = New OleDb.OleDbCommand(strSelInv1, cnnOLEDB)
comm_inv1.Parameters.AddWithValue("#chn_no", chl_no)
dr1 = comm_inv1.ExecuteReader
If dr1.Read = True Then
doc_no = dr1("DOCU_NO")
code_no = dr1("CODE_NO")
memb_nm = dr1("MEMB_NM")
Dim strSelInvUser As String = ("SELECT * FROM INVOICE_USER where CODE_NO=#code AND LAY_NO='VDC' AND DOCU_NO=#docno")
comm_invuser = New OleDb.OleDbCommand(strSelInvUser, cnnOLEDB)
comm_invuser.Parameters.AddWithValue("#code", code_no)
comm_invuser.Parameters.AddWithValue("#docno", doc_no)
dr2 = comm_invuser.ExecuteReader
If dr2.Read = True Then
User_add = dr2("ILEN2") & dr2("ILEN3") & dr2("ILEN4") & dr2("ILEN5")
End If
dr2.Close()
End If
dr1.Close()
Dim strInsRep As String = "INSERT INTO Inst_Report(Mach_srNo,TFT_srNo,Mem_nm,UserNm,Dt_Inst,War_Per,war_till,User_Address) VALUES (#mach_srno,#tft_no,#mem_nm,#uname,#inst_dt,#war_per,#war_till,#address)"
Dim comm_InsRep As OleDb.OleDbCommand = New OleDb.OleDbCommand(strInsRep, cnnOLEDB)
comm_InsRep.Parameters.AddWithValue("#mach_srno", mach_srno)
comm_InsRep.Parameters.AddWithValue("#tft_no", tft_srno)
comm_InsRep.Parameters.AddWithValue("#mem_nm", memb_nm)
comm_InsRep.Parameters.AddWithValue("#uname", usernm)
comm_InsRep.Parameters.AddWithValue("#inst_dt", ins_dt)
comm_InsRep.Parameters.AddWithValue("#war_per", war_perd)
comm_InsRep.Parameters.AddWithValue("#war_till", war_till)
comm_InsRep.Parameters.AddWithValue("#address", User_add)
comm_InsRep.ExecuteNonQuery()
Loop
dr.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
But the problem is that the datareader only reads for the first record even if I am using 'do While Loop'.
I have another question:
As the number of fields are more I want to generate report in Landscape orientation.
So how to change the orientation of report.
I am using Visual studio 2005 and MS-Access 2007. And programming language is VB.NET.
Regarding your data, I believe the standard way to get data from an OleDbDataReader is to use it to fill a datatable, try this:
Private Function GetDataTableUsingDataReader(ByVal command As OleDbCommand) As DataTable
Dim dataTable As DataTable = New DataTable
Using reader As OleDbDataReader = command.ExecuteReader
dataTable.Load(reader, LoadOption.OverwriteChanges)
End Using
Return dataTable
End Function
To get your crystal report to landscape, right click on the report in the designer, goto design => page setup. From here you can change the orientation.