Where is my mistake? - vb.net

I have table which is connected to datagridview and I would like to enter new data by using text boxes. I have following code but it gives me error. Will be appreciated if you help me. Error Message is : Use the "new" keyword to create an object instance
Dim meter As DataTable = Me.DataSet1.Tables.Item("tblmeters")
Dim row As DataRow = meter.NewRow()
row.Item("No") = Me.txtno.Text
row.Item("Turnover") = Me.txtturnover.Text
row.Item("Total Win") = Me.txttotalwin.Text
row.Item("Games Played") = Me.txtgamesplayed.Text
row.Item("Credit In") = Me.txtcreditin.Text
row.Item("Bill In") = Me.txtbillin.Text
row.Item("Hand Pay") = Me.txthandpay.Text
row.Item("Date") = DateTime.Today.ToShortDateString
meter.Rows.Add(row)
Me.TblMeterTableAdapter.Update(Me.DataSet1.tblMeter)
meter.AcceptChanges()

Looks like Me.DataSet1.Tables.Item("tblmeters") returns Nothing. Make sure that DataSet1 indeed contains table tblmeters.

Related

I can not see values in my dropdown

I am using DataSet/DataTable adapters to retrieve data from my database.
Now I need a list of suppliers to my dropdown but unfortunately it is empty all the time, even if I catch with a breakpoint that there are some results.
Here is my code:
Dim riLookup As New RepositoryItemLookUpEdit()
riLookup.NullText = String.Empty
riLookup.DataSource = DataTableDobTableAdapter.FillDob(Me.DsOrders.DataTableDob)
riLookup.ValueMember = "ID"
riLookup.DisplayMember = "TITLE"
riLookup.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup
GridView1.Columns("Code").ColumnEdit = riLookup
What I see when I am debugging:
First of all I dont know why is integer writter there,
and when I continue with debugging and apps run there are empty values:
EDIT:
I never before worked with v.b and devexpress controles so all of this is very confusing to me..
# ED when I rightclick on my method and choose "Go To Definition" I am seeing this:
Public Overloads Overridable Function FillDob(ByVal dataTable As dsOrders.DataTableDobDataTable) As Integer
Me.Adapter.SelectCommand = Me.CommandCollection(0)
If (Me.ClearBeforeFill = true) Then
dataTable.Clear
End If
Dim returnValue As Integer = Me.Adapter.Fill(dataTable)
Return returnValue
End Function
I can see there As Integer but why is that if I can see on data preview my real columns from database and neither of them is Integer.. I am really confused here :/
The fill method on the adapter fills the DataTable and returns the count of rows that it filled the DataTable with. It's right there in the code you showed me for the fill method:
Dim returnValue As Integer = Me.Adapter.Fill(dataTable)
Return returnValue
Me.Adapter.Fill(dataTable). Then the DataTable has rows in it.
The rows are in the DataTable. Use the DataTable as the data source.
Dim riLookup As New RepositoryItemLookUpEdit()
riLookup.NullText = String.Empty
DataTableDobTableAdapter.FillDob(Me.DsOrders.DataTableDob)
riLookup.DataSource = Me.DsOrders.DataTableDob
riLookup.ValueMember = "ID"
riLookup.DisplayMember = "TITLE"
riLookup.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup
GridView1.Columns("Code").ColumnEdit = riLookup

vb.net form linq nullreferenceexception

I'm working on a Sindows Forms application to help keep inventory of some scanners. I'm using Linq2Sql, each table has an id column. On my repair history form. I'm trying to use the serial number from the inventory table so it goes to the database and looks up the sID from the table and it returns the correct value, but when I go to send all the entered data to the history table it gets a null reference exception.
Dim db As New DataClasses1DataContext
Dim rep As Scanner_Repair_History
Dim scan = (From Scanner_Inventory In db.Scanner_Inventories Where scannerid.Text = Scanner_Inventory.SN Select Scanner_Inventory.SID).FirstOrDefault
rep.SID = scan
rep.Date_Broken = datebroke.Value
rep.Description = description.Text
rep.Send_Date = senddate.Text
rep.Recieve_Date = recievedate.Text
rep.Cost = cost.Text
rep.PlantID = plantid.Text
rep.BID = brokenid.Text
rep.RMAnumber = rmanum.Text
db.Scanner_Repair_Histories.InsertOnSubmit(rep)
db.SubmitChanges()
is that me but you didn't instanciate your "rep" variable
You don't have a defined object for placement with a 'new' keyword but I am also curious if it is a system.type.
Update based on Jinx88909
You may be returning an entire POCO Object that may be null and have a null property. You can adjust this most times by doing a null condition if you are using .NET 4.5 and up. '?.' operator.
Dim db As New DataClasses1DataContext
'I need to be a new object and not instantiated as Nothing
Dim rep As New Scanner_Repair_History
'You have the potential for a nothing value here as 'FirstOrDefault' includes a potential Nothing'.
'I would get the entire object and then just a property of it after the fact
Dim scan = (From Scanner_Inventory In db.Scanner_Inventories Where scannerid?.Text = Scanner_Inventory?.SN Select Scanner_Inventory).FirstOrDefault?.Sid
If scan IsNot Nothing Then
rep.SID = scan 'Could you maybe want scan.Id or something similar?
rep.Date_Broken = datebroke.Value
rep.Description = description.Text
rep.Send_Date = senddate.Text
rep.Recieve_Date = recievedate.Text
rep.Cost = cost.Text
rep.PlantID = plantid.Text
rep.BID = brokenid.Text
rep.RMAnumber = rmanum.Text
db.Scanner_Repair_Histories.InsertOnSubmit(rep)
db.SubmitChanges()
Else
Console.WriteLine("I got nothing for you with the inputs you put in!")
End If

Aviod duplicate items in datasource bind checked list box in vb

I made program for populating the data in checked list box. But the problem is the repeated names are displayed in checkedlist box. My code is given below. How can I avoid the duplicate names in checkelstbox using this databind property?
Dim str = "select distinct t.vc_doctype ***********"
conobj.readdata1(str)
CheckedListBox1.DataSource = conobj.ds.Tables(0)
CheckedListBox1.DisplayMember = "vc_doctype"
CheckedListBox1.ValueMember = "vc_doctype"
If you dont want to check your query, you can get whatever result to a datatable and in your code you can get the distinct values to a new Datatable.
For the code above you can follow something like this
Dim view As New DataView(conobj.ds.Tables(0))
Dim DistinctValues As New DataTable
DistinctValues = view.ToTable(True, "vc_doctype")
CheckedListBox1.DataSource = DistinctValues
CheckedListBox1.DisplayMember = "vc_doctype"
CheckedListBox1.ValueMember = "vc_doctype"

VB.net & Access query

Im currently doing a vb.net project for college and want to create a new access record using textboxes, masked textboxes and richtextboxes using the vb gui. However I keep getting this exception:
"An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Syntax error in INSERT INTO statement."
Here is my code which is working on other forms
Private Sub btnSaveNew_Click(sender As Object, e As EventArgs) Handles btnSaveNew.Click
Dim objrow As DataRow
objrow = objDataSet.Tables("tblEngineersReport").NewRow
objrow.Item("To") = txtTo.Text
objrow.Item("Date_Carried_Out") = txtCompletedDate.Text
objrow.Item("Description_Of_Work") = txtWorkDescription.Text
objrow.Item("Comment") = txtComment.Text
objrow.Item("Quantity1") = txtQuantity1.Text
objrow.Item("Quantity2") = txtQuantity2.Text
objrow.Item("Quantity3") = txtQuantity3.Text
objrow.Item("Quantity4") = txtQuantity4.Text
objrow.Item("Item_Description1") = txtDescription.Text
objrow.Item("Item_Description2") = txtDescription2.Text
objrow.Item("Item_Description3") = txtDescription3.Text
objrow.Item("Item_Description4") = txtDescription4.Text
objrow.Item("Unit_Price1") = txtUnitPrice1.Text
objrow.Item("Unit_Price2") = txtUnitPrice2.Text
objrow.Item("Unit_Price3") = txtUnitPrice3.Text
objrow.Item("Unit_Price4") = txtUnitPrice4.Text
objrow.Item("Rate1") = txtRate1.Text
objrow.Item("Rate2") = txtRate2.Text
objrow.Item("Rate3") = txtRate3.Text
objrow.Item("Labour1") = txtDescription5.Text
objrow.Item("Labour2") = txtDescription6.Text
objrow.Item("Labour3") = txtDescription7.Text
objrow.Item("Hours_Worked1") = txtHours1.Text
objrow.Item("Hours_Worked2") = txtHours2.Text
objrow.Item("Hours_Worked3") = txtHours3.Text
objDataSet.Tables("tblEngineersReport").Rows.Add(objrow)
objEngineerDA.Update(objDataSet, "tblEngineersReport")
Retrieve()
MessageBox.Show("new record added")
cboJobID.Enabled = True
End Sub
the Quanity textboxes down to the hours worked are contained within a table layout panel and am just wondering would this have anything to do with the record not saving?
Looking at the names of your columns I could notice that you have a column named TO. This is a reserved keyword in MS-Access and thus the autogenerated queries for your adapter will have a syntax error if you don't tell to your OleDbCommandBuilder to encapsulate the column names with the appropriate QuotePrefix and QuoteSuffix string.
You need to add this code, just after the declaration and initialization of your OleDbCommandBuilder-
Dim builder = new OleDbCommandBuilder(objEngineerDA)
builder.QuotePrefix = "["
builder.QuoteSuffix = "]"
You'll need to check the INSERT statement used in the definition of your DataAdapter (objEngineerDA). The syntax of that INSERT is apparently incorrect, according to the error. Without seeing what is currently there, I cannot advise as to what is wrong with it.

LINQ Query Causing Exit Sub or Swallowing Error

My code is as follows:
Using _EntityModel As New AboveNemaSalesDatabaseEntities()
Dim _SelectActiveOptionCodes = (From _OptCodes In _EntityModel.tblOptionCodes
Where _OptCodes.fdStatus = "A"
Select _OptCodes.fdDescription, _OptCodes.fdOptionCode).ToList()
Dim _SelectActiveOptionCodes2 = (From _OptCodes In _EntityModel.tblOptionCodes
Where _OptCodes.fdStatus = "A"
Select New optionCodes With {.description = _OptCodes.fdDescription,
.optionCode = _OptCodes.fdOptionCode})
sortableOptionCodes = _SelectActiveOptionCodes2
sortedOptionCodes = _SelectActiveOptionCodes2
OptionCodeListBox.DataSource = sortedOptionCodes
OptionCodeListBox.DisplayMember = "fdDescription"
OptionCodeListBox.ValueMember = "fdOptionCode"
End Using
The first query works fine and returns a list in the format [index]{description = "descritption here", optionCode = "option code here"}
The second query creates but when it is called to save to my custom class the program exits the sub or swallows an error. Stepping through the code, the line starting with sortedOptionCodes and after never runs.
The main issue I was dealing with is that my query was producing a list of optionCodes and my variable was not prepared to store this.
Old variables:
Dim sortableOptionCodes As optionCodes
Dim sortedOptionCodes As optionCodes
New variables:
Dim sortableOptionCodes As List(Of optionCodes)
Dim sortedOptionCodes As List(Of optionCodes)
I also added a .ToList() function to the end of the second query.