Reputation: 195
I'm trying to display a "No records found" message box, but I have no idea how to write the code. Here is my code:
Public Class Form1
Dim cnn As New OleDb.OleDbConnection
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
If Me.TextBox1.Text <> "" Then
cmd.CommandText = "INSERT INTO Student(StudentName, StudentID) " & _
" VALUES('" & Me.TextBox1.Text & "','" & Me.TextBox2.Text & "')"
cmd.ExecuteNonQuery()
MsgBox("Record added")
Else
MsgBox("Please fill in required fields")
End If
cnn.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\Testing.mdb"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Student WHERE StudentName='" & Me.TextBox1.Text & "'", cnn)
Dim dt As New DataTable
da.Fill(dt)
Me.TextBox3.Text = dt.Rows(0).Item("StudentName")
Me.TextBox4.Text = dt.Rows(0).Item("StudentID")
cnn.Close()
End Sub
End Class
Please advise how to write the If
Else
code, so that if there is no record, the "No record found" message will be shown. Thanks.
Upvotes: 0
Views: 5955
Reputation: 7689
If i've understand your question, you need to check if you have data in your dataset before displaying in you textbox (code in your Button1_Click event). You can try this;
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Student WHERE StudentName='" & Me.TextBox1.Text & "'", cnn)
Dim dt As New DataTable
da.Fill(dt)
// Assuming that at this stage, dt already contains the data
If dt.Rows.Count > 0 then
Me.TextBox3.Text = dt.Rows(0).Item("StudentName")
Me.TextBox4.Text = dt.Rows(0).Item("StudentID")
Else
MsgBox("No records found")
EndIf
cnn.Close()
End Sub
Upvotes: 3