当前位置:编程学习 > C#/ASP.NET >>

C#为什么值没有传过来

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 System.Data.SqlClient;
namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {
        public string str = "";
        public Form4()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        private void Form4_Load(object sender, EventArgs e)
        {
            textBox1.Text = DATA.Imformation.Instance.USERNAME;
        }


        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            this.Hide();
            if (frm2.ShowDialog() == DialogResult.OK)
            {
                this.Show();
            }
        }

        private void Form4_Load_1(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            {
                if (this.textBox1.Text == "")
                {
                    this.textBox1.Focus();
                    return;
                }

                string StuffID = this.textBox1.Text;

                try
                {
                    SqlConnection SqlDrConn = new SqlConnection("Data Source=.;Initial Catalog=WageManage;User ID=sa;password=147258369;");
                    SqlDrConn.Open();
                    SqlCommand cmd = SqlDrConn.CreateCommand();
                    cmd.CommandText = string.Format("select * from Wage where StuffID='{0}'", StuffID);


                    SqlDataReader dr = cmd.ExecuteReader();


                    if (dr.HasRows == true)
                    {
                        dr.Read();
                        this.textBox2.Text = dr["StuffName"].ToString();
                        this.textBox3.Text = dr["BasicWage"].ToString();
                        this.textBox6.Text = dr["Medical"].ToString();
                        this.textBox5.Text = dr["Pension"].ToString();
                        this.textBox7.Text = dr["OverTime"].ToString();
                        this.textBox4.Text = dr["House"].ToString();
                        this.textBox8.Text = dr["Award"].ToString();
                        this.textBox10.Text = dr["Leave"].ToString();
                        this.textBox11.Text = dr["Absent"].ToString();
                        dr.Close();
                    }

                }
                catch (SqlException ee)
                {
                    MessageBox.Show(ee.Message);
                }
                finally
                {
                }

            }
        }
    }
}
红字部分就是传值的地方   我在另外一个窗口都能传过来  这里为什么不行  用的是类传值。
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 System.Data.SqlClient;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection SqlDrConn = new SqlConnection("Data Source=.;Initial Catalog=WageManage;User ID=sa;password=147258369;");
            SqlDrConn.Open();
            SqlCommand cmd = SqlDrConn.CreateCommand();
            cmd.CommandText = "select * from stuff where StuffID='" + textBox1.Text + "'and Password='" + textBox2.Text + "'";//textbox1,2分别为用户名和密码
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
          try
            {
                DATA.Imformation.Instance.USERNAME = textBox1.Text;
                DATA.Imformation.Instance.PWD = textBox2.Text;
                Form2 form = new Form2();
                form.ShowDialog();
                DialogResult = DialogResult.OK;
                this.Close();
            }
                catch (Exception Err)
            {
                MessageBox.Show("对不起,您的密码不正确");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
红字标记了 --------------------编程问答-------------------- DATA.Imformation.Instance
在哪里定义的。

另外你这种实现方式相当不好。因为这样一来,你的每一个窗口代码就失去了不加修改被再次重用的可能。 --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DATA
{
    [Serializable]
    public class Imformation
    {
        private static Imformation _Instance;
        private string _username;
        private string _pwd;

        public string USERNAME
        {
            get { return _username; }
            set { _username = value; }
        }

        public string PWD
        {
            get { return _pwd; }
            set { _pwd = value; }
        }

        public static Imformation Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new Imformation();
                }
                return _Instance;
            }
        }  
    }
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,