Adding DataRow one by one to DataTable - vb.net

I have a webform for entering Invoice info to a DB.
I have 3 textboxes requesting product quantity, product and price.
I decided to create a DataTable to store temporarily quantity, productId, price so that the user might add N products for later saving the data on the DataTable (displayed on a grid) to the DB.
I don't know what am I doing wrong but am unable to store a second product. After I save 1 row to the DataTable the second one is not added but overwrites the first one. I know this has to be something really stupid on my part but haven´t been able to figure out what am I doing wrong and am sort of a beginner.
Public Class IngresoFacturas
Inherits System.Web.UI.Page
Dim miDataTable = New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
miDataTable.Columns.Add("dCantidad", GetType(System.Int32))
miDataTable.Columns.Add("idProducto", GetType(System.Int32))
miDataTable.Columns.Add("mTotal", GetType(System.Decimal))
End Sub
Protected Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
miDataTable.Rows.Add(RadNumericTextBox2.Text, Int32.Parse(RadAutoCompleteBox2.Entries.Item(0).Value), RadNumericTextBox3.Text)
RadGrid1.Rebind() 'Forces rebind to update Grid
RadAutoCompleteBox2.Entries.Clear()
RadNumericTextBox2.Text = ""
RadNumericTextBox3.Text = ""
End Sub
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
RadGrid1.DataSource = miDataTable
End Sub
End Class

Tried Andrew´s solution but it didn{t work so I came up to this:
Anything you guys could warn me about using ViewState to solve this. It is working.... but don{t know if it{s the best approach
Public Class IngresoFacturas
Inherits System.Web.UI.Page
Dim miDataTable As DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
miDataTable = New DataTable
miDataTable.Columns.Add("dCantidad", GetType(System.Int32))
miDataTable.Columns.Add("idProducto", GetType(System.Int32))
miDataTable.Columns.Add("mTotal", GetType(System.Decimal))
ViewState("tabla") = miDataTable
End If
End Sub
Protected Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
miDataTable = ViewState("tabla")
miDataTable.Rows.Add(RadNumericTextBox2.Text, Int32.Parse(RadAutoCompleteBox2.Entries.Item(0).Value), RadNumericTextBox3.Text)
RadGrid1.Rebind() 'Forces rebind to update Grid
ViewState("tabla") = miDataTable
RadAutoCompleteBox2.Entries.Clear()
RadNumericTextBox2.Text = ""
RadNumericTextBox3.Text = ""
End Sub
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
RadGrid1.DataSource = miDataTable
End Sub
End Class

Something like this might help:
Dim miDataTable As DataTable
Sub PrepareDT()
miDataTable = New DataTable
miDataTable.Columns.Add("dCantidad", GetType(System.Int32))
miDataTable.Columns.Add("idProducto", GetType(System.Int32))
miDataTable.Columns.Add("mTotal", GetType(System.Decimal))
End Sub
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If Not Page.IsPostBack Then
PrepareDT()
End If
End Sub
(and remove the code from the Page_Load method) so that you only "reset" miDataTable on the first access of the page.

Related

How to remove the last item in the list(Of) in vb.net

I have a windows form application which added string into list of collection. This can be done by input the string into the textbox then click 'add' button, and the list will display in a listbox.
Now, I want to delete the last item in the list collection & the listbox.
Below are the code snippett that I have done
Public strList As List(Of String) = New List(Of String)
'add string to list
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TxtBox.Text <> "" Then
strList.Add(TxtBox.Text)
TxtBox.Clear()
End If
lstItem.Items.Clear()
strList.ForEach(AddressOf ListItem)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
lstItem.Items.Clear()
strList.ForEach(AddressOf ListItem)
End Sub
'Add item into list
Public Sub ListItem(s As String)
lstItem.Items.Add(s)
'lstItem.Sorted = True
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
strList.ToList.ForEach(AddressOf DeleteItem)
End Sub
'Delete item
Public Sub DeleteItem(s As String)
For i = 0 To strList.Count
lstItem.Items.RemoveAt(strList.Count - 1)
i = i + 1
Next
End Sub
as you can see, in the sub DeleteItem, i try to delete the last item of the list collection by clicking 'delete button'. but the error says Additional information: InvalidArgument=Value of '1' is not valid for 'index'.
can anyone help me on this? thank you.
What you really ought to do is use a BindingList(Of String) and bind it to the ListBox. That way, you only have to deal with one list. Adding and removing against the underlying BindingList will automatically affect the ListBox:
Private items As New BindingList(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DataSource = items
End Sub
Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
items.Add(TextBox1.Text)
End Sub
Private Sub deleteSelectedButton_Click(sender As Object, e As EventArgs) Handles deleteSelectedButton.Click
items.RemoveAt(ListBox1.SelectedIndex)
End Sub
Private Sub deleteLastButton_Click(sender As Object, e As EventArgs) Handles deleteLastButton.Click
items.RemoveAt(items.Count - 1)
End Sub

How to add an image in Report Viewer with SQL

I am using VB.Net 2013 with SQL Server
What is the problem with my code? Is there something missing?
When I try to select the location, it show me in the Form the name, location and the image.
Everything looks good, the only problem is that the image in report viewer doesn't change.
Imports System.IO
Imports Microsoft.Reporting.WinForms
Public Class Form1
Private Sub Table_locationBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Table_locationBindingNavigatorSaveItem.Click
Me.Validate()
Me.Table_locationBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.KankonDataSet)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'KankonDataSet.Table_location' table. You can move, or remove it, as needed.
Me.Table_locationTableAdapter.Fill(Me.KankonDataSet.Table_location)
If NamelocationComboBox.Text = Nothing Then
NamelocationComboBox.Text = "No thing"
End If
If IdlocationTextBox.Text = Nothing Then
IdlocationTextBox.Text = "No thing"
End If
Dim Param1 As New ReportParameter("ReportParameterlocation", NamelocationComboBox.Text)
ReportViewer1.LocalReport.SetParameters(Param1)
Dim Param2 As New ReportParameter("ReportParameterwhere", IdlocationTextBox.Text)
ReportViewer1.LocalReport.SetParameters(Param2)
Me.ReportViewer1.RefreshReport()
End Sub
Private Sub ReportViewer1_Load(sender As Object, e As EventArgs) Handles ReportViewer1.Load
Me.ReportViewer1.RefreshReport()
End Sub
Private Sub NamelocationComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles NamelocationComboBox.SelectedIndexChanged
Dim Param1 As New ReportParameter("ReportParameterlocation", NamelocationComboBox.Text)
ReportViewer1.LocalReport.SetParameters(Param1)
Me.ReportViewer1.RefreshReport()
End Sub
Private Sub IdlocationTextBox_TextChanged(sender As Object, e As EventArgs) Handles IdlocationTextBox.TextChanged
Dim Param2 As New ReportParameter("ReportParameterwhere", IdlocationTextBox.Text)
ReportViewer1.LocalReport.SetParameters(Param2)
Me.ReportViewer1.RefreshReport()
End Sub
End Class
Image in Attachments!

VB.net WindowsForm changing then [System.Data.MissingPrimaryKeyException]

-I created 2 forms after I use hide() and show() to switch between form.
-In the 2nd form, I create datatable (bond to datagrid) and primary key for datatable to use find().
-After I press [BACK] button form 2nd form to 1st form (use hide() and show() function) and come back to use 2nd form application again. the runtime error show this [System.Data.Missing.PrimaryKeyException: Table doesn't have a primary key]
1st Form
Public Class MainMenu
Public MainForm As MainMenu
Public AssetCheckForm As AssetCheck
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AssetCheckForm.Show()
MainForm.Hide()
End Sub
Public Sub MainMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MainForm = New MainMenu()
AssetCheckForm = New AssetCheck()
End Sub End Class
2nd Form
Dim dtAsset As New DataTable("AssetTable")
Public Sub readData()
If readStatus = 0 Then
Try
Dim splits As String()
Using sr As StreamReader = New StreamReader(inputcsvname)
'read the first line for the table columns
splits = sr.ReadLine.Split(","c)
For i As Integer = 0 To UBound(splits)
dtAsset.Columns.Add(splits(i))
Next
'read the rest of the lines to add rows
Do While Not sr.EndOfStream
splits = sr.ReadLine.Split(","c)
dtAsset.Rows.Add(splits)
Loop
End Using
Catch ex As Exception
Finally
End Try
dtAsset_display = dtAsset.Copy()
totalcount.Text = getRowsCount(dtAsset_display)
dtAsset.Columns("AsstCode").Unique = True
dtAsset.PrimaryKey = New DataColumn() {dtAsset.Columns("AsstCode")}
'bind display part to DataGrid
DataGrid1.DataSource = dtAsset
Private Sub BackButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonF4.Click
clearData()
MainMenu.MainForm.Show()
MainMenu.AssetCheckForm.Hide()
End Sub
I think I missed something about VB.net WinForm concept.
Does anyone know what this error means?
The fact that you are calling MainForm = New MainMenu() in MainForm it means you now have two instances of MainForm.
So then when you call MainMenu.MainForm.Show() you are showing your new instance, not the existing one.
You need to change this around so that you store the reference to the existing MainForm in your AssetCheckForm form.
Something like this:
Public Class MainMenu
Private AssetCheckForm As AssetCheckForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.AssetCheckForm = New AssetCheckForm
Me.AssetCheckForm.MainMenu = Me
Me.Hide()
Me.AssetCheckForm.Show()
End Sub
End Class
Public Class AssetCheckForm
Friend MainMenu As MainMenu
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.MainMenu.Show()
Me.Close()
End Sub
End Class

How do i use an array to represent a form, to show and hide forms in VB

I want to use a array to help me switch between forms in a quiz that i have to create for a school assignment. When a question is correct it shows the correct form. I am using one button to go to the next form. There are 20 questions each with its own form. This is what i need:
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
Me.Hide()
arrayforms(count).Show()
Thanks
First Collect all of your questionary forms into Array. Name your questionary forms as "Questionary1", "Questionary2" something like that, or set tag to you forms. Then find your form by index and create an instance and ShowDialog, that's it. Try following code.
Public Class Form1
Private forms(20) As Type
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim yourFormIndex = 3
Dim frm As Form = Activator.CreateInstance(forms(yourFormIndex))
frm.ShowDialog()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim types As Type() = myAssembly.GetTypes()
Dim index As Integer = 1
For Each myType As Object In types
If myType.BaseType.FullName.ToString.ToUpper = "SYSTEM.WINDOWS.FORMS.FORM" Then
If (myType.Name.ToString.StartsWith("Questionary")) Then
forms(index) = myType
index = index + 1
End If
End If
Next
End Sub
End Class

vb.net textbox not displaing value

In my frmMain class I have a textbox(txtCustomer) which populates from a database. I want to pass this value to another textbox in frmDepartment(txtDeptCustomer).
I am failing to see the logic of why the code I am using is not displaying a value in txtDeptCustomer. I can query the database ok with the variable, so the string is being passed through, but just not displaying in txtDeptCustomer. I would be grateful if someone could point out my error. Thanks
frmDepartment
Dim customer As Object = frmMain.txtCustomer.Text
This is passing correct value to db.
sql = "SELECT * FROM Departments where Customer = '" & CType(customer, String) & "'"
textbox txtDeptCustomer <--- NOT DISPLAYING VALUE
Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
txtDeptCustomer.Text = CType(customer, String)
End Sub
Public Customer as String = Nothing
Private Sub btnDO_Click(sender As Object, e As EventArgs) Handles btnDoWork.Click
Customer = Database Call
Dim frmDepartmentInstance as new frmDepartment
frmDepartment.ShowDialog(Me)
End Sub
Then in the Load event of frmDepartment you can say
txtDeptCustomer.Text = frmMain.Customer
Proof of concept: New Project. Two forms | Form 1 has a button and a textbox | Form2 just has textbox
Public Class Form1
Public Test As String = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Test = TextBox1.Text
Dim frm2 As New Form2
frm2.ShowDialog(Me)
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = Form1.Test
End Sub
End Class
You must declare then as public your customer variable in frmDepartment, like:
Public customer as String
And in the button click from your frmMain you pass the value like:
frmDepartment.customer = txtCustomer.Text
frmDepartment.Show()
And then in Loading your frmDepartment you have now the option of assigning customer to txtDeptCustomer like:
Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtDepartment.Text = customer
End Sub