CoreLab PostgreSQL.net auth for login Friday, November 16, 2007

 

I wanted login access to one of my application through database table fields data on VB.NET  project. For this I devised the following

  1. A table that will have username and password
  2. A SQL query that would run and return record count and if count is = 1. This would be considered as success.

Here is the code that did the trick for me

Dim command As New CoreLab.PostgreSql.PgSqlCommand
Try
PgSqlConnection1.Open()
command = PgSqlConnection1.CreateCommand()
Dim uparam, pparam As New CoreLab.PostgreSql.PgSqlParameter
command.Parameters.Add("uparam", CoreLab.PostgreSql.PgSqlType.VarChar, 100).Value = TextBox1.Text.ToString 'this converts the VB.Net Text box to PostgreSQl data type
command.Parameters.Add("pparam", CoreLab.PostgreSql.PgSqlType.VarChar, 25).Value = TextBox2.Text.ToString
command.CommandText = "select * from authuser where uname = :uparam AND passwd = :pparam"
command.BeginExecuteNonQuery()
Dim rowcount As Integer
rowcount = command.GetRecordCount()
If rowcount = 0 Then
Label3.Text = "Retry Incorrect login or password "
PgSqlConnection1.Close()
Else
Me.Hide()
base.Show()
End If
PgSqlConnection1.Close()
Catch ex As Exception
MsgBox("An Error occured please contact administrator" & Chr(13) & ex.Message, MsgBoxStyle.Critical)
End Try
PgSqlConnection1.Close()





Added the above code in click event of a button in VB.NET form. Though it may not be a master piece code for many but for me it is.

1 comments:

Anonymous said...

Thanks for writing this.