Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Row doesn't exist problem

Reply
Thread Tools

Row doesn't exist problem

 
 
Mike
Guest
Posts: n/a
 
      04-25-2004
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


 
Reply With Quote
 
 
 
 
Chris R. Timmons
Guest
Posts: n/a
 
      04-26-2004
"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/
 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      04-26-2004
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
>
>


 
Reply With Quote
 
Mike Varley
Guest
Posts: n/a
 
      04-26-2004
Thanks Chris


"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xns94D6C3E9659AFcrtimmonscrtimmonsin@207.46.2 48.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/



 
Reply With Quote
 
Mike Varley
Guest
Posts: n/a
 
      04-26-2004
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
> >
> >

>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ok I can do a totals row but how about a percentage row after each data row D ASP .Net Datagrid Control 0 05-23-2005 04:10 PM
Does my object exist? So why its HWND doesn't exist? That's a question... (CMonthCalCtrl control) LT C++ 7 07-25-2004 07:08 PM
Can the Favourites Sidebar (IE, Opera, etc.) be addressed selectively (exist or non-exist)? Markus Mohr Javascript 7 11-28-2003 12:20 AM
In Schema, how to say "If one element exist, another element must exist"? Y.S. XML 3 09-17-2003 02:51 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. Èý¹â ASP .Net 1 07-29-2003 04:31 PM



Advertisments