表单到数据库的连接问题 [英] forms to database connectivity problem
本文介绍了表单到数据库的连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在大学里做一个项目.我为我的软件创建了一个登录页面.它也可以正常工作.但是当我更改数据库中的用户名和密码时,它不起作用..此外,当我将任何一个字段留空时它必须给出有关该特定字段的错误消息.例如:密码为空,您能帮我吗!
这是我的代码:
M doing a project in my college.I created a login page for my software.N its working too.But when i change username and password in database ,its not working..moreover when i leave any one of the fields empty it has to give a error msg about that particular field..eg:passsword is emptyCan u help me plz!!
here is my code:
Imports System.Data
Imports System.Data.SqlClient
Public Class login1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (ComboBox1.Text = "select") Then
MessageBox.Show("please select user or admin")
TextBox1.Text = ""
TextBox2.Text = ""
End If
Try
If (ComboBox1.Text = "admin") Then
Dim str As String = "select * from log where adm = ''admin''; "
Dim con As New SqlConnection()
con.ConnectionString = "Data Source=PROJECT-856F221\SQLEXPRESS;Initial Catalog=farma;Integrated Security=True"
con.Open()
Dim cmd As New SqlCommand(str, con)
Dim reader As SqlDataReader
reader = cmd.ExecuteReader
Dim found As Integer = 0
Dim un, pwd, un1, pwd1, grp, grp1 As String
grp = ComboBox1.Text.Trim()
un = TextBox1.Text.Trim()
pwd = TextBox1.Text.Trim()
While (reader.Read())
grp1 = reader.GetString(0).Trim()
un1 = reader.GetString(1).Trim()
pwd1 = reader.GetString(2).Trim()
If (grp = grp1 And un = un1 And pwd = pwd1) Then
found = 1
Exit While
End If
End While
reader.Close()
con.Close()
If found = 0 Then
Label3.Visible = True
Label3.Text = "User Name or Password Incorrect"
Else
adminhom.Show()
Me.Hide()
End If
ElseIf (ComboBox1.Text = "user") Then
user_login()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub user_login()
Try
Dim str As String = "select * from log where adm = ''user''"
Dim con As New SqlConnection()
con.ConnectionString = "Data Source=PROJECT-856F221\SQLEXPRESS;Initial Catalog=farma;Integrated Security=True"
con.Open()
Dim cmd As New SqlCommand(str, con)
Dim reader As SqlDataReader
reader = cmd.ExecuteReader
Dim found As Integer = 0
Dim un, pwd, un1, pwd1 As String
un = TextBox1.Text.Trim()
pwd = TextBox1.Text.Trim()
While (reader.Read())
un1 = reader.GetString(1).Trim()
pwd1 = reader.GetString(2).Trim()
If (un = un1 And pwd = pwd1) Then
found = 1
Exit While
End If
End While
reader.Close()
con.Close()
If found = 0 Then
Label3.Visible = True
Label3.Text = "User Name or Password Incorrect"
Else
farmerhome.Show()
Me.Hide()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Visible = False
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
End Sub
End Class
推荐答案
亲爱的朋友,
对于某些文本框的离开事件的警报消息框为:-
Dear Friend,
For the alert message box on the leave event of some textbox is:-
Private void TextBox1_Leave(sender As Object, e As System.EventArgs) Handles textBox1.Leave
{
if(TextBox1.Text=="") then
MessageBox.Show("passsword is empty"); //in your case
}
希望对您有所帮助.
谢谢
I hope this will help you out.
Thanks
这篇关于表单到数据库的连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文