No Record Found message vb.net+Sql 2012

  • 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

  • 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?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Thank you, SSC, for helpfully encoding my 'less than' symbol 😀

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • This is what i was thinking about it cannot be zero !! 😀 So, What i have to change in the code ??

  • Any Help ? 😀

  • 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.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • 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 . 😀

  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • 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.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • 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