C# &引用;找不到表0“;

C# &引用;找不到表0“;,c#,asp.net,sql,C#,Asp.net,Sql,我有一个按钮,带有一个event onClick(),当我按下按钮时,它应该添加到两个不同的表中(Seuil和Notifier)。这是我的密码 按钮代码: //Here I instanciate my Connexion class Where I fill the data table and the data set con.seConnecter(); //Here I verify if the table Seuil has already a similar record

我有一个按钮,带有一个event onClick(),当我按下按钮时,它应该添加到两个不同的表中(Seuil和Notifier)。这是我的密码

按钮代码:

//Here I instanciate my Connexion class Where I fill the data table and the data set

con.seConnecter();

//Here I verify if the table Seuil has already a similar record 

if (con.verifierExistance("Seuil", "idSerieMateriel", DropDownList1.selectedItem) &&
    con.verifierExistance("Seuil", "idMagasin", DropDownList2.selectedItem))
{

// If exist, do an update

    con.charger("update Seuil set qteMin ='" + txtQteMin.Text + "', qteMax ='" + txtQteMax.Text + "' where idMagasin='"+DropDownList1.selectedItem+"' and idSerieMateriel ='" + DropDownList2.selectedItem + "'", true);
}
else
{
    //if not Insert a new one
    con.charger("insert into Seuil values('DropDownList1.selectedItem', 'DropDownList2.selectedItem', '" + txtQteMin.Text + "', '" + txtQteMax.Text + "')", true);
}

//and in the end, it should add a row in the table 'notifier'
con.charger("insert into Notifier(matricule, qteMin, qteMax, typeMateriel, Serie, idMagasin, date) values('10', '" + txtQteMin.Text + "', '" + txtQteMax.Text + "', '" + cmbType.SelectedItem + "', '" + cmbSerie.SelectedItem + "', '1', '" + DateTime.Today + "')", true);
我的班级关系

public void charger(string query, bool variable)
{
    commande = new SqlCommand(query, connexion);
    dataAdapter = new SqlDataAdapter(commande);
    dataSet = new DataSet();
    dataAdapter.Fill(dataSet);
    if (variable)
    {
        dataTable = dataSet.Tables[0];
    }
}
验证类似代码是否存在的函数

public Boolean verifierExistance(string tableName, string primaryKey, string textBoxValue)
{
    charger("select * from " + tableName+ " where " + primaryKey+ " ='" + textBoxValue+ "'", true);
    if (dataTable.Rows.Count == 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}
错误呢

System.IndexOutOfRangeException: Cannot find table 0.

Ligne 61 :         if (variable)
Ligne 62 :         {

Ligne 63 :             dataTable = dataSet.Tables[0];
Ligne 64 :         }
Ligne 65 :     }

问题是在数据库中更新或插入行时无法获取数据集,只有在选择数据时才能获取数据集

在这种情况下,您的

dataAdapter.Fill(数据集)

充电器

函数将为空


因此,当您尝试访问第0个索引处的第一个表时,它会抛出一个错误。我找到了它,我只是不知道为什么我没有注意到它,问题是一个小的“真”,必须在查询中用“假”来更改

con.charger("insert into Seuil values('DropDownList1.selectedItem', 'DropDownList2.selectedItem', '" + txtQteMin.Text + "', '" + txtQteMax.Text + "')", FALSE);

非常感谢您的时间、帮助和努力^ ^使用调试器逐步完成代码并查看dataSet对象。您可能没有正确填充。数据是否在您的数据库中?如果是这样,那么查询的前半部分似乎无关紧要。如果不是,那么你应该首先关注这一部分。是的,我的表中有数据。让我发疯的是,当我检查我的数据库时,我发现表“Seuil”中的行已更新,但没有添加到表“Notifier”中的行。如果要查看数据是否已插入或更新,请使用commande.ExecuteNonQuery()@sabrina如果(dataset.Tables.Count>0){对表做点什么}否则{那里什么都没有,继续}@MatthewMartin我按照你说的做了,我得到了这个新错误“对象引用没有设置为对象的实例”,然后检查dataset(不一定是你的数据集的名称)是否为空。如果数据集不为null,则Tables属性将存在,如果没有表,则Count属性仍将存在。然后,您可以根据应用程序的详细信息来决定这意味着什么。(可能这是一条insert语句,没有按预期返回任何内容,可能这是一条insert语句,它通常会返回select@primar_键,但如果它没有返回,那么您就知道存在错误情况,这取决于特定的场景)