如何通过从另一个表单调用数据将数据从数据库显示到另一个表单

时间:2014-02-05 04:40:17

标签: c# mysql

我已在C#中创建了一个表单供用户登录,当他们成功登录时,已登录用户的数据将显示在gridview中的另一个表单中。这里的问题是当用户成功登录然后转到另一个表单时数据没有显示,我得到一个错误“对象引用未设置为对象的实例”。

这是我的代码: LoginFaculty.cs

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using MySql.Data.MySqlClient;
namespace ClassScheduling
{
public partial class LogInFaculty : Form
{
    public LogInFaculty()
    {
        InitializeComponent();
    }

    private void btnSignInFaculty_Click(object sender, EventArgs e)
    {
        try
        {
            string myConnection = "datasource=localhost;port=3306;username=root;password=root";
            MySqlConnection myConn = new MySqlConnection(myConnection);
            MySqlCommand SelectCommand = new MySqlCommand(" select * from database.faculty where FullName='" + this.txtBoxUserNameFaculty.Text + "' and Password= '" + this.txtBoxPasswordFaculty.Text + "'", myConn);
            MySqlDataReader myReader;
            myConn.Open();
            myReader = SelectCommand.ExecuteReader();
            string lastname = myReader.ToString();

            int count = 0;
            while (myReader.Read())
            {
                count = count + 1;
            }
            if (count == 1)
            {

                MessageBox.Show("LogIn Sucessfully");
                this.Hide();

                FacultyLoadForm newForm = new FacultyLoadForm();
                newForm.Show();
            }
            else if (count > 1)
            {
                MessageBox.Show("Duplicate Username and password ....Access Denied!");
            }
            else
                MessageBox.Show("Username or Password is incorrect");

            myConn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void btnExitFaculty_Click(object sender, EventArgs e)
    {
        Account newForm = new Account();
        newForm.Show();
        this.Hide();
    }
}
}

然后是我的FacultyLoad.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ClassScheduling
{
public partial class LogInFaculty : Form
{
    public LogInFaculty()
    {
        InitializeComponent();
    }

    private void btnSignInFaculty_Click(object sender, EventArgs e)
    {
        try
        {
            string myConnection = "datasource=localhost;port=3306;username=root;password=root";
            MySqlConnection myConn = new MySqlConnection(myConnection);
            MySqlCommand SelectCommand = new MySqlCommand(" select * from database.faculty where FullName='" + this.txtBoxUserNameFaculty.Text + "' and Password= '" + this.txtBoxPasswordFaculty.Text + "'", myConn);
            MySqlDataReader myReader;
            myConn.Open();
            myReader = SelectCommand.ExecuteReader();
            string lastname = myReader.ToString();

            int count = 0;
            while (myReader.Read())
            {
                count = count + 1;
            }
            if (count == 1)
            {

                MessageBox.Show("LogIn Sucessfully");
                this.Hide();

                FacultyLoadForm newForm = new FacultyLoadForm();
                newForm.Show();
            }
            else if (count > 1)
            {
                MessageBox.Show("Duplicate Username and password ....Access Denied!");
            }
            else
                MessageBox.Show("Username or Password is incorrect");

            myConn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void btnExitFaculty_Click(object sender, EventArgs e)
    {
        Account newForm = new Account();
        newForm.Show();
        this.Hide();
    }
}
}

就是这样!我真的不知道为什么我一直收到这个错误,我已经尝试了所有可能的解决方案。如果有人知道,请告诉我。

3 个答案:

答案 0 :(得分:0)

做这样的事情..

LoginFaculty.cs的代码:

 private void btnSignInFaculty_Click(object sender, EventArgs e)
    {
        try
        {
            ...
            ....
            while (myReader.Read())
            {
                count = count + 1;
            }
            if (count == 1)
            {

                MessageBox.Show("LogIn Sucessfully");
                this.Hide();
               //Use Overloaded Constructor..
                FacultyLoadForm newForm = new FacultyLoadForm(this.txtBoxUserNameFaculty.Text , this.txtBoxPasswordFaculty.Text);
                newForm.Show();
            }
            ...
            ...            
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

FacultyLoad.cs代码

 // Global Variables, which will be used for Page Load Event to get data...
 string LoginUserName,LoginUserPassword;
 // Over Loaded Constructor..
 public LogInFaculty(string UserName,string Password)
 {
      LoginUserName=UserName;
      LoginUserPassword=Password;
      InitializeComponent();
 }

现在在FacultyLoad.cs Pages Load Event中编写代码以填充Grid View

答案 1 :(得分:0)

FacultyLoadForm创建一个构造函数,并在成功登录后从登录表单传递所有必需的值。

答案 2 :(得分:0)

您收到错误,因为您没有正确地将用户ID传递给详细信息表单。有很多方法可以做到这一点。但更可接受的方法是在成功登录后将用户ID保存到静态类的静态字段中。这样您就可以从任何表单中使用该用户ID,而无需一次又一次地发送用户ID。请按照以下步骤操作。

  1. 在项目中创建静态类名称Global.cs。并添加一个名为UserID的静态成员

    public static class Global
    {
      public static string UserID;
    }
    
  2. 然后在您的登录表单中,在登录按钮单击事件

    中执行以下操作
    // if log in successful then
    Global.UserID= txtUserID.Text;
    // call the details form
    // hide the login form
    
  3. 详细信息表单中,您可以按照以下方式检索用户ID

    string userID=Global.UesrID;
    // now you can user this ID to retrive the detail value of that User