繁体   English   中英

数据存在,但我的程序显示“该行/列不存在数据”

[英]data exists but my program says "No data exists for the row/column"

请指导我如何解决这个问题。

    if (Convert.ToInt32(rd["B_Quan"].ToString()) > 3)
    {
        MessageBox.Show("Oops sobra tama na ");
    }
    cn.Close();

行/列不存在或包含空值。 在这种情况下,您将收到NullReferenceException因为null没有函数.ToString()

你可以检查一下:

if (rd.Table.Columns.Contains("B_Quan") && rd["B_Quan"] != null)
{
    if (Convert.ToInt32(rd["B_Quan"].ToString()) > 3)
    {
        MessageBox.Show("Oops sobra tama na ");
    }
}
cn.Close();

这有什么作用?

using (DataTable dt = new DataTable())
{
    dt.Load(rd);
    Console.WriteLine(dt.Rows.Count);
}
if (Convert.ToInt32(rd["B_Quan"].ToString()) > 3)
{
    MessageBox.Show("Oops sobra tama na ");
}

调试的时候能不能打开dt查看你的表? 是否有名为“B_Quan”的列?

我不知道您为什么删除问题中的代码。

为了这个问题,我从编辑历史记录中复制了您的代码。

我认为,您在代码中收到“不存在数据”的原因是因为您还没有使用SqlDataReader.Read() 有关详细信息,请参阅SqlDataReader 类

样本:

    while(rd.Read()) // You need to read first to get your data.
    {
        if (Convert.ToInt32(rd["B_Quan"].ToString()) > 3)
        {
            MessageBox.Show("Oops sobra tama na ");
        }
    }

你编码:

private void button3_Click(object sender, EventArgs e)
{
    if (cn.State == ConnectionState.Closed)
    {
        cn.Open();
    }  
    string student = "Select * From tbl_student";

    OleDbCommand loadstudent = new OleDbCommand(student, cn);
    loadstudent.Parameters.AddWithValue("@SId", textBox8.Text + "%");

    rd = loadstudent.ExecuteReader();

    if (rd.HasRows == true)
    {
        // ipapalabas ung labas
        while (rd.Read())
        {

        messageBox.Show("Oops sobra tama na ");

        }
    }
    else
    {
        MessageBox.Show("No record(s) found.");
    }
    cn.Close();

    if (cn.State == ConnectionState.Closed)
    {
        cn.Open();
    }
    string s = "Select * From Borrow";

    OleDbCommand sa = new OleDbCommand(s, cn);
    loadstudent.Parameters.AddWithValue("@SId", textBox8.Text + "%");

    rd = sa.ExecuteReader();

    while(rd.Read()) // You need to read first to get your data.
    {
        if (Convert.ToInt32(rd["B_Quan"].ToString()) > 3)
        {
            MessageBox.Show("Oops sobra tama na ");
        }
    }
    cn.Close();

    if (cn.State == ConnectionState.Closed) cn.Open();

    cmd = new OleDbCommand("Select * From Borrow", cn);
    rd = cmd.ExecuteReader();// ipapalitaw
    if (rd.HasRows == true)
    {
        // ipapalabas ung labas
        while (rd.Read())
        {
            if (Convert.ToInt32(rd["B_Quan"].ToString()) > 3)

            {
                MessageBox.Show("Oops sobra tama na ");
                return;

            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM