PDA

View Full Version : ASP: Getting stuff from DBs


karinne
07-03-2003, 10:26 AM
OK... I'm just getting into ASP.NET right now 'cause of work!

If I were to spit stuff out from a database to a web page, in PHP I would write:

<?
connection stuff....
$check = pg_exec ("select * from login where userid='".$_POST['userid']."' and password='".$_POST[password']."'");
$num = pg_numrows($check);

if ($numrows == 0) {
// send them somewhere
} else {
// continue
}
?>

And I could do

Now I'm trying to do the same in ASP (VB.NET). So far, I've got:

Sub LoginBtn_Click(sender As Object, E As EventArgs)

Dim conn As String = "server=(local)\VSdotNET;database=Invoices;trusted_connection=true"
Dim CheckLogin As String = "select * from login where UserID = '" + UserID.Text +"' and Password = '"+ Password.Text +"'"

And this is where I'm stuck! How do I do the bold stuff up above in here?!?!

If (????) Then
Msg.Text = "Winner!"
Else
Msg.Text = "Looser!"
End If

End Sub

Any help, even a site where I can find the info would be greatly appreciated. I've looked but could not fine... frankly, didn't know how to type that in google?!?!

TIA

putts
07-03-2003, 03:49 PM
I sense bitterness about learning ASP......

ASP's recordsets have a nifty little attribute called EOF so you do something like this


set db = server.createobject("ADODB.Connection")
db.open "DB Info here"
set recSet = db.execute("SELECT * FROM TABLE")

if not(recSet.eof) then
Response.write("The Query Is Scottish<BR>")
while not(recSet.eof)
for each field in recSet.fields()
Response.write(field.name & " = " & field & "<BR>")
next
Response.write("<HR>")
recSet.moveNext
wend
else
Response.write("The Query Is Crap!")
end if

recSet.close
set recSet = nothing
db.close
set db = nothing


There's your very generic intro to ASP recordsets (complete with cleanup - although I'm not sure if .NET has a better way of doing that)

Hope this helps ya out.

rdove
07-06-2003, 11:37 PM
putts .EOF does not exsist in ADO.NET.

You are going to need to use a datareader in the system.oledb class library.

Also:

Dim CheckLogin As String = "select * from login where UserID = '" + UserID.Text +"' and Password = '"+ Password.Text +"'"

change to:

Dim CheckLogin As String = "select * from login where UserID = '" & UserID.Text & "' and Password = '" & Password.Text & "'"

When you are concatenating strings you should never use the + sign always use &.

I'm not very fimliar with php, but it looks to me like your checking to see if login credentials are correct:



Sub LoginBtn_Click(sender As Object, E As EventArgs)

If LoginValid(strID, strPwd) Then

Response.Redirect("page.aspx")

Else
lblError.Visible = True
End If

End Sub

Private Function LoginValid( _
ByVal LoginID As String, _
ByVal Password As String) As Boolean

Dim strSQL As String
Dim strConn As String
Dim boolFound As Boolean

' Retrieve connection string.
strConn = 'connection string here

strSQL = 'SQL String

Try
'Execute query and return count of valid records
boolFound = (CInt(ExecuteScalar(strSQL, strConn)) > 0)

If Not boolFound Then
lblError.Visible = True
End If

Catch exp As Exception
lblError.Visible = True
lblError.Text = exp.Message

End Try

Return boolFound
End Function

Public Shared Function ExecuteScalar( _
ByVal SQL As String, _
ByVal ConnectionString As String) As Object

Dim dr As OleDbDataReader
Dim cmd As New OleDbCommand()
Dim Value As Object

Try
With cmd
.Connection = _
New OleDbConnection(ConnectionString)
.Connection.Open()

.CommandText = SQL
Value = .ExecuteScalar()
.Connection.Close()
End With

Catch
Throw

End Try

Return Value
End Function

putts
07-07-2003, 02:30 PM
Ryan,

Any ideas on why they got rid of the EOF function in .NET.

That seams very cracked up as that was like a staple of a lot of ASP coded pages that I've ever used?

The only thing that I can think of is that I know there's functions built right into .NET to populate select boxes using a recordset with the .fill method and stuff like that. Is it assumed that all those functions made the EOF obsolete? I haven't coded .NET more than a few pages, but it's hard for me to imagine that that is the case, but I have been wrong before like once - I think.

Karinne,
Sorry for distracting your thread.

rdove
07-08-2003, 12:59 AM
Originally posted by putts
Ryan,

Any ideas on why they got rid of the EOF function in .NET.

That seams very cracked up as that was like a staple of a lot of ASP coded pages that I've ever used?

The only thing that I can think of is that I know there's functions built right into .NET to populate select boxes using a recordset with the .fill method and stuff like that. Is it assumed that all those functions made the EOF obsolete? I haven't coded .NET more than a few pages, but it's hard for me to imagine that that is the case, but I have been wrong before like once - I think.

Karinne,
Sorry for distracting your thread.

Correct! Most of the controls in .NET have a databind method that binds them to a datasource. .NET introduces something called a dataset that is suppoed to be more powerful than a recordset. You can still use recordsets in .NET, but they are not the preferred way of doing things. As you see in my code above I use a datareader which is a way of creating a dataset and it has a .Read method that works similar to .EOF in recordsets.