Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 690
  • Last Modified:

Message box for empty DataGridView vb.net 2005

My DataGridView is populated when a search for customer records is done. If I can't find any customer records with a last name starting with "gra" then the DataGridView doesn't get populated with records. What I want to do is create a message if the DataGridView is Empty. What would the code be if the DataGridView is Empty?


Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
 
        'DATAGRID MULTIPLE RESULTS LIST
        Dim MySQLConnection As String = My.Settings.dbConnection
        Dim dbConnection As New SqlConnection
        dbConnection.ConnectionString = MySQLConnection
 
        Dim bsPatients As BindingSource
        bsPatients = New BindingSource
        dbConnection.Open()
 
        Dim noRecord As Integer
        noRecord = 0
 
        Dim strSQL As String = "SELECT FirstName, LastName, DOB, CustomerID FROM Customer WHERE LastName LIKE '" & LastName.Text & "%' ORDER BY FirstName"
 
        Dim myDA As SqlDataAdapter = New SqlDataAdapter(strSQL, dbConnection)
 
        Dim myPatient As DataTable = New DataTable
 
        myDA.Fill(myPatient)
 
 
        bsPatients.DataSource = myPatient
 
        DataGridView1.DataSource = bsPatients
 
        dbConnection.Close()
 
        dbConnection = Nothing
 
 
    End Sub

Open in new window

0
rfgraham
Asked:
rfgraham
  • 2
1 Solution
 
JackOfPHCommented:
Put this code after the myDA.Fill code...

        If myPatient.Rows.Count = 0 Then
            MsgBox("No serach found")
        End If
0
 
lakshmidurgaCommented:
Add a bit of logic when biding the data to datagrid

myDA.Fill(myPatient)
 
 if mypatient.rows.count > 0 then
        bsPatients.DataSource = myPatient
 
        DataGridView1.DataSource = bsPatients
else
If txtCardNumber.Text & "" Then
    
    Dim strScript As String = "<script language=javascript>"
    
    strScript += "alert('please enter Credit Card Number');"
    
    strScript += "</script>"
    
    If (Not ClientScript.IsStartupScriptRegistered("clientScript")) Then
        
        ClientScript.RegisterStartupScript(Me.[GetType](), "clientScript", strScript, False)
        
    End If
    
   DataGridView1.visible=false
    
    
End If
 
 
        dbConnection.Close()

Open in new window

0
 
lakshmidurgaCommented:
Hi,

Please ignore

 If txtCardNumber.Text & "" Then in the above code.Just use the javascript code to give a alert message.

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now