为什么我一直收到空白表格C#

时间:2015-10-25 07:20:53

标签: c# sql database forms

我有一个login_form和一个admin_form。每当我尝试登录时,我都会得到一个空表格。为什么我一直这样做?

这是我的login_form代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace InternationalStudentsSociteySmartSystem
{
public partial class login_form : Form
{
    public login_form()
    {
        InitializeComponent();
    }

    private void login_button_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=ISSSS_DB;Integrated Security=True");
        SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) from Table_1 where FullName='" + username_text.Text + "' and Password='" + password_text.Text + "' and Role='" + comboBox1.Text + "'", con);
        DataTable dt = new System.Data.DataTable();
        sda.Fill(dt);
        if (dt.Rows[0][0].ToString() == "1")
        {
            SqlDataAdapter sda1 = new SqlDataAdapter("Select Role from Table_1 where FullName='" + username_text.Text + "' and Password='" + password_text.Text + "'", con);
            DataTable dt1 = new System.Data.DataTable();
            sda1.Fill(dt1);
            if (dt1.Rows[0][0].ToString() == "Administrator")
            {
                admin_form ss = new admin_form();
                ss.Show();
            }
            if (dt1.Rows[0][0].ToString() == "Committee")
            {
                committee_form ss = new committee_form();
                ss.Show();
            }
            if (dt1.Rows[0][0].ToString() == "Secretary")
            {
                secretary_form ss = new secretary_form();
                ss.Show();
            }
        }

    }
}
}

这是我的admin_form代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace InternationalStudentsSociteySmartSystem
{
public partial class admin_form : Form
{
    SqlConnection conn = new SqlConnection("Data Source=TAREK-PC;Initial Catalog=ISSSS_DB;Integrated Security=True");
    SqlCommand command;
    string imgLoc;
    SqlDataReader myReader;
    public admin_form(string username)
    {
        InitializeComponent();
    }

    public admin_form()
    {
        // TODO: Complete member initialization
    }
    private void button1_Click(object sender, EventArgs e)
    {
        welcome_text.Text = "Welcome!";

        try
        {
            string sql = "SELECT Image FROM Table_1 WHERE FullName='" + admin_name_text.Text + "'";
            if (conn.State != ConnectionState.Open)
                conn.Open();
            command = new SqlCommand(sql, conn);
            SqlDataReader reader = command.ExecuteReader();
            reader.Read();
            if (reader.HasRows)
            {
                byte[] img = (byte[])(reader[0]);
                if (img == null)
                    admin_picturebox.Image = null;
                else
                {
                    MemoryStream ms = new MemoryStream(img);
                    admin_picturebox.Image = Image.FromStream(ms);
                }

            }
            else
            {

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

    private void admin_form_Load(object sender, EventArgs e)
    {
        admin_picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
    }

    private void register_committee_button_Click(object sender, EventArgs e)
    {
        register_committee_form rcf = new register_committee_form();
        rcf.Show();
    }
}
}

你可以看到我还有其他形式的委员会是委员会形式和委员会形式(他们工作得很好),但我还没有编写他们的代码。所以我认为问题在于管理员表格...... 感谢您的帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

您正在调用不带任何参数的admin_form构造函数。在该构造函数中,缺少对InitializeComponent的调用。所以你的admin_form是完全空白的

您应该将对InitializeComponent的调用也添加到admin_form的无参数构造函数中。

public admin_form(string username)
{
    InitializeComponent();
}

public admin_form()
{
    // TODO: Complete member initialization
    InitializeComponent();

}

作为旁注,与您的实际问题无关。访问数据库的代码易受Sql Injection hacks攻击。解析输入数据会有很多问题。您应该立即开始使用参数化查询方法而不是字符串连接