My below codes in a button click event
populate data in the grid upon the user click the button.


1. How do I know that my commandtext did not find any matching data to popup the message to the user - No Records found.

2. How do I clear the data in the grid before executing the below codes.


--conection to SQL Server is opened

myadaptor.SelectCommand = New SqlCommand
myadaptor.SelectCommand.Connection = myconection
myadaptor.SelectCommand.CommandText = “--- select ..... where code = ....  ---”
myadaptor.SelectCommand.CommandType = CommandType.Text
myadaptor.Fill(mydataset, "salesdb")
grdlocation.DataSource = mydataset
grdlocation.DataMember = "salesdb"

1. Use the below code

--conection to SQL Server is opened
myadaptor.SelectCommand = New SqlCommand
myadaptor.SelectCommand.Connection = myconection
myadaptor.SelectCommand.CommandText = “--- select ..... where code = .... ---”
myadaptor.SelectCommand.CommandType = CommandType.Text
myadaptor.Fill(mydataset, "salesdb")
If mydataset.Tables(0).Rows.Count > 0 Then
grdlocation.DataSource = mydataset
grdlocation.DataMember = "salesdb"
Else
Label1.Text = "No Records Found"
End If

2. No need to clear the data. Just hide the grid if data not available using its visible property. If data available, just set the same propert to "true".

>2. How do I clear the data in the grid before executing the below codes.

Set Nothing (null)

grdlocation.DataSource=Nothing
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.