我用vs2005开发软件,今天遇到了如下问题:
程序一共用到3个项,要实现的功能是对每个新建注册的用户赋予不同的访问权限。
第一个项:Program.cs   内容如下:
using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace WindowsApplication1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            A系统 form = new A系统();
            login Login = new login();
            form.myname = Login.textBox1.Text.Trim();
            Application.Run(new login());
          
        }
    }
}第二个项: A系统.cs   内容如下using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace WindowsApplication1
{
    public partial class A系统 : Form
    {
        public string myname;
                public A系统()
        {
            InitializeComponent();
        }        private void A系统_Load(object sender, EventArgs e)
        {
            label1.Parent = pictureBox1;
            label1.BackColor = Color.Transparent;            SqlConnection con = new SqlConnection("server=(local);pwd=;uid=008;database=DB-Exempt");
            con.Open();
            //Login login = new Login();
             
            //取得当userinfo.name等于登陆框输入登陆名时所具有的权限值
            SqlDataAdapter adp = new SqlDataAdapter("select popedom1,popedom2,popedom3,popedom4 as Roles_UserInfo from Roles inner join UserInfo on Roles.rolename = UserInfo.rolename where UserInfo.name = '" + myname + "' ", con);
            DataSet ds = new DataSet();
            adp.Fill(ds, "Roles_UserInfo"); ;            //---------通过查询所拥有的权限,设置相应子窗体的Enabled值,从而确定该子窗体是否可用-----------------
            if (ds.Tables["Roles_UserInfo"].Rows[0][0].ToString().Trim() == "Yes")
            {
                this.button1.Enabled = true;
                
            }
            else
            {
                this.button1.Enabled = false;
            }
            if (ds.Tables["Roles_UserInfo"].Rows[0][1].ToString().Trim() == "Yes")
            {
                this.button2.Enabled = true;
            }
            else
            {
                this.button2.Enabled = false;
            }
            if (ds.Tables["Roles_UserInfo"].Rows[0][2].ToString().Trim() == "Yes")
            {
                this.button3.Enabled = true;
            }
            else
            {
                this.button3.Enabled = false;
            }
            if (ds.Tables["Roles_UserInfo"].Rows[0][3].ToString().Trim() == "Yes")
            {
                this.button4.Enabled = true;
            }
            else
            {
                this.button4.Enabled = false;
            }
        }        private void button1_Click(object sender, EventArgs e)
        {
            Form form1 = new WindowsApplication1.select();
            form1.Show();
        }        private void button2_Click(object sender, EventArgs e)
        {
            Form form2 = new WindowsApplication1.add();
            form2.Show();
        }        private void button3_Click(object sender, EventArgs e)
        {
            Form form3 = new WindowsApplication1.update();
            form3.Show();
        }        private void button4_Click(object sender, EventArgs e)
        {
            Form form4 = new WindowsApplication1.delete();
            form4.Show();
        }        private void 使用教程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form form1 = new WindowsApplication1.aboutA();
            form1.Show();
        }    }
}第三个项:login.cs 内容如下:(这个就是注册的部分)using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace WindowsApplication1
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }        
        public bool blCanLogin;        SqlConnection Con = new SqlConnection("server=(local);pwd=;uid=008;database=DB-Exempt");        private void login_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 1000;
            this.label3.Text = Convert.ToString(DateTime.Now.ToLocalTime());            label1.Parent = pictureBox1;
            label1.BackColor = Color.Transparent;            label2.Parent = pictureBox1;
            label2.BackColor = Color.Transparent;            label3.Parent = pictureBox1;
            label3.ba                    }        private void button1_Click(object sender, EventArgs e)
        {
            blCanLogin = false;
            string sql = "select name,passwd from UserInfo";
            DataRow myRow;
            Con.Open();
            SqlDataAdapter adp = new SqlDataAdapter(sql, Con);
            DataSet ds = new DataSet();
            adp.Fill(ds, "UserInfo");
            if (textBox1.Text.Trim() != "")
            {
                if (textBox2.Text.Trim() != "")
                {
                    for (int i = 0; i < ds.Tables["UserInfo"].Rows.Count; i++)
                    {
                        myRow = ds.Tables["UserInfo"].Rows[i];
                        if (myRow[0].ToString().Trim() == textBox1.Text.ToString().Trim() && myRow[1].ToString().Trim() == textBox2.Text.ToString().Trim())
                        {
                            blCanLogin = true;
                            A系统 mainForm = new A系统();
                            mainForm.myname = this.textBox1.Text.Trim();
                            Form fm = new WindowsApplication1.Mainform();
                            fm.Show();
                            
                            return;                        }
                        Con.Close();
                    }
                    MessageBox.Show("你输入的用户名或密码不正确!");
                    Con.Close();
                    return;
                }
                else
                    MessageBox.Show("密码不能为空");
                Con.Close();
                return;
            }
            else
                MessageBox.Show("用户名不能为空");
            Con.Close();
        }        private void button2_Click(object sender, EventArgs e)
        {
            blCanLogin = false;
            this.Close();
        }       
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.label3.Text = Convert.ToString(DateTime.Now.ToLocalTime());        }        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            panel1.Parent = pictureBox1;
            panel1.BackColor = Color.Transparent;
            label1.Parent = panel1;
            label1.BackColor = Color.Transparent;
        }
        
    }
}

解决方案 »

  1.   

    出现的错误是集中在以下代码中:
    private void A系统_Load(object sender, EventArgs e)
            {
                label1.Parent = pictureBox1;
                label1.BackColor = Color.Transparent;            SqlConnection con = new SqlConnection("server=(local);pwd=;uid=008;database=DB-Exempt");
                con.Open();
                //Login login = new Login();
                 
                //取得当userinfo.name等于登陆框输入登陆名时所具有的权限值
                SqlDataAdapter adp = new SqlDataAdapter("select popedom1,popedom2,popedom3,popedom4 as Roles_UserInfo from Roles inner join UserInfo on Roles.rolename = UserInfo.rolename where UserInfo.name = '" + myname + "' ", con);
                DataSet ds = new DataSet();
                adp.Fill(ds, "Roles_UserInfo"); ;            //---------通过查询所拥有的权限,设置相应子窗体的Enabled值,从而确定该子窗体是否可用-----------------
                if (ds.Tables["Roles_UserInfo"].Rows[0][0].ToString().Trim() == "Yes")
                {
                    this.button1.Enabled = true;
                    
                }系统提示:在位置 0 处没有任何行。未处理indexoutofrangeexception我初步判断是where UserInfo.name = '" + myname + "' ", con); 中的 myname 没有被赋值。各位高手帮忙看下!