Item Searching in Database in not there

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • timomike
    New Member
    • Oct 2011
    • 1

    Item Searching in Database in not there

    Hi! pls help me.
    i already have my project. its on VB.net and MS SQL server.

    but im having a hard time in trapping if the one im searching in the databse is not there..

    im searching a primary key but if not there im having an error.

    pls help me. heres my code for viewing.



    Public Function GetByRefNum(ByV al RefNum As Integer) As DataSet
    '' This is where we are inserted all the values
    Dim connectionStrin g As String = "Data Source=MLTimo;I nitial Catalog=OSDSdb; Persist Security Info=True;User ID=sa; PWD=password"
    Dim myConnection As SqlConnection = New SqlConnection(c onnectionString )

    Dim myDataSet As New DataSet
    Dim myDataAdapter As SqlDataAdapter

    Try

    Dim cmd As New SqlCommand
    'Dim reader As SqlDataReader

    cmd.CommandText = "SELECT * FROM tblRequest Where RefNumber = " + RefNum.ToString ()
    cmd.CommandType = CommandType.Tex t
    cmd.Connection = myConnection

    myConnection.Op en()
    myDataAdapter = New SqlDataAdapter( cmd)
    '' fill the dataset
    myDataAdapter.F ill(myDataSet)

    Catch ex As Exception
    Throw ex
    Finally
    '' close the connection
    myConnection.Cl ose()
    End Try

    Return myDataSet

    End Function



    'the error will come here. "Index out of range something"

    Dim ds As DataSet = GetByRefNum(Req uestForm.TxBxRe fNum.Text)
    Me.LblRefNum.Te xt = ds.Tables(0).Ro ws(0).Item("Ref Number").ToStri ng()
    Me.LblType.Text = ds.Tables(0).Ro ws(0).Item("Typ e").ToString ()
    Me.LblRequestor .Text = ds.Tables(0).Ro ws(0).Item("Req uestor").ToStri ng()
    Me.LblName.Text = ds.Tables(0).Ro ws(0).Item("Nam e").ToString ()
    Me.LblVenue.Tex t = ds.Tables(0).Ro ws(0).Item("Ven ue").ToString ()
    Me.LblStat.Text = ds.Tables(0).Ro ws(0).Item("Sta tus").ToString( )
Working...