我是新手,想学着实现c/s模式的医院的报告查询功能,写完后运行发现最终用水晶报表输出结果,有时可以,有时就会报错“未处理nullreferenceexception,未将对象引用设置到对象的实例”这样的。
同样的功能我事先用asp.net实现了,现在用winform我弄了好半天都没找出来原因,请大家帮我看看!
 这是program.cs
   static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 login = new Form1();
            login.ShowDialog();
            if (login.DialogResult == DialogResult.OK)
            {
                Application.Run(new Form2());
            }
        }
系统登陆
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
                MessageBox.Show("用户名不能为空!,");
            else
            {
                if (check(textBox1.Text, textBox2.Text))
                {
                    this.DialogResult = DialogResult.OK;
                }
                else
                    MessageBox.Show("用户名密码不正确!");
            }
        }
        private bool check(string name, string password)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["bsrunconnstring"].ConnectionString);
            SqlCommand comm = new SqlCommand("Select * from SS_OPERATE_USER where YHDM=@YHDM and YHKL=@YHKL", conn);
            comm.Parameters.AddWithValue("@YHDM", textBox1.Text);
            comm.Parameters.AddWithValue("@YHKL", textBox2.Text);
            conn.Open();
            SqlDataReader dr = comm.ExecuteReader();
            if (dr.HasRows)
            {
                return true;
            }
            dr.Close();
            return false;
        }        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
}
datagridview数据列表
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (IsDate(maskedTextBox1.Text) && IsDate(maskedTextBox2.Text))
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["bsrunconnstring"].ConnectionString);
                SqlDataAdapter da = new SqlDataAdapter("select HIS_PATIENT_ID ,RIS_ACCNO ,RIS_PATIENT_ID ,brxm , case brxb when 1 then '男' else '女' end as 性别,RIS_REPORT_NAME,PATIENT_TYPE ,rq from ZY_BRRY,RISREPORT where (convert(varchar(64),ZYH))=HIS_PATIENT_ID and  rq between '" + maskedTextBox1.Text + "'  and  '" + maskedTextBox2.Text + "'   ", conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("查询日期内无有效数据!");
                }
            }
            else
                MessageBox.Show("不合法的日期!");
            for (int i = 0; i < dataGridView1.RowCount; i++)//序号
            {
                this.dataGridView1.Rows[i].Cells["序号"].Value = i + 1;
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["bsrunconnstring"].ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter("select HIS_PATIENT_ID ,RIS_ACCNO ,RIS_PATIENT_ID ,brxm , case brxb when 1 then '男' else '女' end as 性别 ,RIS_REPORT_NAME,PATIENT_TYPE ,rq from ZY_BRRY,RISREPORT where (convert(varchar(64),ZYH))=HIS_PATIENT_ID and  brxm='" + textBox1.Text + "'   ", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("查无此人!");
            }
            for (int i = 0; i < dataGridView1.RowCount; i++)//序号
            {
                this.dataGridView1.Rows[i].Cells["序号"].Value = i + 1;
            }
        }
        protected bool IsDate(Object Expression)//时间验证
        {
            if (Expression != null)
            {
                if (Expression is DateTime)
                {
                    return true;
                }
                if (Expression is string)
                {
                    DateTime time1;
                    return DateTime.TryParse((string)Expression, out time1);
                }
            }
            return false;
        }
//实现任意位置双击跳转至关联病人的“HIS_PATIENT_ID”查询具体的病人报告
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string s = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                Form3 windows3 = new Form3(s);
                windows3.Show();
        }
    }效果这样:
到了水晶报表了,我是用Dataset连接的水晶报表:public partial class Form3 : Form
    {
        string value;
        public Form3(string s)
        {
            value = s;
            InitializeComponent();
        }        
        private void Form3_Load(object sender, EventArgs e)
        {
                CrystalReport1 cr = new CrystalReport1();
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["bsrunconnstring"].ConnectionString);
                DataSet1 ds = new DataSet1();
                SqlDataAdapter da = new SqlDataAdapter("select KSMC,BRCH,ZYHM,RIS_PATIENT_ID ,BRXM ,BRXB ,RIS_REPORT_DESCRIPTION ,RIS_REPORT_IMPRESSION ,CSNY ,RQ ,AUDITOR from ZY_BRRY,RISREPORT ,GY_KSDM where (convert(varchar(64),ZYH)) = HIS_PATIENT_ID and KSDM=BRBQ and HIS_PATIENT_ID =" + "'" + value + "'", conn);
                if (ds.Tables.Count > 0)
                {
                    da.Fill(ds, "DataTable1");
                }
                cr.SetDataSource(ds);
                crystalReportViewer1.ReportSource = cr;
        }
    }实例DataGridView

解决方案 »

  1.   

       奇怪的是有时可以正常运行,我不断通过Datagridview点击查询就可能报这个错,感觉好像是随机...
       在 CrystalDecisions.Windows.Forms.PageControl.OnMouseMove(MouseEventArgs e)
       在 System.Windows.Forms.Control.WmMouseMove(Message& m)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       在 System.Windows.Forms.UserControl.WndProc(Message& m)
       在 CrystalDecisions.Windows.Forms.PageControl.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 WindowsFormsApplication1.Program.Main() 位置 C:\Documents and Settings\Administrator\my documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:行号 26
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
      

  2.   

    你这样写试试
    Form2 form2 = new Form2();
    Application.Run(form2);
      

  3.   

    不推荐你在Progrem文件里写那么多,打开Form2的事件最好写在Form1里
    Main方法只写这些最好:
    Application.Run(new Form1());
      

  4.   

    感谢几位的热心回答 我按照 guyuekkk11 的方法将
     Application.Run(new form2());
    修改为:
    Form2 form2 = new Form2();
     Application.Run(form2);
    结果真的好了!非常感谢;但是可以再问问这种问题的原因吗,这两种写法有什么不同吗,为什么就好了
      

  5.   

    好吧我发现在form2中重复双击跳转多次后,偶尔还会出现此问题,但是问题出现的几率下降了好多。还请朋友在帮帮我
      

  6.   

    检查你的Form2 里面放置了写什么控件,你给的代码中不包含控件部分的代码,也就是那个InitializeComponent();的代码,错误肯定是那个InitializeComponent()函数内部引发的,而那个函数的内容是视图设计器自动产生的代码,因此你必须检查你在拖拉控件的时候,是否进行了什么设置,而那设置访问的对象为null。