[英]“Cannot find table 0”
I have a button, with an event onClick(), when I press down the button it Should add in two different tables (Seuil and Notifier). 我有一个带有事件onClick()的按钮,当我按下按钮时,它应该添加到两个不同的表(Seuil和Notifier)中。 This is My code
这是我的代码
Button Code : 按钮代码:
//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);
My class connexion 我的班级联系
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];
}
}
The function that verifies the existance of a similar code 验证相似代码是否存在的函数
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;
}
}
And the error 和错误
System.IndexOutOfRangeException: Cannot find table 0.
Ligne 61 : if (variable)
Ligne 62 : {
Ligne 63 : dataTable = dataSet.Tables[0];
Ligne 64 : }
Ligne 65 : }
The problem is you cant get a dataset when you update or insert a row in db, you will get it only when you select data, 问题是,当您在db中更新或插入行时无法获取数据集,只有在选择数据后才能获取数据集,
In this case your 在这种情况下
dataAdapter.Fill(dataSet);
DataAdapter.Fill方法(数据集);
in 在
charger
充电器
function will be empty 函数将为空
hence it throws a error when you try to access the 1st table at 0th index 因此,当您尝试在第0个索引处访问第一个表时,它将引发错误
I found it, I just don't know why I didn't pay attention to it, the probleme was a small 'true' that had to be changed with a 'false' in my query 我找到了它,我只是不知道为什么我不注意它,问题是一个小的“ true”,必须在查询中将其更改为“ false”
con.charger("insert into Seuil values('DropDownList1.selectedItem', 'DropDownList2.selectedItem', '" + txtQteMin.Text + "', '" + txtQteMax.Text + "')", FALSE);
Thank you very much for your time, help and effort ^_^ <3 非常感谢您的时间,帮助和努力^ _ ^ <3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.