Row doesn't exist problem

Discussion in 'ASP .Net' started by Mike, Apr 25, 2004.

  1. Mike

    Mike Guest

    Hi

    I've written my first asp.net page below. It queries an Access database with
    one table consisting of two columns - a username and a server name. The
    users enter their login name and the page queries the database and then
    directs them to the correct server. This works fine as long as their name
    actually exists in the database. If it doesn't the the page fails at the

    server = dataSet.Tables(0).Rows(0).Item(0).ToString()

    line saying row doesn't exist. My question is how can I handle this? If the
    usersname is not in the database I would then like the server string to be
    set to a predefined value such as 'noserver'.

    Cheers

    Mike

    Sub Page_Load()

    If Page.IsPostback

    Dim mailserver As String
    mailserver = MyQueryMethod(txtName.Text)

    Select Case mailserver
    Case "Bob"
    Response.Redirect("http://localhost/mailserver1")
    Case "Bill"
    Response.Redirect("http://localhost/mailsever2")
    Case Else
    Response.Redirect("http://localhost/nomailserver")
    End Select

    End If

    End Sub

    Function MyQueryMethod(ByVal user As String) As String
    Dim connectionString As String =
    "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data
    Source=C:\Documents an"& _
    "d Settings\Mike\My Documents\BegASPNET11\Ch01\users.mdb"
    Dim dbConnection As System.Data.IDbConnection = New
    System.Data.OleDb.OleDbConnection(connectionString)

    Dim queryString As String = "SELECT [Users].[Mailserver] FROM
    [Users] WHERE ([Users].[User] = @User)"
    Dim dbCommand As System.Data.IDbCommand = New
    System.Data.OleDb.OleDbCommand
    dbCommand.CommandText = queryString
    dbCommand.Connection = dbConnection

    Dim dbParam_user As System.Data.IDataParameter = New
    System.Data.OleDb.OleDbParameter
    dbParam_user.ParameterName = "@User"
    dbParam_user.Value = user
    dbParam_user.DbType = System.Data.DbType.String
    dbCommand.Parameters.Add(dbParam_user)

    Dim dataAdapter As System.Data.IDbDataAdapter = New
    System.Data.OleDb.OleDbDataAdapter
    dataAdapter.SelectCommand = dbCommand
    Dim dataSet As System.Data.DataSet = New System.Data.DataSet
    dataAdapter.Fill(dataSet)

    Dim server As String
    server = dataSet.Tables(0).Rows(0).Item(0).ToString()

    Return server
    End Function
     
    Mike, Apr 25, 2004
    #1
    1. Advertising

  2. "Mike" <mike@notdisclosed!!.com> wrote in
    news::

    > Hi
    >
    > I've written my first asp.net page below. It queries an Access
    > database with one table consisting of two columns - a username
    > and a server name. The users enter their login name and the page
    > queries the database and then directs them to the correct
    > server. This works fine as long as their name actually exists in
    > the database. If it doesn't the the page fails at the
    >
    > server = dataSet.Tables(0).Rows(0).Item(0).ToString()
    >
    > line saying row doesn't exist. My question is how can I handle
    > this? If the usersname is not in the database I would then like
    > the server string to be set to a predefined value such as
    > 'noserver'.


    Mike,

    If dataSet.Tables(0).Rows.Count > 0 Then
    Return dataSet.Tables(0).Rows(0).Item(0).ToString()
    Else
    Return "noserver"
    End If


    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
     
    Chris R. Timmons, Apr 26, 2004
    #2
    1. Advertising

  3. Hi Mike, you want to test if the value is null using the IsDBNull function.
    Here's an example:

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim server As String
    ds.Tables.Add(CreateDataSource())
    If IsDBNull(ds.Tables(0).Rows(0).Item(0)) Then
    server = "Not in database"
    Else
    server = ds.Tables(0).Rows(0).Item(0).ToString()
    End If
    Response.Write(server)
    End Sub

    Function CreateDataSource() As DataTable
    Dim dt As New DataTable
    Dim dr As DataRow
    dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
    dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
    dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
    dt.Columns.Add(New DataColumn("Boolean", GetType(Boolean)))
    Dim i As Integer
    For i = 0 To 8
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "Item " + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = (i = 4)
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function


    Does this help?

    Ken
    Microsoft MVP [ASP.NET]
    Toronto

    "Mike" <mike@notdisclosed!!.com> wrote in message
    news:...
    > Hi
    >
    > I've written my first asp.net page below. It queries an Access database
    > with
    > one table consisting of two columns - a username and a server name. The
    > users enter their login name and the page queries the database and then
    > directs them to the correct server. This works fine as long as their name
    > actually exists in the database. If it doesn't the the page fails at the
    >
    > server = dataSet.Tables(0).Rows(0).Item(0).ToString()
    >
    > line saying row doesn't exist. My question is how can I handle this? If
    > the
    > usersname is not in the database I would then like the server string to be
    > set to a predefined value such as 'noserver'.
    >
    > Cheers
    >
    > Mike
    >
    > Sub Page_Load()
    >
    > If Page.IsPostback
    >
    > Dim mailserver As String
    > mailserver = MyQueryMethod(txtName.Text)
    >
    > Select Case mailserver
    > Case "Bob"
    > Response.Redirect("http://localhost/mailserver1")
    > Case "Bill"
    > Response.Redirect("http://localhost/mailsever2")
    > Case Else
    > Response.Redirect("http://localhost/nomailserver")
    > End Select
    >
    > End If
    >
    > End Sub
    >
    > Function MyQueryMethod(ByVal user As String) As String
    > Dim connectionString As String =
    > "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data
    > Source=C:\Documents an"& _
    > "d Settings\Mike\My Documents\BegASPNET11\Ch01\users.mdb"
    > Dim dbConnection As System.Data.IDbConnection = New
    > System.Data.OleDb.OleDbConnection(connectionString)
    >
    > Dim queryString As String = "SELECT [Users].[Mailserver] FROM
    > [Users] WHERE ([Users].[User] = @User)"
    > Dim dbCommand As System.Data.IDbCommand = New
    > System.Data.OleDb.OleDbCommand
    > dbCommand.CommandText = queryString
    > dbCommand.Connection = dbConnection
    >
    > Dim dbParam_user As System.Data.IDataParameter = New
    > System.Data.OleDb.OleDbParameter
    > dbParam_user.ParameterName = "@User"
    > dbParam_user.Value = user
    > dbParam_user.DbType = System.Data.DbType.String
    > dbCommand.Parameters.Add(dbParam_user)
    >
    > Dim dataAdapter As System.Data.IDbDataAdapter = New
    > System.Data.OleDb.OleDbDataAdapter
    > dataAdapter.SelectCommand = dbCommand
    > Dim dataSet As System.Data.DataSet = New System.Data.DataSet
    > dataAdapter.Fill(dataSet)
    >
    > Dim server As String
    > server = dataSet.Tables(0).Rows(0).Item(0).ToString()
    >
    > Return server
    > End Function
    >
    >
     
    Ken Cox [Microsoft MVP], Apr 26, 2004
    #3
  4. Mike

    Mike Varley Guest

    Thanks Chris


    "Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
    news:Xns94D6C3E9659AFcrtimmonscrtimmonsin@207.46.248.16...
    > "Mike" <mike@notdisclosed!!.com> wrote in
    > news::
    >
    > > Hi
    > >
    > > I've written my first asp.net page below. It queries an Access
    > > database with one table consisting of two columns - a username
    > > and a server name. The users enter their login name and the page
    > > queries the database and then directs them to the correct
    > > server. This works fine as long as their name actually exists in
    > > the database. If it doesn't the the page fails at the
    > >
    > > server = dataSet.Tables(0).Rows(0).Item(0).ToString()
    > >
    > > line saying row doesn't exist. My question is how can I handle
    > > this? If the usersname is not in the database I would then like
    > > the server string to be set to a predefined value such as
    > > 'noserver'.

    >
    > Mike,
    >
    > If dataSet.Tables(0).Rows.Count > 0 Then
    > Return dataSet.Tables(0).Rows(0).Item(0).ToString()
    > Else
    > Return "noserver"
    > End If
    >
    >
    > Hope this helps.
    >
    > Chris.
    > -------------
    > C.R. Timmons Consulting, Inc.
    > http://www.crtimmonsinc.com/
     
    Mike Varley, Apr 26, 2004
    #4
  5. Mike

    Mike Varley Guest

    Thanks Ken

    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:#...
    > Hi Mike, you want to test if the value is null using the IsDBNull

    function.
    > Here's an example:
    >
    > Private Sub Page_Load _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles MyBase.Load
    > Dim ds As New DataSet
    > Dim dt As New DataTable
    > Dim server As String
    > ds.Tables.Add(CreateDataSource())
    > If IsDBNull(ds.Tables(0).Rows(0).Item(0)) Then
    > server = "Not in database"
    > Else
    > server = ds.Tables(0).Rows(0).Item(0).ToString()
    > End If
    > Response.Write(server)
    > End Sub
    >
    > Function CreateDataSource() As DataTable
    > Dim dt As New DataTable
    > Dim dr As DataRow
    > dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
    > dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
    > dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
    > dt.Columns.Add(New DataColumn("Boolean", GetType(Boolean)))
    > Dim i As Integer
    > For i = 0 To 8
    > dr = dt.NewRow()
    > dr(0) = i
    > dr(1) = "Item " + i.ToString()
    > dr(2) = 1.23 * (i + 1)
    > dr(3) = (i = 4)
    > dt.Rows.Add(dr)
    > Next i
    > Return dt
    > End Function
    >
    >
    > Does this help?
    >
    > Ken
    > Microsoft MVP [ASP.NET]
    > Toronto
    >
    > "Mike" <mike@notdisclosed!!.com> wrote in message
    > news:...
    > > Hi
    > >
    > > I've written my first asp.net page below. It queries an Access database
    > > with
    > > one table consisting of two columns - a username and a server name. The
    > > users enter their login name and the page queries the database and then
    > > directs them to the correct server. This works fine as long as their

    name
    > > actually exists in the database. If it doesn't the the page fails at the
    > >
    > > server = dataSet.Tables(0).Rows(0).Item(0).ToString()
    > >
    > > line saying row doesn't exist. My question is how can I handle this? If
    > > the
    > > usersname is not in the database I would then like the server string to

    be
    > > set to a predefined value such as 'noserver'.
    > >
    > > Cheers
    > >
    > > Mike
    > >
    > > Sub Page_Load()
    > >
    > > If Page.IsPostback
    > >
    > > Dim mailserver As String
    > > mailserver = MyQueryMethod(txtName.Text)
    > >
    > > Select Case mailserver
    > > Case "Bob"
    > > Response.Redirect("http://localhost/mailserver1")
    > > Case "Bill"
    > > Response.Redirect("http://localhost/mailsever2")
    > > Case Else
    > > Response.Redirect("http://localhost/nomailserver")
    > > End Select
    > >
    > > End If
    > >
    > > End Sub
    > >
    > > Function MyQueryMethod(ByVal user As String) As String
    > > Dim connectionString As String =
    > > "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data
    > > Source=C:\Documents an"& _
    > > "d Settings\Mike\My Documents\BegASPNET11\Ch01\users.mdb"
    > > Dim dbConnection As System.Data.IDbConnection = New
    > > System.Data.OleDb.OleDbConnection(connectionString)
    > >
    > > Dim queryString As String = "SELECT [Users].[Mailserver]

    FROM
    > > [Users] WHERE ([Users].[User] = @User)"
    > > Dim dbCommand As System.Data.IDbCommand = New
    > > System.Data.OleDb.OleDbCommand
    > > dbCommand.CommandText = queryString
    > > dbCommand.Connection = dbConnection
    > >
    > > Dim dbParam_user As System.Data.IDataParameter = New
    > > System.Data.OleDb.OleDbParameter
    > > dbParam_user.ParameterName = "@User"
    > > dbParam_user.Value = user
    > > dbParam_user.DbType = System.Data.DbType.String
    > > dbCommand.Parameters.Add(dbParam_user)
    > >
    > > Dim dataAdapter As System.Data.IDbDataAdapter = New
    > > System.Data.OleDb.OleDbDataAdapter
    > > dataAdapter.SelectCommand = dbCommand
    > > Dim dataSet As System.Data.DataSet = New System.Data.DataSet
    > > dataAdapter.Fill(dataSet)
    > >
    > > Dim server As String
    > > server = dataSet.Tables(0).Rows(0).Item(0).ToString()
    > >
    > > Return server
    > > End Function
    > >
    > >

    >
     
    Mike Varley, Apr 26, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H5N1
    Replies:
    1
    Views:
    765
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Apr 12, 2006
  2. Y.S.
    Replies:
    3
    Views:
    1,080
    strajan
    Sep 17, 2003
  3. LT
    Replies:
    7
    Views:
    2,146
    Phlip
    Jul 25, 2004
  4. D
    Replies:
    0
    Views:
    251
  5. Markus Mohr
    Replies:
    7
    Views:
    275
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
Loading...

Share This Page