TextBox value from a form and display it to Crystal Reports using VB.NET - mysql

I created a parameter in Crystal Report named IID. But when the report loads it has nothing to display with.
This is my code :
Dim cryRpt As New rptPrntIss
cryRpt.Load("C:\Users\IEEC\Desktop\Sys\InventorySys\InventorySys\rptPrntIss.rpt")
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = frmInvntStocks.txtIID.Text
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("IID")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()

You are assigning the report object to viewer and refreshing at two places. Assign the report object (cryRpt) to viewer (CrystalReportViewer1) only in the last and remove refresh in the last too. Refresh is likely to drop all your modifications to the report object i.e. any filters, parameters etc. Modify your code as below:
cryRpt.Load("C:\Users\IEEC\Desktop\Sys\InventorySys\InventorySys\rptPrntIss.rpt")
'REMOVE REPORTSOURCE AND REFRESH STATEMENTS HERE
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
and
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
CrystalReportViewer1.ReportSource = cryRpt
'REMOVE REFRESH HERE

Report Part
Create A New Parameter 'TestParameter' on your report then put Your desire location.
VB.Net Part
now Click on Project Menu and go Your Project Propertise and click Setting then setup this format
Private Sub btnShowReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowReport.Click
Dim MyReport As CrystalDecisions.CrystalReports.Engine.ReportDocument = New DailyShadeCosting
MyReport.SetParameterValue("TestParameter", TextBox6.Text)
ShowReport(MyReport, filterstring, CrystalReportViewer1)
End Sub
Public Sub ShowReport(ByVal MyReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal filterstring As String, ByVal CrystalReportViewer As CrystalDecisions.Windows.Forms.CrystalReportViewer)
Dim myLogonInfo As New CrystalDecisions.Shared.TableLogOnInfo
Dim myTable As Table
For Each myTable In MyReport.Database.Tables
myLogonInfo = myTable.LogOnInfo
myLogonInfo.ConnectionInfo.ServerName = My.Settings.RptserverPath.ToString
myLogonInfo.ConnectionInfo.DatabaseName = My.Settings.Database.ToString
myLogonInfo.ConnectionInfo.UserID = My.Settings.DBUser.ToString
myLogonInfo.ConnectionInfo.Password = My.Settings.DBPass.ToString
myTable.ApplyLogOnInfo(myLogonInfo)
Next myTable
CrystalReportViewer.ReportSource = MyReport
CrystalReportViewer.SelectionFormula = filterstring
CrystalReportViewer.Refresh()
End Sub

Related

Refresh a combobox in a parent tabcontrol page after making an entry in child tabcontrol page

I am using MySQL Database which is connected to Visual Studio Community 2017 Community Edition software. I am using a TabControl form, with tables in each TabPage. When I add and save records in the first form (i.e. the "Books" form), I would like the Combobox in the second form (i.e. the "Authors" Form) to update after the new record is added in the first form.
The parent form has two textboxes: txtBookID and txtBookName. The textbook txtBookID is autoincremented. The child form has a ComboBox called CboBookID_fkey. If I make a new entry in the textbook txtBookName, I would like the CboBookID_fkey, to automatically update with the new SelectedItem in the ComboBox's drop-down list.
However, when I make an update in the TextBox on the parent TabPage, the CboBookID_fkey ComboBox drop-downlist does not automatically update with the changes. I tried to add a fill dataset in the INSERT event but it did not work. I created a ComboValues method and called it from the load event of my form, but there were no exceptions/errors. :My vb.net code is shown below: -
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Load
Dim MysqlConn As New MySqlConnection
MysqlConn.ConnectionString =
"server=localhost;Port=3306;database=mydatabase;userid=root;password=mypassword;persist security info=True"
daBooks = New MySqlDataAdapter("SELECT * From Books", MysqlConn)
Dim dtBooks As DataTable = New DataTable()
daBooks.MissingSchemaAction = MissingSchemaAction.AddWithKey
'daBooks.Fill(dsBooks, "Books")
daBooks.Fill(dtBooks)
dsBooks.Tables.Add(dtBooks)
cbBooks = New MySqlCommandBuilder(daBooks)
dtBooks.Columns("BookID").AutoIncrement = True
dtBooks.Columns(0).AutoIncrementStep = 1
'Bind the DataTable to the UI via a BindingSource.
BookBindingSource.DataSource = dtBooks
BookBindingNavigator.BindingSource = Me.BookBindingSource
txtBookID.DataBindings.Add("Text", BookBindingSource, "BookID")
txtBookCode.DataBindings.Add("Text", BookBindingSource, "BookCode")
txtBookName.DataBindings.Add("Text", BookBindingSource, "BookName")
Dim dtAuthors As New DataTable
daAuthors = New MySqlDataAdapter("SELECT * FROM Authors", MysqlConn)
daAuthors.MissingSchemaAction = MissingSchemaAction.AddWithKey
' Dim dsAuthors As New DataSet
dtAuthors = New DataTable("Authors")
daAuthors.Fill(dtAuthors)
dsAuthors.Tables.Add(dtAuthors)
daBooks = New MySqlDataAdapter("SELECT * FROM Books", MysqlConn)
dtBooks = New DataTable("Books")
daBooks.Fill(dtBooks)
dsAuthors.Tables.Add(dtBooks)
cbAuthors = New MySqlCommandBuilder(daBooks)
dtAuthors.Columns("AuthorID").AutoIncrement = True
dtBooks.Columns("BookID").AutoIncrement = True
dtAuthors.Columns(0).AutoIncrementSeed = dtBooks.Rows.Cast(Of DataRow).Max(Function(dr As DataRow) CInt(dr.Item(0))) + 1
dtBooks.Columns(0).AutoIncrementStep = 1
dtBooks.Columns(0).AutoIncrementSeed = dtBooks.Rows.Cast(Of DataRow).Max(Function(dr) CInt(dr.Item(0))) + 1
dtBooks.Columns(0).AutoIncrementStep = 1
dsAuthors.Relations.Add(New DataRelation("relation", dsAuthors.Tables("Books").Columns("BookID"), dsAuthors.Tables("Authors").Columns("BookID_fkey")))
BookBindingSource = New BindingSource(dsAuthors, "Books")
CboBookID_fkey.DisplayMember = "BookName"
CboBookID_fkey.ValueMember = "BookID"
CboBookID_fkey.DataSource = BookBindingSource
AuthorBindingSource = New BindingSource(BookBindingSource, "relation")
'bind the Books' foreign key to the combobox's "SelectedValue"
CboBookID_fkey.DataBindings.Add(New Binding("SelectedValue", AuthorBindingSource, "BookID_fkey", True))
'Bind the DataTable to the UI via a BindingSource.
AuthorBindingSource.DataSource = dtAuthors
AuthorBindingNavigator.BindingSource = Me.AuthorBindingSource
txtAuthorID.DataBindings.Add("Text", AuthorBindingSource, "AuthorID")
txtAuthorName.DataBindings.Add("Text", AuthorBindingSource, "AuthorName")
ComboValues()
End Sub
Private Sub BtnBookSave_Click(sender As Object, e As EventArgs) Handles BtnBookSave.Click
InsertBooks()
End Sub
Public Sub InsertBooks()
Dim sql As String = "INSERT INTO Books (`BookID`, `BookCode`,`BookName`) values (#Bookid,#Bookcode,#Bookname)"
con = New MySqlConnection("Server=localhost;Port=3306;Database=mydatabase;userid=root;password=mypassword;persist security info=True")
con.Open()
Dim cmd = New MySqlCommand(sql, con)
cmd.Parameters.AddWithValue("#Bookid", Me.txtBookID.Text)
cmd.Parameters.AddWithValue("#Bookcode", Me.txtBookCode.Text)
cmd.Parameters.AddWithValue("#Bookname", Me.txtBookName.Text)
cmd.ExecuteNonQuery()
con.Close()
End Sub
Private Sub ComboValues()
Dim mycon As New MySqlConnection("Server=localhost;Port=3306;Database=mydatabase;userid=root;password=mypassword;persist security info=True")
mycon.Open()
dtAuthors.Clear()
Dim daAuthors As New MySqlDataAdapter("select BookName,BookID from Books", mycon)
daAuthors.Fill(dtAuthors)
CboBookID_fkey.DataSource = dtAuthors
With CboBookID_fkey
.DisplayMember = "BookName"
.ValueMember = "BookID"
End With
End Sub

Accessing values from a loop in VB.Net from another function using button click event handler

I am trying to create a new POS system using VB.Net but am a beginner with it. I have a problem. Basically I use an online SQL database using which different categories of items for the POS can be added by the admin. Since all the items have been stored in an SQL table I have managed to retrieve that information based on categories and display them in the form of buttons on my POS system.
Now the problem is that the loop holds all the information related to the different buttons created and when I try to access the value of the current category being clicked on from another button it doesn't seem to give me the current value but instead gives me the last value which was updated in the loop. I'm leaving an example of the code below:
Imports MySql.Data.MySqlClient
Public Class Form13
Dim arr(100) As String
Public verifier(1) As Integer
Dim counter As Integer
Public value As String
Public Sub Form13_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim point As Integer
Dim point1 As Integer
point = 20
point1 = 100
counter = 0
Label2.Text = Form12.selected_value
Dim connection As New MySqlConnection
connection.ConnectionString = ("host=localhost;user=root;password=;database=pos;")
connection.Open()
Dim command As New MySqlCommand("select * from categories", connection)
Dim reader As MySqlDataReader
reader = command.ExecuteReader
While reader.Read()
Dim button As New Button
value = reader.GetString("category_name")
button.Name = value
button.Text = value
button.Height = 50
button.Width = 190
button.Font = New Font("arial", 11)
button.Location = New Point(point, point1)
Me.Controls.Add(button)
point1 = point1 + 50
counter = counter + 1
verifier(0) = counter
arr(counter) = value
AddHandler button.Click, AddressOf button_Click
button_Click()
End While
End Sub
Private Sub button_Click()
MsgBox(arr(counter))
End Sub
End Class
Shown above is the code where the category name is gathered from the MySQL table and later a single button is being reused until all the buttons have been created for all existing category names in the MySQL table. Now the outcome I would like is to have the name of the category when the relevant button is being clicked in the form of a MsgBox in the button handler. Any help would be very much appreciated.
The SQL table is being hosted using XAMPP and a screenshot of the category table used in the code above is shown below:
enter image description here
The problem is, that you have a the String value which is a referenced object.
Your loop adds the same reference to the string with every iteration of your loop.
The reference tells your programm where to look for the value in you RAM. The value for the same object gets updated every iteration so every entry has the same data.
You can fix this by declaring the Public value as String within the loop. That way every time you create a new reference.
Remove this line:
Public value As String
Change this line:
value = reader.GetString("category_name")
to:
Dim value as String = reader.GetString("category_name")
I used SQL Server because that is where I had a sample database. Just use the sender's Name or Text to find what category button was clicked.
Imports System.Data.SqlClient
Public Class Form3
Public Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim value As String
Dim point As Integer
Dim point1 As Integer
point = 20
point1 = 100
Label2.Text = Form12.selected_value
Using connection As New SqlConnection
connection.ConnectionString = My.Settings.BuildersConnectio
Dim command As New SqlCommand("select * from Builders", connection)
Dim reader As SqlDataReader
connection.Open()
reader = command.ExecuteReader
While reader.Read()
Dim button As New Button
value = reader.GetString(1)
button.Name = value
button.Text = value
button.Height = 50
button.Width = 190
button.Font = New Font("arial", 11)
button.Location = New Point(point, point1)
Me.Controls.Add(button)
point1 = point1 + 50
AddHandler button.Click, AddressOf button_Click
End While
End Using
End Sub
Private Sub button_Click(sender As Object, e As EventArgs)
Dim btn As Button = CType(sender, Button)
Dim ProductCategory = btn.Name
MessageBox.Show(ProductCategory)
End Sub
End Class

VB.Net from table(time data type) to label

I have a problem regarding producing report but before that I need to run a code in order for me to create multiple conditions for the next report.
I have here a screenshot of my tbldtr. What I want is the value of am_time_in which has the data type of time will be transferred into a label/textbox/variable. I am using Visual Basic with MySQL.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
checktime()
End Sub
Private Sub checktime()
Dim cn As New MySqlConnection(CnPath)
Dim Sql As String = "SELECT `tbldtr`.`am_time_in` FROM `tbldtr` WHERE `tbldtr`.`id` = '11' AND `tbldtr`.`dtrdate` = '2017-10-16'"
Dim daCmd5 As New MySqlCommand(Sql, cn)
cn.Open()
Dim datinfo As MySqlDataReader = daCmd5.ExecuteReader()
While datinfo.Read()
If IsDBNull(datinfo(0)) = True Then
lblamtimein.Text = ""
Else
lblamtimein.Text = datinfo(0)
End If
End While
cn.Close()
End Sub
End Class
Error here:
The error is occurring on this line -
lblamtimein.Text = datinfo(0)
A timespan needs to be explicitly converted to a string like this -
lblamtimein.Text = datinfo(0).ToString

Passing (form1) Datagridview "ID" cell content to (form2) in textbox with the click of button

I have a question regarding to my problem on how to pass the value from datagridview or selected ID by the user then with a click of the button the Selected ID in Datagridview should pass into the other form in textbox and show. This is what I want to happen. Because I want to use the ID from form 1 to form 2 for my child database.
Actually my content in datagridview is from mysql as my database and load data automatically when it runs. If anyone could help me then
Public Class frmCurriculumCourses
Dim con As New MySqlConnection
Dim cmd As New MySqlCommand
Dim dt As New DataTable
Dim da As New MySqlDataAdapter
Dim ds As New DataSet
Dim ConnectionString As String = "server=127.0.0.1;user id=root;database=dbgradeinquiry;password=12345;"
Sub LoadData()
con.ConnectionString = ConnectionString
con.Open()
DataGridView1.AutoGenerateColumns = False
With cmd
.Connection = con
.CommandText = "SELECT * from tblcurriculumcourses"
End With
dt = New DataTable
da.SelectCommand = cmd
da.Fill(dt)
DataGridView1.DataSource = dt
con.Dispose()
con.Close()
da.Dispose()
End Sub
Private Sub frmCurriculum_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadData()
End Sub
Private Sub btnViewCourses_Click(sender As Object, e As EventArgs) Handles btnViewCourses.Click
'This is the button that im going to use for passing the selected ID from datagridview(form1) to textbox(form2)
End Sub
End Class
If your going to pass the column 1 values of the Selected Row in Datagridview, then use following code,
Private Sub btnViewCourses_Click(sender As Object, e As EventArgs) Handles btnViewCourses.Click
Form2.show()
Form2.TextBox1.Text=DataGridView1.SelectedRows(0).Cells(0).Value.ToString
End Sub
or use this following command,
DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value.ToString
Create a module and a global variable:
Public Module GlobalVariables
Public cellID As String
End Module
Add this code to the btnViewCourses button (form1):
Form2.TextBox1.Text = cellID
Add this piece of code to your Form1 class:
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
cellID = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
End Sub
or click on datagridview > events > CellClick and add (same as above):
cellID = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
This code will pass a SELECTED SINGLE value.
How to use: Show form2 (don't forget), select a cell in datagridview with elft click, press the button.
Make sure you have a textbox named textbox2 in your form2. If you want to change the name do it and change the name in Form2.TextBox1.Text = cellID
If you want to pass the whole column (to a multiline textbox or a new datagridview) then store values one by one into the public variable or make your public variable an array. I would go with one by one.
Change your public variable from String to Integer if you are using integer numbers for IDs (you should).

Property 'ReportSource' is 'ReadOnly'

I'm currently making a report in VB.net and I've got this Error:
Property 'ReportSource' is 'ReadOnly'.
and this is my code:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class MRTable
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Dim cryRpt As New ReportDocument
cryRpt.Load("D:\Orly\workspace\HMIS\01.26.15\HoMISv2.0\CRMedicalRecords.rpt")
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = Nurse.SelectedPatient
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("PCode")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
CRMedicalRecords.ReportSource = cryRpt
End Sub
End Class
I hope that you could help me with this.
Please open the report in Crystal Reports, or Visual Studio and check under
File > Report Options
The option Read-Only to be turned off.
Probably this is preventing you from editing the report, as you are doing with adding the parameter.