各位大家好,我是一名学生,面试者毕业设计,以前没有接触过C#,现在开始学,不是很明白,最近有个问题一直没有解决,我编写的是一个实际考试系统,采用C#和SQL 2000,我的构想是由登录form1开始,若登录成功就在下一个form2中显示该名考生的详细信息,但是数据总是显示不出来,请帮忙解决一下,在这谢谢了。

解决方案 »

  1.   

    应该没有取到值或者绑定出错。把sql语句贴出来看看
      

  2.   

    使用get,set传递,不能用VB中的方式
      

  3.   


    你先public一个变量str ,用来存储sql语句,public   Form1()   
      {   
        
      public   string   str;   
      private   void   button1_Click(object   sender,   System.EventArgs   e)   
      {   
            
        
      Form2   form2=new   Form2(this);   
      Form1.ActiveForm.Hide();   
      form2.Show();   
      }   
        
      }   
        
        
        
      在FORM2中   
      public   Form2(Form1   form1)   
      {   
      str   =form1.str;   
      }   
      

  4.   

    form1验证登陆成功后,把唯一标识该人信息的ID传给Form2,然后在Form2的From_load事件中取出该人的信息,显示在窗口上
      

  5.   

    把语句返回给form2,然后执行这条语句绑定数据
      

  6.   

    c#窗体间传递复杂数据
    2006-12-15 10:25
    在设计窗体程序时往往需要相互调用的窗体间传递复杂的数据,有时候甚至需要子窗体修改父窗体的内容.前一阵在博客园中看到很多人讨论这个问题,在海天一鸥 《窗体间传值和窗体间互操作》的评论中,我看到有这么几种做法:1)公开一个静态变量;2)在子窗体中创建一个公有字段;3)在父窗体中使用委托与事件; 4)将子窗体作为父窗体成员.
    这些办法我感觉都不是特别好,会导致父窗体与子窗体耦合过于紧密,对任何一个窗体的修改需要重新编译另外一个窗体.根据"依赖倒置”的原则,通过引入一个 结果对象,就可以避免这种紧耦合,同时也可以传递任意复杂的数据.如果需要在子窗体中改变父窗体状态,也可以在这个结果对象中定义委托与事件来达到目的. 我在这里给出我的解决方案.首先定义一个结果对象,用来存放子窗体返回的结果.同时定义一些事件,可以让子窗体修改父窗体的状态.代码如下:
    using System;
    namespace WinParam
    {
    public delegate void TextChangedHandler(string s);
    public class cResult
    {
    public string Result1 = "";
    public string Result2 = "";
    public event TextChangedHandler TextChanged;
    public void ChangeText(string s)
    {
    if(TextChanged != null)
    TextChanged(s);
    }
    }
    }
    添加一子窗体构造函数,允许接收一结果对象:
    private cResult r;
    public frmChild(cResult r):this()
    {
    this.r = r;
    }
    在父窗体中创建子窗体,并订阅cResult事件:
    private void btnCallChild_Click(object sender, System.EventArgs e)
    {
    cResult r = new cResult();
    r.TextChanged += new TextChangedHandler(this.EventResultChanged);
    frmChild fc = new frmChild(r);
    fc.ShowDialog();
    txtCallResult.Text = "The Result is: " + r.Result1 + " " + r.Result2;
    }
    private void EventResultChanged(string s)
    {
    txtEventResult.Text = s;
    }
    这样确保父窗体知道子窗体,而子窗体不知道父窗体.父窗体改变后不需要重新编译子窗体.同时两个窗体都依赖于结果对象,结果对象的稳定性也决定了父窗体与子窗体关系的稳定性.下面是程序运行结果:
    注:提供的代码仅仅是功能演示,如果实际使用需要添加一些额外辅助代码(对象释放、取消事件订阅等).所有源代码一起转贴如下:
    Main.cs
    using System;
    using System.Windows.Forms;
    namespace WinParam
    {
    public class AppMain
    {
    /// <summary>
    /// 应用程序的主入口点.
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.Run(new frmMain());
    }
    }
    }
    MainForm.cs
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    namespace WinParam
    {
    /// <summary>
    /// Form1 的摘要说明.
    /// </summary>
    public class frmMain : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox txtCallResult;
    private System.Windows.Forms.Button btnCallChild;
    private System.Windows.Forms.TextBox txtEventResult;
    /// <summary>
    /// 必需的设计器变量.
    /// </summary>
    private System.ComponentModel.Container components = null;
    public frmMain()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }
    /// <summary>
    /// 清理所有正在使用的资源.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容.
    /// </summary>
    private void InitializeComponent()
    {
    this.txtCallResult = new System.Windows.Forms.TextBox();
    this.btnCallChild = new System.Windows.Forms.Button();
    this.txtEventResult = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    //
    // txtCallResult
    //
    this.txtCallResult.Location = new System.Drawing.Point(8, 48);
    this.txtCallResult.Multiline = true;
    this.txtCallResult.Name = "txtCallResult";
    this.txtCallResult.Size = new System.Drawing.Size(368, 176);
    this.txtCallResult.TabIndex = 5;
    this.txtCallResult.Text = "";
    //
    // btnCallChild
    //
    this.btnCallChild.Location = new System.Drawing.Point(264, 8);
    this.btnCallChild.Name = "btnCallChild";
    this.btnCallChild.Size = new System.Drawing.Size(112, 23);
    this.btnCallChild.TabIndex = 4;
    this.btnCallChild.Text = "Call ChildForm";
    this.btnCallChild.Click += new System.EventHandler(this.btnCallChild_Click);
    //
    // txtEventResult
    //
    this.txtEventResult.Location = new System.Drawing.Point(8, 8);
    this.txtEventResult.Name = "txtEventResult";
    this.txtEventResult.Size = new System.Drawing.Size(240, 21);
    this.txtEventResult.TabIndex = 3;
    this.txtEventResult.Text = "";
    //
    // frmMain
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(392, 237);
    this.Controls.Add(this.txtCallResult);
    this.Controls.Add(this.btnCallChild);
    this.Controls.Add(this.txtEventResult);
    this.Name = "frmMain";
    this.Text = "主窗体";
    this.ResumeLayout(false);
    }
    #endregion
    private void btnCallChild_Click(object sender, System.EventArgs e)
    {
    cResult r = new cResult();
    r.TextChanged += new TextChangedHandler(this.EventResultChanged);
    frmChild fc = new frmChild(r);
    fc.ShowDialog();
    txtCallResult.Text = "The Result is:rn" + r.Result1 + "rn" + r.Result2;
    }
    private void EventResultChanged(string s)
    {
    txtEventResult.Text = s;
    }
    }
    }
    ChildForm.cs
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    namespace WinParam
    {
    /// <summary>
    /// ChildForm 的摘要说明.
    /// </summary>
    public class frmChild : System.Windows.Forms.Form
    {
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.RadioButton optThree;
    private System.Windows.Forms.RadioButton optOne;
    private System.Windows.Forms.RadioButton optTwo;
    private System.Windows.Forms.TextBox txtResult2;
    private System.Windows.Forms.TextBox txtResult1;
    private System.Windows.Forms.Button cmdOk;
    /// <summary>
    /// 必需的设计器变量.
    /// </summary>
    private System.ComponentModel.Container components = null;
    public frmChild()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }
    private cResult r;
    public frmChild(cResult r):this()
    {
    this.r = r;
    }
    /// <summary>
    /// 清理所有正在使用的资源.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容.
    /// </summary>
    private void InitializeComponent()
    {
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.optThree = new System.Windows.Forms.RadioButton();
    this.optOne = new System.Windows.Forms.RadioButton();
    this.optTwo = new System.Windows.Forms.RadioButton();
    this.groupBox2 = new System.Windows.Forms.GroupBox();
    this.txtResult2 = new System.Windows.Forms.TextBox();
    this.txtResult1 = new System.Windows.Forms.TextBox();
    this.cmdOk = new System.Windows.Forms.Button();
    this.groupBox1.SuspendLayout();
    this.groupBox2.SuspendLayout();
    this.SuspendLayout();
    //
    // groupBox1
    //
    this.groupBox1.Controls.Add(this.optThree);
    this.groupBox1.Controls.Add(this.optOne);
    this.groupBox1.Controls.Add(this.optTwo);
    this.groupBox1.Location = new System.Drawing.Point(8, 8);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.Size = new System.Drawing.Size(144, 104);
    this.groupBox1.TabIndex = 5;
    this.groupBox1.TabStop = false;
    this.groupBox1.Text = "修改父窗体值";
    //
    // optThree
    //
      

  7.   


    接上
    this.optThree.Location = new System.Drawing.Point(16, 72);
    this.optThree.Name = "optThree";
    this.optThree.Size = new System.Drawing.Size(104, 16);
    this.optThree.TabIndex = 7;
    this.optThree.Text = "Show "Three"";
    this.optThree.CheckedChanged += new System.EventHandler(this.optThree_CheckedChanged);
    //
    // optOne
    //
    this.optOne.Location = new System.Drawing.Point(16, 24);
    this.optOne.Name = "optOne";
    this.optOne.Size = new System.Drawing.Size(104, 16);
    this.optOne.TabIndex = 6;
    this.optOne.Text = "Show "One"";
    this.optOne.CheckedChanged += new System.EventHandler(this.optOne_CheckedChanged);
    //
    // optTwo
    //
    this.optTwo.Location = new System.Drawing.Point(16, 48);
    this.optTwo.Name = "optTwo";
    this.optTwo.Size = new System.Drawing.Size(104, 16);
    this.optTwo.TabIndex = 5;
    this.optTwo.Text = "Show "Two"";
    this.optTwo.CheckedChanged += new System.EventHandler(this.optTwo_CheckedChanged);
    //
    // groupBox2
    //
    this.groupBox2.Controls.Add(this.txtResult2);
    this.groupBox2.Controls.Add(this.txtResult1);
    this.groupBox2.Location = new System.Drawing.Point(160, 8);
    this.groupBox2.Name = "groupBox2";
    this.groupBox2.Size = new System.Drawing.Size(136, 104);
    this.groupBox2.TabIndex = 6;
    this.groupBox2.TabStop = false;
    this.groupBox2.Text = "回传的结果";
    //
    // txtResult2
    //
    this.txtResult2.Location = new System.Drawing.Point(16, 64);
    this.txtResult2.Name = "txtResult2";
    this.txtResult2.TabIndex = 3;
    this.txtResult2.Text = "Result(2)";
    //
    // txtResult1
    //
    this.txtResult1.Location = new System.Drawing.Point(16, 32);
    this.txtResult1.Name = "txtResult1";
    this.txtResult1.TabIndex = 2;
    this.txtResult1.Text = "Result(1)";
    //
    // cmdOk
    //
    this.cmdOk.DialogResult = System.Windows.Forms.DialogResult.OK;
    this.cmdOk.Location = new System.Drawing.Point(224, 128);
    this.cmdOk.Name = "cmdOk";
    this.cmdOk.TabIndex = 7;
    this.cmdOk.Text = "确定(&O)";
    this.cmdOk.Click += new System.EventHandler(this.cmdOk_Click);
    //
    // frmChild
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(304, 157);
    this.Controls.Add(this.cmdOk);
    this.Controls.Add(this.groupBox2);
    this.Controls.Add(this.groupBox1);
    this.Name = "frmChild";
    this.Text = "子窗体";
    this.groupBox1.ResumeLayout(false);
    this.groupBox2.ResumeLayout(false);
    this.ResumeLayout(false);
    }
    #endregion
    private void optOne_CheckedChanged(object sender, System.EventArgs e)
    {
    r.ChangeText("One");
    }
    private void optTwo_CheckedChanged(object sender, System.EventArgs e)
    {
    r.ChangeText("Two");
    }
    private void optThree_CheckedChanged(object sender, System.EventArgs e)
    {
    r.ChangeText("Three");
    }
    private void cmdOk_Click(object sender, System.EventArgs e)
    {
    r.Result1 = txtResult1.Text;
    r.Result2 = txtResult2.Text;
    }
    }
    }
    cResult.cs
    using System;
    namespace WinParam
    {
    public delegate void TextChangedHandler(string s);
    public class cResult
    {
    public string Result1 = "";
    public string Result2 = "";
    public event TextChangedHandler TextChanged;
    public void ChangeText(string s)
    {
    if(TextChanged != null)
    TextChanged(s);
    }
    }
    }
      

  8.   

    http://hi.baidu.com/yudiefly/blog/item/77980433c2b700fd1b4cff95.html
      

  9.   

    你把你的登录信息,保存在一个全局类里面,如果登录成功,在from2里面就可以显示了。
      

  10.   

    肯能是在Form1登录时的ID号没有传给Form2。
      

  11.   

    在登陆窗体内将填写用户名的控件设置为公有public的,当登陆成功后在另一个页面接受这个控件内的值
    在form1中登陆成功则跳转Form1 F1=new Form1()
    F1.Owner=this;
    F1.show();在form2中接收这个用户名Form1 F1=(Form1)this.Owner;
    string name=F1.用户名控件.Text
      

  12.   

    哦,写了个错误的代码
    在form1中登陆成功则跳转中应为Form2 F2=new Form2()
    F2.Owner=this;
    F2.show();
      

  13.   

    其实很简单,在你的登录框form1类里加一个static变量用来保存登录人的ID,然后在用户详细信息框类根据该ID查询该用户详细信息即可。
    例子:
    在Login.cs中:   public static string _readID;  也可以设为private,然后用属性设置和访问。
    然后在登录的实现代码里加入:  _readID="你输入的用户的ID",然后再personInfo的初始化里用"select * from [tableName]"得到你想要的用户的具体信息。
    我的项目有点大,就不把代码贴出来了,这样应该够了吧。 
      

  14.   

    .net 技术交流群34938110 
      

  15.   

    .net 技术交流群34938110 
      

  16.   

    大家好,还是那个问题,现在把我的代码贴出来,大家给指导一下这是form1的:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    namespace login
    {
        public partial class Form1 : Form
        {
            public string strEnter;
            
            BaseClass.StudentClass studentclass = new login.BaseClass.StudentClass();
            
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {        }        private void button1_Click(object sender, EventArgs e)
            {
                string username = this.textBox1.Text;
                strEnter = studentclass.loginEnter(this.textBox1.Text);
                if (strEnter != "true")
                {
                    MessageBox.Show("不存在该考生,请重新输入!");
                }
                else
                {                new Form2().Show();
                    this.Visible = false;
                }
            }
        }
    }这是form2的:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace login
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
               
            }
                   private void Form2_Load(object sender, EventArgs e)
            {
                new GroupBox().Show();
                
            }       
            
        }
    }我采用类调用的方法
      

  17.   

    form1中:
      private void button1_Click(object sender, EventArgs e) 
            { 
                string username = this.textBox1.Text; 
                strEnter = studentclass.loginEnter(this.textBox1.Text); 
                if (strEnter != "true") 
                { 
                    MessageBox.Show("不存在该考生,请重新输入!"); 
                } 
                else 
                {                 new Form2().Show(); 
                    form2.username=username;                this.Visible = false; 
                } 
            } 
    form2中:
          public partial class Form2 : Form 
        { 
            public Form2() 
            { 
                InitializeComponent(); 
              
            } 
          private string _username;
          public string username
         {
            set 
          {_username=value;}
         }

            private void Form2_Load(object sender, EventArgs e) 
            { 
                new GroupBox().Show(); 
                
            }       
            
        } 
      

  18.   

    然后再form2中,只要显示_username这个变量就可以了!
      

  19.   

    你们怎么都这么麻烦呢SYSTEM.EXE.CONFIG 中设置数据库连接信息FORM2加载的时候从数据库读数据(DATAVIEW的)直接把数据给DATAGRIDVIEWdatagridview1.datasource = mydataview;这样不就OK了么
      

  20.   

    难道是没有加using 这种小疏忽?
      

  21.   

      //建方法根据条件,从数据库中读取信息
            public void FillListview()
            {
                string timu;
                string A;
                string B;
                string C;
                string D;
                string daan;
                string nandu;
                try
                {
                    // 查找题目的SQL语句
                    string Sql = string.Format("SELECT QuestionId, Question,OptionA,OptionB,OptionC,OptionD,Answer,Difficulty From Question where Difficulty='{0}'", Convert.ToInt32(cbonandu.Text));
                    SqlCommand command = new SqlCommand(Sql, DBHelper.connection);
                    SqlDataReader dataReader;
                    DBHelper.connection.Open();
                    dataReader = command.ExecuteReader();
                    lvijieguo.Items.Clear();
                    if (!dataReader.HasRows)
                    {
                        MessageBox.Show("对不起,没有你要找的题目!", "查找提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        //将结果循环写到ListView中
                        while (dataReader.Read())
                        {
                            //将从数据库中读取的题目,答案A,答案B、答案C、答案D、答案、难度 赋给相应的变量
                            timu = (string)dataReader["Question"];
                            A = (string)dataReader["OptionA"];
                            B = (string)dataReader["OptionB"];
                            C = (string)dataReader["OptionC"];
                            D = (string)dataReader["OptionD"];
                            daan = (string)dataReader["Answer"];
                            nandu = Convert.ToString(dataReader["Difficulty"]);
                            //创建一个listview项
                            ListViewItem listview = new ListViewItem(timu);                        listview.Tag = (int)dataReader["QuestionId"];                        //向listview中添加一个新项
                            lvijieguo.Items.Add(listview);                        //向但前向中添加子项
                            listview.SubItems.AddRange(new string[] { A, B, C, D, daan, nandu });
                            //FillListview();
                        }
                        dataReader.Close();
                        
                    }
                   
      

  22.   

    form1中:
    mainForm main = new mainForm(txtmailadd.Text);                main.Show();                this.Hide();Form2中:
     string username;//定用于接收用名的-----*        public mainForm(string username2)
            {
                this.username = username2;//接收并赋值----*            InitializeComponent();
                CheckForIllegalCrossThreadCalls=false;        }  
            this.toolStripStatusLabel1.Text = username;
      

  23.   

    嗯!别急慢慢来……
    学会自己解决问题!加asp.net群83847699