总出现“添加失败未将对象引用设置到对象的实例”的提示,请大家帮忙指点一下!
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "" && textBox4.Text == "" && textBox5.Text == "" && textBox6.Text == "" && textBox7.Text == "" && textBox8.Text == "" && textBox9.Text == "")
{
MessageBox.Show("内容有为空的项","提示");
}
try
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=mingdan.mdb"; //相对路径。tongxunlu.mdb存放于当前目录下,即debug文件夹下
OleDbConnection conn = new OleDbConnection(strConnection);
OleDbDataAdapter thisAdapter = new OleDbDataAdapter("select *from tongxunlu", conn);
OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter); //看似没有用,但却不可缺少。它可以根据指定的sql语句来完成 添加,删除,修改等操作
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "mingdan");
DataRow thisRow = thisDataSet.Tables["tongxunlu"].NewRow();
thisRow["name"] = textBox1.Text.Trim();
thisRow["sex"] = textBox2.Text.Trim();
thisRow["age"] = textBox3.Text.Trim();
thisRow["adgree"] = textBox4.Text.Trim();
thisRow["address"] = textBox5.Text.Trim();
thisRow["addnumber"] = textBox6.Text.Trim();
thisRow["E-mail"] = textBox7.Text.Trim();
thisRow["QQ"] = textBox8.Text.Trim();
thisRow["tel"] = textBox9.Text.Trim();
thisDataSet.Tables["tongxunlu"].Rows.Add(thisRow);
thisAdapter.Update(thisDataSet, "tongxunlu");
conn.Close(); MessageBox.Show("添加成功", "提示"); textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
}
catch (Exception ex)
{
MessageBox.Show("添加失败" + ex.Message, "提示");
} }
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "" && textBox4.Text == "" && textBox5.Text == "" && textBox6.Text == "" && textBox7.Text == "" && textBox8.Text == "" && textBox9.Text == "")
{
MessageBox.Show("内容有为空的项","提示");
}
try
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=mingdan.mdb"; //相对路径。tongxunlu.mdb存放于当前目录下,即debug文件夹下
OleDbConnection conn = new OleDbConnection(strConnection);
OleDbDataAdapter thisAdapter = new OleDbDataAdapter("select *from tongxunlu", conn);
OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter); //看似没有用,但却不可缺少。它可以根据指定的sql语句来完成 添加,删除,修改等操作
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "mingdan");
DataRow thisRow = thisDataSet.Tables["tongxunlu"].NewRow();
thisRow["name"] = textBox1.Text.Trim();
thisRow["sex"] = textBox2.Text.Trim();
thisRow["age"] = textBox3.Text.Trim();
thisRow["adgree"] = textBox4.Text.Trim();
thisRow["address"] = textBox5.Text.Trim();
thisRow["addnumber"] = textBox6.Text.Trim();
thisRow["E-mail"] = textBox7.Text.Trim();
thisRow["QQ"] = textBox8.Text.Trim();
thisRow["tel"] = textBox9.Text.Trim();
thisDataSet.Tables["tongxunlu"].Rows.Add(thisRow);
thisAdapter.Update(thisDataSet, "tongxunlu");
conn.Close(); MessageBox.Show("添加成功", "提示"); textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
}
catch (Exception ex)
{
MessageBox.Show("添加失败" + ex.Message, "提示");
} }
解决方案 »
- 抛砖引玉。自己写的工具类,有没有人能够使用泛型改写下,再贴出来。也欢迎大家拿去用,欢迎对代码做修改和指正。
- C# 方法调用
- c#编译生成的exe文件,是微软中间文件吗?是不是类似于java的字节码呢?
- 如何实现新弹出小窗口后,父窗口冻结,不可编辑,新窗口选择完成确定后,数据传至父窗体.
- datagrid 中插入出现的问题?
- 谢谢,大家指导!!!推荐! 再一次感谢!!!!
- 请问各位大侠,这几个模板是什么意思?100分不够再送
- Listview CheckBoX 挡住数据项
- 问题:如何在ListBox里面显示多列
- 有关线程问题!!
- 关于短信接口的问题 希望高手能帮忙下 跪求了。
- C#中如何把二进制字符串转化为二进制
--
select * from tongxunl加个空格
DataRow thisRow = thisDataSet.Tables["mingdan"].NewRow(); //数据集中你也要用别名mingdan
thisAdapter.Fill(thisDataSet,'"tongxunlu");
DataRow thisRow = thisDataSet.Tables["tongxunlu"].NewRow();
确实手误,但是改正之后又有新的错误提示“添加失败INSERT INTO语句的语法错误”