other type of checkedlistbox need answer - vb.net

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..

Related

How to display multiple results from a users' input from filters using MS Access and Visual Basic

I'm struggling to grasp VB as I'm new to programming. I am creating a program which displays a result (namely a link to a website) based on a users' filters. I've done this so far, which just selects the size filter. However, I don't know how to write the code that will check the database for the users' preference and output an appropriate link in the form of a label. Is it as simple as just printing DataTable or am I totally wrong? Any help would be greatly appreciated.
Here is my code so far:
Imports System.Data.OleDb
Public Class frmFilters
Dim provider As String
Dim dataFile As String
Dim ConnString As String
Dim myConnection As OleDbConnection = New OleDbConnection
Private ConStr As String = "My Connection String"
Private Sub buttonSearch_Click(sender As Object, e As EventArgs) Handles buttonSearch.Click
provider = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source="
dataFile = "H:\Visual studio files\Project\CMPC\partLists.accdb"
End Sub
Private Function GetPartData(filterMicroATX As Integer, filterATX As Integer) As DataTable
Dim dt As New DataTable
Dim sql = "SELECT * FROM partList WHERE
[size] > #MicroATX
And [size] < #ATX;"
Using con As New OleDbConnection(ConStr),
cmd As New OleDbCommand(sql, con)
With cmd.Parameters
.Add("#MicroATX", OleDbType.Integer).Value = filterMicroATX
.Add("#ATX", OleDbType.Integer).Value = filterATX
End With
con.Open()
dt.Load(cmd.ExecuteReader)
End Using
Return dt
End Function
Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
Me.Close()
End Sub
End Class

printing and saving current record

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

BC30469: Reference to a non-shared member requires an object reference

I am trying to write code that uses user input to query a data base and then display a GridView in VB 2015. I am getting a compilation error and cannot figure out what I an doing wrong.
Here is all my .VB coding. I have intentionally set variable to defined numbers for testing purposes.
Imports System.Data
Imports System.Data.SqlClient
Partial Class BeaversInc_GridTest
Inherits System.Web.UI.Page
Dim strConnectionString As String
Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim conn As New SqlConnection(strConnectionString)
Dim drDataReader As SqlDataReader
Dim dtDataTable As New DataTable
Dim cmd As New SqlCommand
Dim intStoreNumber As Integer
Dim dteStartDate As Date
Dim dteEndDate As Date
intStoreNumber = "686"
dteStartDate = CDate("11/15/2013")
dteEndDate = CDate("11/15/2015")
conn.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString
cmd.Connection = conn
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.CommandText = "CriticalFails"
With cmd.Parameters
.AddWithValue("#StoreNumber", intStoreNumber)
.AddWithValue("#StartDate", dteStartDate)
.AddWithValue("#Enddate", dteEndDate)
End With
conn.Open()
drDataReader = SqlCommand.ExecuteReader
dtDataTable.Load(drDataReader)
GridView1.DataSource = dtDataTable
GridView1.DataBind()
conn.Close()
End If
End Sub
End Class
The line "drDataReader = SqlCommand.ExecuteReader" was not supposed to be "drDataReader = cmd.ExecuteReader"??
I think that is your problem. =D

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

Code behind for print?

I have a report that I need to print after a sale has completed.
I am having issues with the length and printing when using PrintForm and PrintDocument components however if I right click and select Print from the list the report prints perfectly.
Is there any way that I could write code in my form load event that would imitate the right click print?
Ive been struggling with this part of my project for ages now and the whole thing is now way behind schedule.
My Code for the whole Receipt form page is:
Imports System.Data.OleDb
Imports System.IO
Imports System.Drawing.Printing
Public Class Receipt
Public PicLocation As String
Dim Ctrl1, Ctrl2 As Control
Dim CN As New OleDb.OleDbConnection
Dim CMD As New OleDb.OleDbCommand
Dim DataR As OleDb.OleDbDataReader
'To display into datagrid purpose
Dim dataS As New DataSet
Dim dataAd As New OleDb.OleDbDataAdapter
Public SqlStr, SqlStr1, DBPath, DBStatus, SearchBox As String
Dim X, Y, SqlH, Onh As Integer
Dim SqlUser As String
Dim DataP As Decimal
Dim Balance As Integer
Private Sub Receipt_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ProductListDataSet' table.
DBPath = (Application.StartupPath & "\ProductList.accdb")
If CN.State = ConnectionState.Open Then
CN.Close()
DBStatus = ("Not Connected")
End If
SqlStr = ("Provider = Microsoft.ACE.OLEDB.12.0;Data Source =" & DBPath)
CN.ConnectionString = SqlStr
CN.Open()
Onh = Nothing
lblTime.Text = DateAndTime.Now
lblUser.Text = Login.userTB.Text
Dim sql As String
sql = "SELECT * Receipt"
Me.ReceiptTableAdapter.ClearBeforeFill = True
Me.ReceiptTableAdapter.Connection = CN
Me.ReceiptTableAdapter.Connection.CreateCommand.CommandText = sql
Me.ReceiptTableAdapter.Fill(Me.ProductListDataSet.Receipt)
Me.ReportViewer1.RefreshReport()
Me.Refresh()
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
With Me.PrintForm1
.PrintAction = Printing.PrintAction.PrintToPrinter
Dim MyMargins As New Margins
With MyMargins
.Left = 0
.Right = 0
.Top = 0
.Bottom = 0
End With
.PrinterSettings.DefaultPageSettings.Margins = MyMargins
.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.CompatibleModeClientAreaOnly)
End With
End Sub
End Class