当前位置: 代码迷 >> C# >> 返回空值的调试方法解决思路
  详细解决方案

返回空值的调试方法解决思路

热度:94   发布时间:2016-05-05 03:00:54.0
返回空值的调试方法
调试时大概是this.libraryDataSet5.GetChanges()这个返回了null了吧,不知道该如何解决了,我把代码粘过来了,大神门帮俺瞅瞅哪有问题啊,还是数据库有问题啊,不懂~~~,先谢谢啦!!!
这是还书按钮下的代码(包括用到的函数)
private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "")
            {
                MessageBox.Show("借书证号,ISBN,图书ID输入完整!");
                return;

            }
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand("Book_Borrow", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter inReaderID = new SqlParameter("@in_ReaderID", SqlDbType.Char, 8);
            inReaderID.Direction = ParameterDirection.Input;
            inReaderID.Value = textBox1.Text.Trim();
            cmd.Parameters.Add(inReaderID);
            SqlParameter inISBN = new SqlParameter("@in_ISBN", SqlDbType.Char, 18);
            inISBN.Direction = ParameterDirection.Input;
            inISBN.Value = textBox2.Text.Trim();
            cmd.Parameters.Add(inISBN);
            SqlParameter inBookID = new SqlParameter("@in_BookID", SqlDbType.Char, 8);
            inBookID.Direction = ParameterDirection.Input;
            inBookID.Value = textBox3.Text.Trim();
            cmd.Parameters.Add(inBookID);
            //SqlParameter inLTime = new SqlParameter("@in_LTime", SqlDbType.DateTime);
            //inLTime.Direction = ParameterDirection.Input;
            //inLTime.Value = textBox4.Text.Trim();
            //cmd.Parameters.Add(inLTime);
            SqlParameter outReturn = new SqlParameter("@out_str", SqlDbType.Char, 30);
            outReturn.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(outReturn);
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show(outReturn.Value.ToString());

            }
            catch
            { MessageBox.Show("借书出错!"); }
            finally
            {
                conn.Close();
                button1_Click(null, null);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

            DataRow[] rows = this.GetSpecialRecord(this.textBox1.Text, this.textBox2.Text, this.textBox3.Text);
            if (rows != null)
            {
                foreach (DataRow drow in rows)
                {
                    int dday = System.DateTime.Today.DayOfYear - ((System.DateTime)drow["RTime"]).DayOfYear;
                    if (dday > 0)
                        if (MessageBox.Show(this.textBox1.Text + "客户你的" + this.textBox2.Text + "," + this.textBox3.Text + "图书已经过期" + Convert.ToString(dday)
                            + "天,罚 款" + Convert.ToString(dday / 10) + "元RMB", "过期", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
                        {
                            return;
                        }


                    drow.Delete();
                }
 
            }

            if (ReturnReader(this.textBox1.Text) && ReturnBook(this.textBox2.Text, this.textBox2.Text))
            {
                UpdateDataSet();
                dataGridView1.Refresh();
                MessageBox.Show(this.textBox1.Text + "归还图书" + this.textBox2.Text + "成功");
            }


        }
        public void UpdateDataSet()
        {
            LibraryDataSet5 objDataSetTemp = new LibraryDataSet5();
            objDataSetTemp = (LibraryDataSet5)(this.libraryDataSet5.GetChanges());
            try
            {
                this.UpdateDataSource(objDataSetTemp);
                this.libraryDataSet5.Merge(objDataSetTemp);
                this.libraryDataSet5.AcceptChanges();
            }
            catch (System.Exception E)
            {
                this.ErrorHandle(E);
            }
        }

        public void UpdateDataSource(LibraryDataSet5 Changerows)
        {
            SqlConnection conn = new SqlConnection(strcon);
            string sqlStrSelect = "delete [BookID],[ISBN],[BookName],[Publisher],[Price],[Ltime] from [RBL] where [ReaderID]='" + textBox1.Text.Trim() + "'";
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter(sqlStrSelect, conn);
                conn.Open();
                adapter.Update(Changerows);
            }
            catch (System.Exception E)
            {
                this.ErrorHandle(E);
            }
            finally
            {
                conn.Close();

            }
        }
        public DataRow[] GetSpecialRecord(string ReaderID, string ISBN, string BookID)
        {

            try
            {
                DataRow[] rows = this.libraryDataSet5.Tables["RBL"].Select("ReaderID= '" + ReaderID + "'" + "and ISBN ='" + ISBN + "'" + "and BookID='" + BookID + "'");
                return rows;
            }
            catch (Exception ex)
            {
                Console.WriteLine("GetSpecialExpert failed in ExpertAccess!" + ex.StackTrace.ToString());
                return null;
            }
        }
        private bool ReturnReader(string ReaderID)
        {
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand returnbook = new SqlCommand();
            returnbook.Connection = conn;
            returnbook.CommandType = CommandType.StoredProcedure;
            returnbook.CommandText = "Return_Reader";
            SqlParameter parinput = returnbook.Parameters.Add("@ReaderID", SqlDbType.Char);
            parinput.Direction = ParameterDirection.Input;
            parinput.Value = ReaderID;
            try
            {
                conn.Open();
                returnbook.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (System.Exception e)
            {
                this.ErrorHandle(e);
                conn.Close();
                return false;

            }
        }

        //private void dataGridView1_CurrentCellChanged(object sender, System.EventArgs e)
        //{
        //    this.textBox1.Text = this.dataGridView1.CurrentCell.ToString();
        //    this.textBox1.Refresh();
        //}
        private bool ReturnBook(string ISBN, string BookID)
        {
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand returnbook = new SqlCommand();
            returnbook.Connection = conn;
            returnbook.CommandType = CommandType.StoredProcedure;
            returnbook.CommandText = "Return_Book";
            SqlParameter parinput = returnbook.Parameters.Add("@ISBN", SqlDbType.Char);
            parinput.Direction = ParameterDirection.Input;
            parinput.Value = ISBN;
            SqlParameter input = returnbook.Parameters.Add("@BookID", SqlDbType.Char);
            input.Direction = ParameterDirection.Input;
            input.Value = BookID;
            try
            {
                conn.Open();
                returnbook.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (System.Exception e)
            {
                this.ErrorHandle(e);
                conn.Close();
                return false;

            }
        }
        private void ErrorHandle(System.Exception E)
        {
            MessageBox.Show(E.ToString());
        }

       
  执行时出现这种问题


该怎么调试呢?
------解决思路----------------------
没人能帮你解决,自己调试代码,一步步调,空指针问题,很明确的错误了,不难找到原因
------解决思路----------------------
引用:
调试时大概是this.libraryDataSet5.GetChanges()这个返回了null了吧,不知道该如何解决了,我把代码粘过来了


你调试时,不能确定 this.libraryDataSet5.GetChanges() 返回的到底是不是 null 吗?为什么说“大概”?

你想从第一步就不用动脑,让别人帮你猜测着结果值?
------解决思路----------------------
已经告诉你是151行那里dataset是null值……
------解决思路----------------------
引用:
我是新手,多理解,不懂你们说的一步步调是啥意思,设断点吗,知道这个,但是设断点调试具体方法又是怎样的,有哪位耐心讲下啊,万分感谢,高手多多指导啦~~~


“设断点调试具体方法又是怎样的”这样的话如果说多了,就成了梦魇了。你要表现出一丁点主动地自己去找资料、上网搜索的劲儿,而不是一味地躺在别人身上去忽悠别人给你写东西。
  相关解决方案