printing and saving current record - vb.net

hello everyone i have Form1 and Form2 for crystal report viewer , the Form1 as main data entry and main page has 10 fields of TextBox and 1 datagridview
i want to combine save then print directly THE selected current Records but i failed i tried many ways and i changed and edited codes but no luck.. the only things i got that i was able to print in crystal reports viewer but for the whole thing not the current selected record my code of printing is
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Data
Imports MySql.Data.MySqlClient
Public Class Form1
Dim conn As New MySqlConnection("server=localhost;userid=sh;password=***;database=saj;sslMode=none")
Dim cmd As New MySqlCommand("", conn)
Dim dt As New DataTable
Dim da As New MySqlDataAdapter
Dim bs As New BindingSource
Dim sex As String
Dim ds As DataSet
Dim myReport As New ReportDocument
Private Sub pr_Click(sender As Object, e As EventArgs) Handles pr.Click
Dim rep As New CrystalReport1
rep.SetDataSource(dt)
Form2.CrystalReportViewer1.ReportSource = Application.StartupPath + "\CrystalReport1.rpt"
Form2.CrystalReportViewer1.Refresh()
Form2.CrystalReportViewer1.RefreshReport()
Form2.Show()
i also tried to fillert datagridview as the code below
If Not String.IsNullOrWhiteSpace(r.Text) Then
bs.Filter = String.Format("r Like '%{0}%'", r.Text)
Else
Dim rep As New CrystalReport1
Dim DataViewFilter As New DataView(dt)
DataGridView1.DataSource = DataViewFilter
DataGridView1.DataSource = DataViewFilter
bs.Filter = String.Format("r Like '%{0}%'", r.Text)
rep.SetDataSource(dt)
Form2.CrystalReportViewer1.ReportSource = Application.StartupPath + "\CrystalReport1.rpt"
Form2.CrystalReportViewer1.Refresh()
Form2.CrystalReportViewer1.RefreshReport()
End If
Form2.Show()
but did not work :( .. any help please

Related

other type of checkedlistbox need answer

i'm a new guy to learn vb...in my question to save checkedlistbox to database.
my question is:
LocalDatabase table 1: Employee
[EMPLOYEE]
Form1
LocalDatabase table 2 : record
checkedlistbox list form Employee datatable..then i want save the value to database where checkedlistbox selected...i don't know how to coding vb...can somebody help??
i have tried in code by learn in other..but still have in question in checklistbox saving.
my code
vb class
Imports System.Data.SqlClient
Public Class ConnectionDatabase
Public dset As New DataSet
Public adaptor As New SqlDataAdapter
Public conn As New SqlConnection
Public cmd As New SqlCommand
Public locate As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\asus\Documents\vs twins\TT - Copy\TT\twin.mdf';Integrated Security=True"
End class
so in vb coding is:
Imports System.Data
Imports System.Data.SqlClient
Public Class Record
Dim vs As New ConnectionDatabase
Dim tblDIV As DataTable
Dim daDIV As SqlDataAdapter
Dim dsDIV As New DataSet
Dim oCon As SqlConnection
Private Sub Record_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim oCon = New SqlConnection
oCon.ConnectionString = vs.locate
dsDIV = New DataSet
daDIV = New SqlDataAdapter("SELECT Name FROM Employee", oCon)
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(daDIV)
builder.QuotePrefix = "["
builder.QuoteSuffix = "]"
Try
daDIV.FillSchema(dsDIV, SchemaType.Source, "DIV")
daDIV.Fill(dsDIV, "DIV")
tblDIV = dsDIV.Tables("DIV")
CheckedListBox1.DataSource = tblDIV
CheckedListBox1.DisplayMember = "Name"
Catch ex As Exception
MsgBox("Encountered an Error;" & vbNewLine & ex.Message)
oCon.Close()
End Try
End Sub
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
Dim TextBox1 As Double
Dim TextBox2 As Double
Dim Ans As Double
WkH = Val(TextBox1.Text)
WkS = Val(TextBox2.Text)
Ans = WkH * WkS
Label4.Text = Ans
End Sub
that is all can be run...but i can't code in the last part (saving into database) can suggest some link or example to learn ??Please Help Thank You..

Pass DataTable to ReportViewer

I am trying to pass a datatable to a reportviewer which I fill by code, is there a way to do that? I tried this but nothing happened:
Dim bs As BindingSource
bs = New BindingSource()
bs.DataSource = DataTablefillbycode
Dim rs As ReportDataSource
rs = New ReportDataSource()
rs.Name = "Tabletest"
rs.Value = bs
form2.ReportViewer1.RefreshReport()
form2.ReportViewer1.Reset()
form2.ReportViewer1.LocalReport.ReportEmbeddedResource = "Test.Report1.rdlc"
form2.ReportViewer1.LocalReport.DataSources.Clear()
form2.ReportViewer1.LocalReport.DataSources.Add(rs)
form2.ReportViewer1.RefreshReport()
form2.ShowDialog()
PS : The GridView works fine with the table "Tablefillbycode"
Follow these steps to be able to pass data table to your report:
I suppose you created a Report1.rdlc in root of your project Test, so the name of its embedded resource would be Test.Report1.rdlc. Also I suppose the name of DataSet in your Report1 is DataSet1.
Put a report viewer on your Form2 and set its Dock property to Fill and set its Modifier property to Public.
In Form1 I suppose you have a DataGridView1 that you want to fill it in the Form_Load and you will use the same query that you used for creating report.
In Form1 I suppose you have a Button1 that you want to show Form2 when you click on Button1 and you want pass the data of DataGridView1 to it.
Don't forget to Imports Microsoft.Reporting.WinForms in Form1
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cn = "data source=(localdb)\v11.0;initial catalog=TestDB;integrated security=True;"
Dim cmd = "SELECT Id,Name FROM Category"
Dim adapter = New SqlDataAdapter(cmd, cn)
Dim table = New DataTable()
adapter.Fill(table)
Me.DataGridView1.DataSource = table
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form2 = New Form2()
Dim rds= New ReportDataSource("DataSet1", Me.DataGridView1.DataSource)
form2.ReportViewer1.LocalReport.DataSources.Clear()
form2.ReportViewer1.LocalReport.DataSources.Add(rds)
form2.ReportViewer1.LocalReport.ReportEmbeddedResource = "Test.Report1.rdlc"
form2.ShowDialog()
End Sub
Screenshot:

Filtering Data within a DataGridView in VB.NET

How would I add a filtration function to my program so when I begin typing into a text box, it filters the results in the DataGridView? Just to clarify, this code loads a linked Access database into the DataGridView on form load and allows me to add, remove and update the data contained with in.
I'm relatively new to Visual Basic and I have ran out of ideas in regards to what to in order to get it to work. Thank you in advance for any help provided!
Imports System.Data.OleDb
Public Class frmDatabase
Dim con As New OleDbConnection
Dim ds As New DataSet
Dim dt As New DataTable
Dim da As New OleDbDataAdapter
Private Sub frmDatabase_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Joe\Documents\Visual Studio 2012\Projects\school database viewer\school database viewer\dbSchoolDatabase.mdb"
con.Open()
ds.Tables.Add(dt)
da = New OleDbDataAdapter("Select * from tableStudentDetails", con)
Dim cb = New OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
da.Fill(dt)
dgvStudentDetails.DataSource = dt.DefaultView
con.Close()
End Sub
Private Sub cmdSave_Click_1(sender As Object, e As EventArgs) Handles cmdSave.Click
da.Update(dt)
End Sub

Issue with Comobox on DataGridView

I am using VB.NET to pull some data from an SQL database into a Datagridview.
I then want the user to be able to modify the information and save it back to the database, which I have working fine.
What I need to be able to do now is to restrict the values, but way of a combobox for the field Tarrif.
I have configured a DataSource called Tarrif1 and I am using the below code.
I have a couple of issues/questions.
Firstly the dropdown shows a single value of "System.Data.DataViewManagerListItemTypeDescriptor" not the Tarrif1 values.
Secondly, I now have 2 columns on my datatable called Tarrif, the original database one and the one I have added - How can I get the ComboBox to right back to the appropriate Tarrif database field.
Here is my code:
Imports System.Data.SqlClient
Imports System.Data.Common
Public Class ViewCustomersForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=barry-laptop\SQLEXPRESS; database=BillingReferenceData; integrated security=yes"
Dim sql As String = "SELECT * FROM Customers"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim comm As SqlCommand = New SqlCommand(Sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
Private Sub ViewCustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'---open the connection and fill the dataset---
conn.Open()
dataadapter.Fill(ds, "Customers_table")
conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers_table"
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Tarrifs"
'---data bind it---
comboBoxCol.DataSource = Tarrifs1
'comboBoxCol.DisplayMember = "Tarrif" // when I add these rows the new Tarrif column is not visible
'comboBoxCol.ValueMember = "Tarrif" // when I add these rows the new Tarrif column is not visible
'---add a combobox column to the DataGridView control---
DataGridView1.Columns.Add(comboBoxCol)
End Sub
Private Sub Button_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim sqlCmdBuilder As New SqlCommandBuilder(dataadapter)
sqlCmdBuilder.GetUpdateCommand()
dataadapter.Update(ds.Tables("Customers_table"))
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
End Class
Hope that makes sense. Any help greatly appreciated.
A few things in this code:
dataadapter.Fill(ds, "Customers_table")
conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers_table"
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Tarrifs"
'---data bind it---
comboBoxCol.DataSource = Tarrifs1
First, I'm not sure what happens whenever the code attempts to fill a DataSet that already has data in it. In my code, I never assume, so I always reinitialize it each time unless I need it:
ds = new DataSet()
Next, you are setting the DataSource for comboBoxCol as Tarrifs1, but I do not see that defined anywhere. You might want to look into that.
Finally, about the SQL: Dim sql As String = "SELECT * FROM Customers" I don't know what columns are in your Customers table. Is Tarrif an actual column?
I figured half my issue out.
I configured a second data connection to pull the Tarrif drop down from the Tarrifs table and display this as part of the customer data table.
My outstanding issues now, is that I need to write the select Tarrif value back to the Customers table.
All the other updated values save, just not sure how to write the Tarrif dropdown back to my SQL table
Here is my latest code.
Imports System.Data.SqlClient
Imports System.Data.Common
Public Class ViewCustomersForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=barry-laptop\SQLEXPRESS; database=BillingReferenceData; integrated security=yes"
Dim sql As String = "SELECT [Customer ID] ,[Customer Name] ,[Address] ,[City] ,[County] ,[Post Code] FROM [BillingReferenceData].[dbo].[Customers]"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim comm As SqlCommand = New SqlCommand(Sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
Dim con As New System.Data.SqlClient.SqlConnection("server=barry-laptop\SQLEXPRESS; database=Test; integrated security=yes")
Dim strSQL As String = "SELECT * FROM Tarrifs"
Dim da As New System.Data.SqlClient.SqlDataAdapter(strSQL, con)
Private Sub ViewCustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'---open the connection and fill the dataset---
conn.Open()
dataadapter.Fill(ds, "Customers_table")
conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers_table"
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Tarrifs"
'---data bind it---
da.Fill(ds, "Tarrifs")
comboBoxCol.DataSource = ds.Tables("Tarrifs")
comboBoxCol.DisplayMember = "Tarrif"
comboBoxCol.ValueMember = "Tarrif"
'---add a combobox column to the DataGridView control---
DataGridView1.Columns.Add(comboBoxCol)
End Sub
Private Sub Button_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim sqlCmdBuilder As New SqlCommandBuilder(dataadapter)
sqlCmdBuilder.GetUpdateCommand()
dataadapter.Update(ds.Tables("Customers_table"))
dataadapter.Update(ds.Tables("Tarrifs"))
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
End Class

vb database connection gives no information as to whether connection was successful

I am trying to connect to an access database THROUGH CODE. I capitalized that last bit because I am well aware of the visual method of doing it but can't use that method in this instance. Basically I just want to create a databinding to a combobox on a single column from a table Title that displays the column Year. To clear that up I want to populate cboYearsFillBy with the data from the column YearPublished. The thing I am finding to be the biggest pain is that vb doesn't throw any sort of fuss when the db isn't found so I don't even know if it is finding it. My code is below
Imports System.Data.OleDb
Public Class frmTitle
' Create untyped dataTables
Dim dtYears As New DataTable
' Declare a variable to represent the DataAdapter
Dim daYears As OleDbDataAdapter
' Create BindingSource objects for Employee & Dept tables:
Dim WithEvents bsYears As New BindingSource
Private Sub frmTitle_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim connStr As String = "Provider=Microsoft.ace.OLEDB.12.0;Data Source=D:\VisualStudioProjects\Biblio.accdb"
MessageBox.Show(connStr)
Dim titleSQLStr As String = "SELECT * FROM Title"
daYears = New OleDbDataAdapter(titleSQLStr, connStr)
Dim commandBuilder As New OleDb.OleDbCommandBuilder(daYears)
daYears.Fill(dtYears)
dtYears.PrimaryKey = New DataColumn() {dtYears.Columns("YearPublished")}
bsYears.DataSource = dtYears
cboYearsFillBy.DataSource = bsYears
cboYearsFillBy.DisplayMember = "YearPublished"
cboYearsFillBy.ValueMember = "YearPublished"
End Sub
End Class
I get the following output
Imports System.Data.OleDb
Public Class frmTitle
' Create untyped dataTables
Dim dtYears As New DataTable
' Declare a variable to represent the DataAdapter
Dim daYears As OleDbDataAdapter
' Create BindingSource objects for Employee & Dept tables:
Dim WithEvents bsYears As New BindingSource
Private Sub frmTitle_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim connStr As String = "Provider=Microsoft.ace.OLEDB.12.0;Data Source=D:\VisualStudioProjects\Biblio.accdb; Persist Security Info=True"
MessageBox.Show(connStr)
Dim con As New OleDbConnection
con = New OleDbConnection(connStr)
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim titleSQLStr As String = "SELECT * FROM Title"
daYears = New OleDbDataAdapter(titleSQLStr, connStr)
daYears.Fill(dtYears)
cboYearsFillBy.DataSource = dtYears
cboYearsFillBy.DisplayMember = "YearPublished"
cboYearsFillBy.ValueMember = "YearPublished"
con.Close()
End Sub
End Class
I simplified your code just try now