检查Datareader是否为空

时间:2015-05-01 07:23:27

标签: c# sql-server null sqldatareader

请参阅下面的代码,我试图从表中读取数据,如果表中有任何行与条件匹配,则执行一些代码行,如果没有行,则执行其他代码集。

当我执行这段代码时,它抛出异常。 “无数据存在时读取无效” 关于到底出错的地方的任何建议。

public void Check_Last_RegNo()
    {
        try
        {

            con = new SqlConnection(cs.DBConn);
            con.Open();
            string ct = "SELECT Max(RegistrationNo) from tbl_StudentRegNo where (RegistrationNo Like'%" + label3.Text + "%')";
            cmd = new SqlCommand(ct);
            cmd.Connection = con;
            rdr = cmd.ExecuteReader();
            if (rdr[0]!=null && rdr[0]!=DBNull.Value)
            {

                int ii = 1;
                for (int max = 0; max < dataGridView1.Rows.Count; max++)
                {

                    label4.Text = ii.ToString("0000");
                    string concati = reg_no + label3.Text + label4.Text;
                    dataGridView1.Rows[max].Cells[2].Value = concati;
                    ii++;
                }


            }
            else
                {
                    while (rdr.HasRows)
                    {
                        label5.Text = (rdr[0]).ToString();
                        string lastregno = label5.Text;
                        string digits = lastregno.Substring(lastregno.Length - 4, 4);
                        label4.Text = digits.ToString();
                        int i = Convert.ToInt32(digits) + 1;
                        for (int max = 0; max < dataGridView1.Rows.Count; max++)
                        {

                            label4.Text = i.ToString("0000");
                            string concati = reg_no + label3.Text + label4.Text;
                            dataGridView1.Rows[max].Cells[2].Value = concati;
                            i++;
                        }
                    }
                }


        con.Close();

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

1 个答案:

答案 0 :(得分:0)

Finally I had to change my code a bit, and the exception was gone, change seems bit complicated but it worked. Here is the code.

try
        {

            con = new SqlConnection(cs.DBConn);
            con.Open();
            string ct = "SELECT Max(RegistrationNo) from tbl_StudentRegNo where (RegistrationNo Like'%" + lblshortcode.Text + "%')";
            cmd = new SqlCommand(ct);
            cmd.Connection = con;
            rdr = cmd.ExecuteReader();
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    lbllastregno.Text = (rdr[0]).ToString();
                    string lastregno = lbllastregno.Text;
                    if(rdr[0]!=DBNull.Value)
                    {
                        string current_serial_no = lastregno.Substring(lastregno.Length - 4, 4);
                        lblcurrentsno.Text = current_serial_no;
                        int next_serial_no = Convert.ToInt32(current_serial_no) + 1;
                        lblnextsno.Text = next_serial_no.ToString("0000");
                    }
                    else
                    {
                        lbllastregno.Text = reg_no + lblshortcode.Text + lbldigitformat.Text;
                    }

                }
            }
            else
            {
                lbllastregno.Text = reg_no + lblshortcode.Text + lbldigitformat.Text;
            }
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

Thank you for your valuable suggestions.