Search to DataGridView ( showing record) selected record print vb.net - vb.net

This is my code but it prints all records. I want to search a record and print crystalreport.
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
Me.Patient_tblTableAdapter.Fill(Me.DoctorDataSet.patient_tbl)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PatienttblBindingSource.Filter = "(Convert(ID,'System.String') LIKE '" & TextBox1.Text & "%')"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rpt As New CrystalReport1
rpt.SetDataSource(DoctorDataSet)
Form2.CrystalReportViewer1.ReportSource = rpt
Form2.ShowDialog()
End Sub
End Class

try to use '*' instead '%' in like statement :
PatienttblBindingSource.Filter = "(Convert(ID,'System.String') LIKE '" & TextBox1.Text & "*')"

Related

Moving between DataGridView rows via code and show through textboxes

I'm not an experienced programmer and I must have made some misconceptions.
I have two forms, one for the search (FormSearch) and one to show the results (FormMain).
The question is: how can I populate the textboxes with the previous results (click on the btnPrev button) or the next results (click on the btnNext button)?
FormSearch
Imports System.Data.SqlClient
Public Class FormSearch
Private Sub btnsearch_Click(sender As Object, e As EventArgs) Handles btnsearch.Click
Dim con As New SqlConnection("Data Source=" & Form1.server & "," & Form1.port & "; Initial Catalog=" & Form1.database & "; User Id=" & Form1.txtUsername.Text & "; Password=" & Form1.txtPassword.Text & ";")
Dim cmd As New SqlCommand("select * FROM dbo.customers;", con)
Dim adapter As New SqlDataAdapter(cmd)
Dim table As New DataTable
adapter.Fill(table)
DataGridView1.DataSource = table
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
FormMain.TextBox1.Text = row.Cells("Name").Value.ToString
FormMain.TextBox2.Text = row.Cells("Surname").Value.ToString
FormMain.TextBox3.Text = row.Cells("Age").Value.ToString
Me.Hide()
FormMain.Show()
End If
End Sub
End Class
FormMain
Public Class FormMain
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
FormSearch.Show()
Me.Hide()
End Sub
Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
'Show prev result
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
'Show next result
End Sub
End Class
Try this:
1) Apply the code below in FormMain
Public Class FormMain
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
FormSearch.Show()
Me.Hide()
End Sub
Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
BS.Position = BS.Position - 1
DisplayRecords()
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
BS.Position = BS.Position + 1
DisplayRecords()
End Sub
End Class
2) Apply the code below in FormSearch
Imports System.Data.SqlClient
Public Class FormSearch
Private Sub btnsearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim con As New SqlConnection("Data Source=" & Form1.server & "," & Form1.port & "; Initial Catalog=" & Form1.database & "; User Id=" & Form1.txtUsername.Text & "; Password=" & Form1.txtPassword.Text & ";")
Dim adapter As New SqlDataAdapter("select * FROM [dbo.customers]", con)
adapter.Fill(DS, "myTableNane")
BS.DataSource = DS
BS.DataMember = "myTableNane"
DataGridView1.DataSource = BS
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.RowIndex >= 0 Then
BS.Position = e.RowIndex
Me.Hide()
FormMain.Show()
End If
End Sub
End Class
2) Create a Module and place the below code.
Module Module1
Public DS As New DataSet
Public WithEvents BS As New BindingSource
Sub DisplayRecords(Optional ByVal table As String = "myTableNane")
FormMain.TextBox1.Text = DS.Tables(table).Rows(BS.Position)("Name").ToString
FormMain.TextBox2.Text = DS.Tables(table).Rows(BS.Position)("Surename").ToString
FormMain.TextBox3.Text = DS.Tables(table).Rows(BS.Position)("Age").ToString
End Sub
End Module
Hope you find it helpful and solved your problem.
Moiyd

vb.net Find and REMOVE a line in a textbox

I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class

OleDb error: "ExecuteNonQuery() requires an open and available connection" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've been trying to connect Access and VB 2013, but it keeps giving me this error
ExecuteNonQuery() requires an open and Avaliable connection
Here is my code:
Imports System.Data.OleDb
Imports System.IO
Public Class Form3
Public sEditType As String = String.Empty
Dim cn As New OleDbConnection
Dim constring As String = "Provider=Microsoft.Jet.Oledb.12.0; Data Source=C:\Users\MarkTT\Documents\major.mdb; persist security info = false"
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.KeyPress
End Sub
Private Sub Label4_Click(sender As Object, e As EventArgs)
End Sub
Private Sub TextBox2_TextChanged_1(sender As Object, e As EventArgs)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
End Sub
Private Sub Label5_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cm As New OleDbCommand
Try
Dim sqlquery As String = "INSERT INTO tablea (Name,Cost,Supplier,Quantity) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')"
With cm
.CommandText = sqlquery
.Connection = cn
.ExecuteNonQuery()
End With
MsgBox("Added")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox3.Enabled = True
TextBox4.Enabled = True
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox3.Enabled = False
TextBox4.Enabled = False
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub TextBox2_TextChanged_2(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
End Sub
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
cn.ConnectionString = constring
cn.Open()
End Sub
End Class
Provider=Microsoft.Jet.Oledb.12.0
is not a valid OLEDB provider name. You either need to use
Provider=Microsoft.Jet.OLEDB.4.0
or
Provider=Microsoft.ACE.OLEDB.12.0

Throw New NotImplementedException - VB

I'm making an application that manages the inputs and outputs of a company's customers as well as the creation of the same, however when I "submit" the form of the creation of a new customer to me the following error:
"Throw New NotImplementedException"
I leave here my code, so they can help me.
FORM1.vb
Public Class Form1
Private Property cnt As Object
<SerializableAttribute> _
'<ComVisibleAttribute(True)> _
Public Class NotImplementedException _
'Inherits SystemException
End Class
Private Sub TabPage1_Click(sender As Object, e As EventArgs)
Dim cnt As New OleDb.OleDbConnection
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs)
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
TabControl1.SelectedTab = TabPage2
End Sub
Private Sub TabPage2_Click(sender As Object, e As EventArgs) Handles TabPage2.Click
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'cnt.ConnectionString = "Provinder=Microsoft.Jet.Oledb.40; Data Source=" & Application.StartupPath & "\bdtesteentradas.mdb"
'TODO: This line of code loads data into the 'BdtesteentradasDataSet.empresa' table. You can move, or remove it, as needed.
Me.EmpresaTableAdapter.Fill(Me.BdtesteentradasDataSet.empresa)
End Sub
Private Sub FillByToolStripButton_Click(sender As Object, e As EventArgs) Handles FillByToolStripButton.Click
Try
Me.EmpresaTableAdapter.FillBy(Me.BdtesteentradasDataSet.empresa)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Me.EmpresaBindingSource.Filter = "nif ='" & TextBox1.Text & "'"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox2.Text = ""
TextBox3.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox5.Text = ""
TextBox4.Text = ""
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Dim cnt As New OleDb.OleDbCommand
If Not ConnectionState.Open Then
End If
Cmd.Connection = cnt()
Cmd.CommandText = "INSERT INTO empresa (NIF, Empresa, nCliente, Distrito, NomeContacto, ApelidoContacto, Funcao" &
"VALUES(" & Me.TextBox6.Text & ")"
'Cmd.ExecuteNonQuery()
MsgBox("Dados inseridos.")
End Sub
Cmd.vb
Class Cmd
Shared Property CommandText As String
Shared Property Connection As Object
Shared Sub ExecuteNonQuery()
Throw New NotImplementedException
End Sub
End Class
Error message
Already a big thank you
You got error on your "cmd" variable and "fixed it" this way:
Didn't you? This generated the file Cmd.vb for you.
That is not what you want. Remove Cmd.vb from you project an try this:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'You want to check, what the State of *your* connection is
If Not cnt.State = ConnectionState.Open Then
End If
'Create a Command-Object from the Connection
Using cmd = cnt.CreateCommand()
cmd.CommandText = "INSERT INTO empresa (NIF, Empresa, nCliente, Distrito, NomeContacto, ApelidoContacto, Funcao" &
"VALUES(" & Me.TextBox6.Text & ")"
cmd.ExecuteNonQuery()
MsgBox("Dados inseridos.")
End Using
End Sub

vb.net error on something

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Shell("CMD.exe")
SendKeys.Send("net user")("TextBox1.Text")("TextBox2.Text")
SendKeys.Send("{ENTER}")
End Sub
End Class
It says:
Error 1 Expression is not an array or a method, and cannot have an
argument list.
for SendKeys.Send("net user")
Glad that you have resolved, but trust me, this is not the correct way to work with CMD.exe from inside a NET app.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pi = new ProcessStartInfo()
pi.FileName = "CMD.EXE"
pi.Arguments = "/K NET USER " + textBox1.Text + " " + TextBox2.Text
Process.Start(pi)
End Sub
ProcessStartInfo class
Process class