08-09-14, 08:56 PM
.....
تم تعديل الكود للفورم1
كما ذكر الأستاذ kslawy استخدم id المستخدم بدلا من اسمه وهو الأسلوب الصحيح
على افتراض ان:
الجدول users له الحقول التالية
id رقم
username نص
password نص
usertype نص
الجدول comments له الحقول التالية
post_id رقم
userid رقم
title نص
message نص
تم تجاهل أكواد Button1 وما بعدها
.....
تم تعديل الكود للفورم1
كما ذكر الأستاذ kslawy استخدم id المستخدم بدلا من اسمه وهو الأسلوب الصحيح
على افتراض ان:
الجدول users له الحقول التالية
id رقم
username نص
password نص
usertype نص
الجدول comments له الحقول التالية
post_id رقم
userid رقم
title نص
message نص
تم تجاهل أكواد Button1 وما بعدها
كود :
Imports MySql.Data.MySqlClient
Public Class Form1
Private conString As String = "Server=localhost; Database=oman ;Uid=root; Pwd=;"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GroupBox1.Enabled = True
lbllogin.Enabled = False
End Sub
Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
'check if the textbox is equal to nothing then it will display the message below!.
If Me.txtuname.Text = "" And Me.txtpass.Text = "" Then
MsgBox("Password or Username Incorrect!")
Exit Sub
End If
Try
Using con As New MySqlConnection(conString)
Dim sql As String = "SELECT * FROM `users` WHERE `username`=@username AND `password`=@password"
Using da As New MySqlDataAdapter(sql, con)
da.SelectCommand.Parameters.AddWithValue("@username", Me.txtuname.Text)
da.SelectCommand.Parameters.AddWithValue("@password", Me.txtpass.Text)
Dim dt As New DataTable
If da.Fill(dt) > 0 Then
Dim row As DataRow = dt.Rows(0)
Select Case row("usertype")
Case "admin"
'welcomes the user as Admiistrator
MsgBox("Welcome " & row("username") & " you login as Administrator ")
'set the lbllogin text to Logout
'disabled the groupbox
Me.GroupBox1.Enabled = False
'reset all the two textbox
'set the lblname to the specific user
Me.lblname.Text = "Hi, " & row("username")
Case "Encoder"
MsgBox("Welcome " & row("username") & " you login as Encoder ")
Me.GroupBox1.Enabled = True
Me.lblname.Text = "Hi, " & row("username")
Case Else
MsgBox("You login as Guest!")
Me.GroupBox1.Enabled = False
Me.lblname.Text = "Hi, " & row("username")
Me.lbllogin.Enabled = True
Me.DataGridView1.Visible = True
End Select
Me.lbllogin.Enabled = True
Me.lbllogin.Text = "Logout"
ViewUserComments(row("id"))
Else
MsgBox("Contact administrator to registered!")
End If
Me.txtuname.Text = ""
Me.txtpass.Text = ""
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ViewUserComments(userid As String)
Using con As New MySqlConnection(conString)
Using da As New MySqlDataAdapter("SELECT * FROM `comments` WHERE `userid`=@userid ", con)
da.SelectCommand.Parameters.AddWithValue("@userid", userid)
Using dt As New DataTable
If da.Fill(dt) > 0 Then
DataGridView1.DataSource = dt
DataGridView1.Columns(0).HeaderText = "ID"
DataGridView1.Columns(0).Width = 50
DataGridView1.Columns(1).HeaderText = "added by"
DataGridView1.Columns(1).Width = 150
DataGridView1.Columns(2).HeaderText = "Name Thread"
DataGridView1.Columns(2).Width = 200
DataGridView1.Columns(3).HeaderText = "Notes"
DataGridView1.Columns(3).Width = 200
DataGridView1.Columns(1).DataGridView.Name = "& name"
End If
End Using
End Using
End Using
End Sub
Private Sub lbllogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbllogin.Click
If Me.lbllogin.Text = "Logout" Then
Me.DataGridView1.DataSource = Nothing
Me.lbllogin.Enabled = False
Me.lbllogin.Text = "Login"
Me.lblname.Text = "Hi, Guest!"
Me.GroupBox1.Enabled = True
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button1.Click
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim bm As New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)
DataGridView1.DrawToBitmap(bm, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height))
e.Graphics.DrawImage(bm, 0, 0)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
Dim sql As String = "SELECT * FROM comments"
Dim ds As New DataSet()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "comments"
End Sub
End Class.....



