November 24, 2015 at 12:44 am
Hello . . .
i use this code for searching in sql database what i want is where should i put the message "No Record Found "
If TextBox1.Text = "" Then
MsgBox("Please Enter License Number ", MsgBoxStyle.Critical, "Error")
Exit Sub
End If
Dim sqlStr As String = "SELECT * FROM MonitoringReports WHERE EmployeeID = '" & TextBox9.Text & "' and LicensesNumber='" & TextBox1.Text & "'"
Dim dataAdapter As New SqlDataAdapter(sqlStr, con)
dataAdapter.Fill(dt)
If dt.Rows.Count < 0 Then
MsgBox("No Record Found", MsgBoxStyle.Critical, "Error")
Else
For i As Integer = 0 To (dt.Rows.Count - 1)
rowIndex = i
TextBox8.Text = CStr(dt.Rows(rowIndex)("EmployeeName"))
TextBox9.Text = CStr(dt.Rows(rowIndex)("EmployeeID"))
DateTimePicker2.Value = CStr(dt.Rows(rowIndex)("DateOfEntry"))
TextBox1.Text = CStr(dt.Rows(rowIndex)("LicensesNumber"))
TextBox2.Text = CStr(dt.Rows(rowIndex)("LicensesName"))
TextBox5.Text = CStr(dt.Rows(rowIndex)("LicensesType"))
ComboBox6.Text = CStr(dt.Rows(rowIndex)("LicensesCase"))
ComboBox1.Text = CStr(dt.Rows(rowIndex)("Location"))
ComboBox2.Text = CStr(dt.Rows(rowIndex)("Street"))
TextBox3.Text = CStr(dt.Rows(rowIndex)("NearOrNextTo"))
ComboBox3.SelectedValue = CStr(dt.Rows(rowIndex)("ActionType"))
ComboBox7.SelectedValue = CStr(dt.Rows(rowIndex)("ActionNumber"))
ComboBox5.SelectedValue = CStr(dt.Rows(rowIndex)("FineOrWarrningType"))
TextBox4.Text = CStr(dt.Rows(rowIndex)("Notes"))
DateTimePicker1.Value = CStr(dt.Rows(rowIndex)("DateOfFollow"))
Next
End If
Kind Regards
November 24, 2015 at 12:50 am
dt.Rows.Count < 0
If no rows are returned, the count would equal zero, surely? What makes you think that it would be less than zero?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
November 24, 2015 at 12:51 am
Thank you, SSC, for helpfully encoding my 'less than' symbol 😀
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
November 24, 2015 at 12:55 am
This is what i was thinking about it cannot be zero !! 😀 So, What i have to change in the code ??
November 24, 2015 at 1:25 am
Any Help ? 😀
November 24, 2015 at 1:38 am
I just don't understand what you mean by 'where should I put the message?'
Why do you need to 'put it' anywhere?
If you need it on a Winform, put it into a text box.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
November 24, 2015 at 1:45 am
ok let me explane to you sir what i want to do . Sorry for my bad english
What i want is to search record in database ok ? if no recorde found then message will show to user that no result found in database .
is this code is correct ? because i works fine no problems but when i enter number that not in database the message dose not show . 😀
November 24, 2015 at 1:49 am
Phil already told you what the problem is.
dt.Rows.Count < 0
If no rows are returned, the count would equal zero, surely?
You're checking to see if the row count will be less than zero. It can't be. If no rows are returned, then the row count will = 0, not be less that. So fix your IF statement to check for 0 rows.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 24, 2015 at 1:49 am
Phil Parkin (11/24/2015)
Thank you, SSC, for helpfully encoding my 'less than' symbol 😀
Happens if the and [ /code] are on the same lime. Put a new line in your code box and it'll show fine.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 24, 2015 at 1:51 am
Btw, Khalid, you have a SQL Injection vulnerability in your code, potentially allowing any random user to steal your database, change data, or drop the entire database. For the sake of your company and users, please do some research on SQL Injection and how to properly parameterise queries and stop concatenating text boxes into the query string, before someone does something nasty to your DB.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 24, 2015 at 1:57 am
GilaMonster (11/24/2015)
Phil Parkin (11/24/2015)
Thank you, SSC, for helpfully encoding my 'less than' symbol 😀Happens if the
and [ /code] are on the same lime. Put a new line in your code box and it'll show fine.
And that is today's top tip! Thank you very much.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
November 24, 2015 at 4:35 am
Fill() method returns number of rows successfully added to or refreshed. So if you need to know exactly the number of rows fetch by Fill() (no matter if dt has any rows before Fill ) try
If dataAdapter.Fill(dt) = 0 Then
MsgBox("No Record Found", MsgBoxStyle.Critical, "Error")
Else
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply