HOME > .NET Framework > Forum > ทำไมทำการ ค้นหาไม่ได้อะครับ ช่วยดู code หน่อยครับ VB ครับ error :: object reference not sett to an instance of an object
ทำไมทำการ ค้นหาไม่ได้อะครับ ช่วยดู code หน่อยครับ VB ครับ error :: object reference not sett to an instance of an object
Option Explicit On
Option Strict On
Imports System.IO
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing.Drawing2D
Public Class Fitemdetail
Dim Conn As SqlConnection
Dim com As SqlCommand
Dim dr As SqlDataReader
Dim dtCustomer As DataTable
Dim bs As BindingSource = New BindingSource()
Dim sb As StringBuilder
Dim Today As DateTime
Private Sub Fitem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GroupBox1.BackColor = Color.FromArgb(0, 0, 0, 0)
GroupBox2.BackColor = Color.FromArgb(0, 0, 0, 0)
Dim strConn As String
strConn = DBConnString.strConn
Conn = New SqlConnection()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
tsSearch.Focus()
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim lgb As New LinearGradientBrush(ClientRectangle, Color.FloralWhite, Color.Moccasin, LinearGradientMode.Vertical)
e.Graphics.FillRectangle(lgb, ClientRectangle)
lgb.Dispose()
End Sub
Private Sub bnMoveFirst_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles bnMoveFirst.MouseUp
ShowPosition()
End Sub
Private Sub bnMovePrevious_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles bnMovePrevious.MouseUp
ShowPosition()
End Sub
Private Sub bnMoveNext_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles bnMoveNext.MouseUp
ShowPosition()
End Sub
Private Sub bnMoveLast_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles bnMoveLast.MouseUp
ShowPosition()
End Sub
Private Sub ShowPosition()
Dim CurrentRecord As Integer
If bs.Count > 0 Then
CurrentRecord = bs.Position + 1
bnRecord.Text = CStr(CurrentRecord)
Else
CurrentRecord = 0
bnRecord.Text = "0"
bnCount.Text = "0"
End If
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Dim bs = New BindingSource()
If tsSearch.Text.Trim() = "" Then Exit Sub
sb.Append("SELECT * FROM Item")
sb.Append(" WHERE item_code=@item_code")
Dim sqlSearch As String
sqlSearch = sb.ToString()
dtCustomer = New DataTable()
With com
.Parameters.Clear()
.Parameters.Add("@item_code", SqlDbType.NVarChar).Value = tsSearch.Text.Trim()
.CommandText = sqlSearch
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader()
If dr.HasRows Then
dtCustomer.Load(dr)
bs.DataSource = dtCustomer
item_code.DataBindings.Add("Text", bs, "item_code")
'txtFullName.DataBindings.Add("Text", bs, "FullName")
'mskIDCard.DataBindings.Add("Text", bs, "IDCard")
'txtAddress.DataBindings.Add("Text", bs, "Address")
'txtTelephone.DataBindings.Add("Text", bs, "Telephone")
Else
bnRecord.Text = "0"
bnCount.Text = "0"
MessageBox.Show("เงื่อนไขที่คุณป้อน ไม่มี !!!", "ผลการค้นหา", MessageBoxButtons.OK, MessageBoxIcon.Information)
tsSearch.Focus()
tsSearch.SelectAll()
End If
End With
dr.Close()
End Sub
End Class
Tag : .NET, Ms SQL Server 2005, VS 2003 (.NET 1.1), VS 2005 (.NET 2.x), VS 2008 (.NET 3.x), VS 2010 (.NET 4.x)