這裏是我的代碼:C#實體框架存儲的值返回null例外
protected void btnShow_Click(object sender, EventArgs e)
{
foreach (Control control in Panel1.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
if (string.IsNullOrEmpty(textBox.Text))
{
textBox.Style["visibility"] = "hidden";
}
// textBox.Enabled = false;
var id = from t in textBox.Text
where t != null
select textBox.ID;
var text = from t in textBox.Text
where t != null
select t;
foreach (var x in id)
{
Model.crossword insert = new Model.crossword();
insert.TextBoxID = x;
daoCrossword.Insert(insert);
}
foreach (var a in text)
{
Model.crossword insert = new Model.crossword();
insert.TextBoxValue = a.ToString();
daoCrossword.Insert(insert);
}
daoCrossword.Save();
}
}
}
daoCrossword是有它的CRUD代碼的類文件,我使用EF要做到這一點,我是新來的這一點,給我一個錯誤:System.NullReferenceException:對象引用未設置爲對象的實例。
CRUD類文件(部分):
public void Insert(Model.crossword exe)
{
context.crosswords.AddObject(exe);
}
public void Save()
{
context.SaveChanges();
}
你在哪裏得到錯誤?你嘗試過調試嗎?你的變量在錯誤發生時包含什麼值? –
我做過調試,x和a包含值,daoCrossword.Save()中的錯誤行;和context.crosswords.AddObject(exe);這是我的CRUD類文件 – user2376998
我已經編輯在上面,包括我的類文件 – user2376998