How to Load rdlc report runtime - vb.net

i created 2 reports billa4.rdlc and billa5.rdlc and i want to load one of the report to the reportviewer as per the selection of option of combobox by user .
please help me for this .
the below code is loading the billa4.rdlc
Try
Dim P2 As New ReportParameter("pbillnum", billnoprint)
Me.DataTable1TableAdapter.Fill(Me.billdata.DataTable1, billnoprint)
Me.ReportViewer1.LocalReport.SetParameters(New ReportParameter() {P2})
Me.ReportViewer1.SetDisplayMode(DisplayMode.PrintLayout)
Me.ReportViewer1.ZoomMode = ZoomMode.FullPage
Me.ReportViewer1.RefreshReport()
Catch ex As Exception
MsgBox("error")
End Try

What have you tried? Don't set up an report source directly from the design mode, do it by code.
Maybe something like this:
if( combooption.SelectedValue = 0) Then
Dim P2 As New ReportParameter("pbillnum", billnoprint)
Me.DataTable1TableAdapter.Fill(Me.billdata.DataTable1, billnoprint)
Me.ReportViewer1.ViewReport("billa4.rdlc")
Me.ReportViewer1.LocalReport.SetParameters(New ReportParameter() {P2})
Me.ReportViewer1.SetDisplayMode(DisplayMode.PrintLayout)
Me.ReportViewer1.ZoomMode = ZoomMode.FullPage
Me.ReportViewer1.RefreshReport()
else
Dim P2 As New ReportParameter("pbillnum", billnoprint)
Me.DataTable1TableAdapter.Fill(Me.billdata.DataTable1, billnoprint)
Me.ReportViewer1.ViewReport("billa5.rdlc")
Me.ReportViewer1.LocalReport.SetParameters(New ReportParameter() {P2})
Me.ReportViewer1.SetDisplayMode(DisplayMode.PrintLayout)
Me.ReportViewer1.ZoomMode = ZoomMode.FullPage
Me.ReportViewer1.RefreshReport()
End if

Related

Issue with binding and DBNull. Exception raised only once

I have a datatable with two columns C1 & C2. (C1 has AllowDBNull = false). The datatable is created as follows :
Private Function GetDataTable() As DataTable
Dim DT As New DataTable
'Create the first column
Dim C As New DataColumn("C1")
C.AllowDBNull = False
DT.Columns.Add(C)
'Second column
DT.Columns.Add(New DataColumn("C2"))
Return DT
End Function
Then I have a form with two text boxes bound to the datatable :
Dim DT As DataTable = GetDataTable()
Dim CurrencyManager As CurrencyManager = CType(Me.BindingContext(DT), CurrencyManager)
'Add the bindings
TextBox1.BindingContext = Me.BindingContext
TextBox2.BindingContext = Me.BindingContext
TextBox1.DataBindings.Add(New Binding("text", DT, "C1", True, DataSourceUpdateMode.OnValidation))
TextBox2.DataBindings.Add(New Binding("text", DT, "C2", True, DataSourceUpdateMode.OnValidation))
'Set the null value of the Textbox1
TextBox1.DataBindings(0).NullValue = ""
I'm setting the NullValue of the textBox1 such that whenever the textbox is "", it should be considered as DBNull.
I insert a new row using the CurrencyManager :
'Insert a new row
CurrencyManager.AddNew()
'Fill the two columns...
Dim Row As DataRowView = CurrencyManager.Current
Row.Row.Item(0) = "Column 1 Value"
Row.Row.Item(1) = "Column 2 Value"
'Validate the entry
CurrencyManager.EndCurrentEdit() 'No issue here since
Now when the user clears the FirstTextBox (which datatable column has AllowDBNull false) if I run the following code twice. The first time an exception is raised and the msgbox is displayed, however the second time it doesn't raise an exception and it takes back the previous value which is "Column 1 Value" and the column is not anymore dbnull.
Try
CurrencyManager.EndCurrentEdit()
Catch ex As Exception
msgbox("The field C1 can not be empty")
End Try
My question is : is there any way to make the last code always raise an exception when the field is empy ?
Cheers,
Assuming that I am understanding your goal correctly, then something like this should work.
Private Sub TextBox1_Validating(sender As Object, e As CancelEventArgs) Handles TextBox1.Validating
Try
CurrencyManager.EndCurrentEdit()
Catch ex As Exception
MsgBox("The field C1 can not be empty")
TextBox1.DataBindings(0).WriteValue() ' push the value to the datasource
e.Cancel = True
End Try
End Sub
Edit: I just want to state that this use of an exception for validation is not recommended as you could easily validate the text without using an exception. This also assumes that this could is placed in the validating event; an assumption on my part that my be incorrect.

VB.NET Concurrency Exception with DataGridView

I have encountered a with my datagridview, I bound it to a datatable and am calling and the handler on row is as follows
Dim cmdBuild As New SqlCommandBuilder(sda)
sda.Update(dgv.DataSource)
sda is SqlDataAdapter
so the problem is it works fine, any changes are updated to the database, but the concurrency exception pops up and I understand it is due to updating a ghost row in the database, now what I don't know is how do I go about it, how do I prevent it from happening. spent a lot of time on this one thing so any help that will sort me out will be HIGHLY appreciated. Thanks in advance.
P.S. If more code is needed I'll post it.
Edit
Public Sub editUsers(ByRef mainPanel As CustomTabControl, ByRef dt As System.Data.DataTable)
For i As Integer = 0 To mainPanel.TabCount - 1
If mainPanel.TabPages(i).Text = "Excel users" Then
mainPanel.SelectedTab = mainPanel.TabPages(i)
Exit Sub
End If
Next
Dim tb As New TabPage("Excel users")
Dim datagrid As New DataGridView
datagrid.AllowUserToAddRows = False
dt.Rows.Add(dt.NewRow)
datagrid.DataSource = dt
AddHandler datagrid.RowLeave, AddressOf Home.dataUpdate
tb.Controls.Add(datagrid)
datagrid.Dock = DockStyle.Fill
mainPanel.TabPages.Add(tb)
mainPanel.SelectedTab = tb
datagrid.Columns("ID").Visible = False
datagrid.Columns("Username").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
datagrid.Columns("Username").FillWeight = 45
datagrid.Columns("Password").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
datagrid.Columns("Password").FillWeight = 45
End Sub
Public Sub dataUpdate()
Dim dgv As DataGridView = Nothing
For i As Integer = 0 To mainPanel.TabCount - 1
If mainPanel.TabPages(i).Text = "Excel users" Then
dgv = mainPanel.TabPages(i).Controls(0)
End If
Next
If IsNothing(dgv) Then
Exit Sub
End If
For i As Integer = dgv.DataSource.Rows.Count - 1 To 0 Step -1
If dgv.DataSource.Rows(i).RowState = DataRowState.Deleted Then
Continue For
End If
If IsDBNull(dgv.DataSource.Rows(i).Item(1)) Or IsDBNull(dgv.DataSource.Rows(i).Item(2)) Then
Exit Sub
End If
Next
Dim cmdBuild As New SqlCommandBuilder(sda)
sda.Update(dgv.DataSource)
dgv.DataSource.Rows.Add(dtt.NewRow)
End Sub
The code above is all the code that is involved with datagridview control

Adding Group Item in QuickBooks using QBFC

Need a little help here.
I just want to add a Group Item with Item List on it using the QBFC12 but am having a trouble with it. I have tried creating the same method like this with Inventory Assembly and it works well. but this one makes me feel terrible. When the request is processed, it return error telling that there're missing fields on the request. Hope anyone can help me out with this.
Thanks
Here's my code below:
Dim msgSetRequest As IMsgSetRequest
Dim QBSM As New QBSessionManager
Try
With QBSM
.OpenConnection("", "QB Test")
.BeginSession("", ENOpenMode.omDontCare)
End With
Catch ex As Exception
Throw New Exception(ex.Message)
Return False
End Try
msgSetRequest = QBSM.CreateMsgSetRequest("US", 8, 0)
msgSetRequest.Attributes.OnError = ENRqOnError.roeStop
msgSetRequest.ClearRequests()
Dim gAdd As IItemGroupAdd = msgSetRequest.AppendItemGroupAddRq
gAdd.IsActive.SetValue(True)
gAdd.Name.SetValue("Group Name")
gAdd.ItemDesc.SetValue("Group Description")
For Each gListItem As clsInventoryGroupItem In gItem.InventoryGroupItemList
Dim gItemAdd As IItemGroupLine = msgSetRequest.AppendItemGroupAddRq.ItemGroupLineList.Append
gItemAdd.ItemRef.FullName.SetValue(gListItem.ItemRef)
gItemAdd.Quantity.SetValue(gListItem.Quantity)
Next
Dim response As IMsgSetResponse = QBSM.DoRequests(msgSetRequest)
If response.ResponseList.GetAt(0).StatusCode = 0 Then
MessageBox.Show("Success")
else
MessageBox.Show("An Error occurred while inserting Group")
endif
I think the problem is how you are adding your group lines, but haven't tested it yet. Instead of using ItemGroupLineList.Append from the msgSetRequest, you should call it from your IItemGroupAdd object, gAdd. Here's what I came up with, but didn't test it.
Dim msgSetRequest As IMsgSetRequest
Dim QBSM As New QBSessionManager
Try
With QBSM
.OpenConnection("", "QB Test")
.BeginSession("", ENOpenMode.omDontCare)
End With
Catch ex As Exception
Throw New Exception(ex.Message)
Return False
End Try
msgSetRequest = QBSM.CreateMsgSetRequest("US", 8, 0)
msgSetRequest.Attributes.OnError = ENRqOnError.roeStop
msgSetRequest.ClearRequests()
Dim gAdd As IItemGroupAdd = msgSetRequest.AppendItemGroupAddRq
gAdd.IsActive.SetValue(True)
gAdd.Name.SetValue("Group Name")
gAdd.ItemDesc.SetValue("Group Description")
For Each gListItem As clsInventoryGroupItem In gItem.InventoryGroupItemList
Dim gItemAdd As IItemGroupLine = gAdd.ItemGroupLineList.Append
gItemAdd.ItemRef.FullName.SetValue(gListItem.ItemRef)
gItemAdd.Quantity.SetValue(gListItem.Quantity)
Next
Dim response As IMsgSetResponse = QBSM.DoRequests(msgSetRequest)
If response.ResponseList.GetAt(0).StatusCode = 0 Then
MessageBox.Show("Success")
else
MessageBox.Show("An Error occurred while inserting Group")
endif

DataGridView not Refreshing/Updating/Reloading Data. After Child form closes

This is a VB.NET, Winforms App. I have a datagridview on "Form1" that uses a databinding.datasource which is an Entity Framework table. I fill the datagridview with the below function on Form1:
Sub PM_UnitViewGrid()
Try
_form1.UnitsBindingSource.DataSource = db.units.Where(Function(f) f.propertyId = _form1.CurrentPropertyId).OrderBy(Function(F) F.unitNumber)
_form1.UnitDataGridView.DataSource = _form1.UnitsBindingSource.DataSource
Dim iCount As Integer = _form1.UnitDataGridView.RowCount
For x As Integer = 0 To iCount - 1
If Not IsNothing(_form1.UnitDataGridView.Rows(x).Cells(4).Value) Then
Dim tid As Integer = _form1.UnitDataGridView.Rows(x).Cells(4).Value
Dim _ten As tenant = db.tenants.Single(Function(f) f.Occupantid = tid)
_form1.UnitDataGridView.Rows(x).Cells(1).Value = _ten.first_name + ", " + _ten.last_name
Else
Dim btnColumn As DataGridViewButtonCell = CType(_form1.UnitDataGridView.Rows(x).Cells(1), DataGridViewButtonCell)
btnColumn.Style.BackColor = Color.Green
_form1.UnitDataGridView.Rows(x).Cells(1).Value = "VACANT"
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return
End Sub
This works great and also assigns the needed values to an unbound column. The problem is that the cells(1) is a button. Which when clicked takes the user to another form as a new dialog window. The function for which is below. However, once the changes are made in that form I need for the datagridview to refresh the data that its using from the database and show the correct data. As it stands right now the values are not updating on the datagridview unless the app is completely exited and restarted. Nothing I have found seems to work and Refresh and Update only redraw the control. I need the underlying datasource to refresh and then the datagridview once the child form is exited.. This has had me stumped for a good 36 hours now and I am lost as to why nothing I am trying is working. ANY and all help would be greatly appreciated.
The sub that loads the child form based on the cells(1) button clicked is as follows:
Private Sub UnitDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles UnitDataGridView.CellContentClick
UnitDataGridView.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
Dim y As DataGridViewCellEventArgs = e
Dim Tid As Integer = Nothing
If e.ColumnIndex = 1 Then
If Not e.RowIndex = -1 Then
If Not IsNothing(UnitDataGridView.Rows(e.RowIndex).Cells(4).Value) Then
currentTenent = UnitDataGridView.Rows(e.RowIndex).Cells(4).Value
TenentIdentification = currentTenent
If Not IsNothing(e) Then
If Not IsNothing(UnitDataGridView.Rows(e.RowIndex).Cells(4).Value) Then
Tid = UnitDataGridView.Rows(e.RowIndex).Cells(4).Value
Dim _ten As tenant = db.tenants.Single(Function(f) f.Occupantid = Tid) 'tenant is a table entity
TenantViewSubs.tenId = _ten.Occupantid
Dim t As New TenantView
t.tenId = tid
t.ShowDialog()
End If
End If
PropertyManagSubs.PM_UnitViewGrid() 'This is the function that is above that fills the datagridview
Else
Dim uTview As New UnassignedTenants
uTview.selectedProperty = selectedProperty 'selectedProperty is Integer
uTview.ShowDialog()
PropertyManagSubs.PM_UnitViewGrid() 'This is the function that is above that fills the datagridview
End If
End If
End If
End Sub
I tried each of the following code blocks after the t.ShowDialog() line with no change at all.
UnitDataGridView.Refresh()
.
UnitsBindingSource.Dispose()
UnitsBindingSource.DataSource = db.units.Where(Function(f) f.propertyId = selectedProperty).OrderBy(Function(f) f.unitNumber)
UnitDataGridView.DataSource = UnitsBindingSource.DataSource
.
UnitsBindingSource.DataSource = nothing
unitsBindingSource.DataSource = db.units.Where(Function(f) f.propertyId = selectedProperty).OrderBy(Function(f) f.unitNumber)
UnitDataGridView.DataSource = UnitsBindingSource.DataSource
I finally fixed this on my own.. It was in the way I passed my db context to the databinding..
I simply wrote the below sub:
Private Sub UpdateValues()
Dim context As New storageEntities 'storageEntities is an Entity
Dim query = context.units.Where(Function(F) F.propertyId = selectedProperty).OrderBy(Function(f) f.unitNumber)
UnitDataGridView.DataSource = query
End Sub
Then anytime a child form updated data I simply call
UpdateValues()
After the dialog box closes.
This may help someone else with the same problems so that is why I am posting it.

To iterate through the values of combo box control using vb.net

I update my question here .. Am using a combo box with no of phone numbers .I want to get the phone no one by one in a variable. Now am using the below code to get the combobox values. But still now am getting the following error message System.Data.DataRowView. Please help me to fix this error. am new for vb.net.
My partial code is here ..
For i = 0 To ComboBox1.Items.Count
Dim s As String
s = Convert.ToString(ComboBox1.Items(i))
Next i
you are using an index which is zero based.
change this:
For i = 0 To ComboBox1.Items.Count
to this:
For i = 0 To ComboBox1.Items.Count - 1
This also works!
Dim stgTest = "Some Text"
Dim blnItemMatched As Boolean = False
'-- Loop through combobox list to see if the text matches
Dim i As Integer = 0
For i = 0 To Me.Items.Count - 1
If Me.GetItemText(Me.Items(i)) = stgTest Then
blnItemMatched = True
Exit For
End If
Next i
If blnItemMatched = False Then
Dim stgPrompt As String = "You entered '" & stgTypedValue & "', which is not in the list."
MessageBox.Show(stgPrompt, "Incorrect Entry", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Text = ""
Me.Focus()
End If
Your problem probably happens here:
s = Convert.ToString(ComboBox1.Items(i))
This doesn't return the value. It returns a string representation of the object at the given index, which in your case apparently is of type System.Data.DataRowView.
You would have to cast ComboBox1.Items(i) to the approbriate type and access its Value. Or, since its a DataRowView, you can access the values throgh the appropriate column names:
Dim row = CType(ComboBox1.Items(i), System.Data.DataRowView)
s = row.Item("column_name")
Nevertheless, first of all you should definitely close and dispose the connection, no matter whether the transaction fails or succeeds. This can be done in a finally block (option 1) or with a using statement (option 2).
Option 1
// ...
con1 = New MySqlConnection(str)
con1.Open()
Try
// ...
Catch ex As Exception
Lblmsg.Text = " Error in data insertion process....." + ex.Message
Finally
con1.Close()
con1.Dispose()
End Try
Option 2
// ...
Using con1 as New MySqlConnection(str)
con1.Open()
Try
// ...
Catch ex As Exception
Lblmsg.Text = " Error in data insertion process....." + ex.Message
Finally
con1.Close()
End Try
End using
Even after long time back you will achieve this with simply by following
For Each item As Object In combx.Items
readercollection.Add(item.ToString)
Next
Please try this
For j As Integer = 0 To CboCompany.Items.Count - 1
Dim obj As DataRowView = CboCompany.Items(j)
Dim xx = obj.Row(0)
If xx = "COMP01" Then
CboCompany.SelectedIndex = j
Exit For
End If
Next
I could not find this answer online in its entirety but pieced it together. In the snippet below cbox is a ComboBox control that has the DisplayMember and ValueMember properties initialized.
Dim itemIE As IEnumerator = cbox.Items.GetEnumerator
itemIE.Reset()
Dim thisItem As DataRowView
While itemIE.MoveNext()
thisItem = CType(itemIE.Current(), DataRowView)
Dim valueMember As Object = thisItem.Row.ItemArray(0)
Dim displayMember As Object = thisItem.Row.ItemArray(1)
' Insert code to process this element of the collection.
End While