Click here to Skip to main content
15,914,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Private Sub frmDamage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      'Set the focus of the search textbox
      txtSearch.Focus()

      'Me.Stock_InTableAdapter.Fill(Me.InventorySystemDataSet.Stock_In)

      'Search button is disable
      FillByStockNameToolStripButton.Enabled = False

      ToolStripbtnRefresh.Enabled = False

      'Dim conn As OleDbConnection
      Dim strSQL As String
      Dim dbConn As OleDbConnection
      dbConn = New OleDbConnection(cnSettings())

      'conn = New OleDbConnection(strConn)
      dbConn.Open()
      'conn.Open()
      strSQL = "SELECT * FROM Stock_In WHERE Condition LIKE 'Damage'"
      Stock_InDataGridView.DataSource = strSQL
      dbConn.Close()


  End Sub



Please correct my code I can't make that when I load the form I want only to show the damage word from the table stockin in my database.......
Posted

It is not good to do things like this in the form load event.

What is the exact problem you are seeing, that would help understand your issue.

It would be better to have a Public method on the form and call it once you load the form (if calling from another form), or have a button the user clicks to start the query once the form is loaded.

Change the SQL to
strSQL = "SELECT * FROM Stock_In WHERE Condition LIKE '%Damage%'" for an SQL database or '*Damage*' for an access database.
 
Share this answer
 
v2
i'm not big in vb, but the datasource should be some sort of collection not a string. therefore, you need to execute your strSQL and put it into a collection of some type... then you'll probably need to bind the collection to the control depending on what you want to do...

Protected Sub Page_Load(ByVal sender As Object, _
                        ByVal e As System.EventArgs) Handles Me.Load
 If Not Page.IsPostBack Then
   'Start by determining the connection string value
   Dim connString As String = _
       ConfigurationManager.ConnectionStrings(connStringName).ConnectionString
   'Create a SqlConnection instance
   Using myConnection As New SqlConnection(connString)
     'Specify the SQL query
     Const sql As String = "SELECT * FROM Customers"
     'Create a SqlCommand instance
     Dim myCommand As New SqlCommand(sql, myConnection)
     'Get back a DataSet
     Dim myDataSet As New DataSet
     'Create a SqlDataAdapter instance
     Dim myAdapter As New SqlDataAdapter(myCommand)
     myAdapter.Fill(myDataSet)
     'Bind the DataSet to the GridView
     gvCustomers.DataSource = myDataSet
     gvCustomers.DataBind()
     'Close the connection
     myConnection.Close()
   End Using
 End If
End Sub


i also agree with the other commenter about your SQL... LIKE 'Damage' means exactly like 'Damage' without wilcards (% for SQL, * for Access)
 
Share this answer
 
1. First I agree with the other people. I would't put this in form load.

2. Read up on using statement for oledbconnection.

3. Read up on using a data reader for reading the sql data

4. datasource needs to use data reader or you can use a dataadapter and fill a datatable

5. Your sql like needs a wildcard * such '*Damage*' or 'Damage*' (SQL use %)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900