菜鸟问题:未将对象引用设置到对象的实例

lihzsonny 2016-02-23 11:34:11
最近一直在尝试使用DataAdapter与DataSet,以事务的方式进行数据库的插入,但总出现如题错误,在网上也查了很多,也试着进行修改,但总是报错,实在是找不出问题所在了,只得来求助各位高手了,下面是源代码:
    protected void Button1_Click(object sender, EventArgs e)
{
string n_title, n_content, n_author, n_date,channel,lanmu,quotedfrom,quotedlink,subject,mysess;
n_title = tx_title.Text.Trim();
n_content = FCKeditor1.Value.ToString();
n_author = tx_author.Text.Trim();
n_date = tx_date.Text.Trim();
channel = tx_channel.Text;
lanmu = tx_lanmu.Text;
quotedfrom=tx_quotedfrom.Text;
quotedlink=tx_quotedlink.Text;
subject=tx_subject.Text.Trim();
mysess=Session["fid"].ToString();

OleDbConnection conn=DBConnection.getConn();
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter();
OleDbCommand insertCmd = new OleDbCommand();
if (FileUpload1.FileName == "" || FileUpload1.FileName == null || FileUpload1.FileName == "null")
{
insertCmd.CommandText= "insert into articles(title,author,quotedfrom,quotedlink,subject,[update],content,channel,lanmu,fid) values(?,?,?,?,?,?,?,?,?,?)";//我有选择地插入列值,没列出的列数据允许为空
insertCmd.CommandType=CommandType.Text;
insertCmd.Connection = conn;
insertCmd.Parameters.Add("?", OleDbType.VarWChar, 100, "title");
insertCmd.Parameters.Add("?", OleDbType.VarWChar, 10, "author");
insertCmd.Parameters.Add("?", OleDbType.VarWChar, 20, "quotedfrom");
insertCmd.Parameters.Add("?", OleDbType.VarWChar, 255, "quotedlink");
insertCmd.Parameters.Add("?", OleDbType.VarWChar, 10, "subject");
insertCmd.Parameters.Add("?", OleDbType.DBTimeStamp, 20,"update");
insertCmd.Parameters.Add("?", OleDbType.VarWChar, 8000, "content");
insertCmd.Parameters.Add("?", OleDbType.VarChar, 10, "channel");
insertCmd.Parameters.Add("?", OleDbType.VarChar, 20, "lanmu");
insertCmd.Parameters.Add("?", OleDbType.Char, 20, "fid");
adapter.InsertCommand=insertCmd;

conn.Open();
using (OleDbTransaction tran = conn.BeginTransaction(IsolationLevel.ReadCommitted))
{
adapter.InsertCommand.Transaction = tran;

try
{

//insert
DataRow updateRow = ds.Tables["articles"].NewRow();
updateRow["title"] = n_title;
updateRow["author"] = n_author;

updateRow["author"] = n_author;
updateRow["quotedfrom"] = quotedfrom;
updateRow["quotedlink"] = quotedlink;
updateRow["subject"] = subject;
updateRow["update"] = n_date;
updateRow["content"] = n_content;
updateRow["channel"] = channel;
updateRow["lanmu"] = lanmu;
updateRow["fid"]=Session["fid"];
ds.Tables["articles"].Rows.Add(updateRow);
adapter.Update(ds, "articles");

tran.Commit();
}
catch (Exception ex)
{
try
{
tran.Rollback();
}
catch (Exception exc)
{
JS.JScript.Alert(ex.Message);
}
//以下是我尝试找出问题的过程,填写表单的话它们均不为空
if (ds == null) {JS.JScript.Alert("ds对象为空!");}
if (string.IsNullOrEmpty(n_title)) {JS.JScript.Alert("n_title对象为空!");}
if (string.IsNullOrEmpty(channel)) {JS.JScript.Alert("channel对象为空!");}
if (string.IsNullOrEmpty(lanmu)) {JS.JScript.Alert("lanmu对象为空!");}
if (string.IsNullOrEmpty(n_author)) {JS.JScript.Alert("n_author对象为空!");}

if (string.IsNullOrEmpty(quotedfrom)) {JS.JScript.Alert("quotedfrom对象为空!");}
if (string.IsNullOrEmpty(quotedlink)) {JS.JScript.Alert("quotelink对象为空!");}
if (string.IsNullOrEmpty(subject)) {JS.JScript.Alert("subject对象为空!");}
if (string.IsNullOrEmpty(n_date)) {JS.JScript.Alert("n_date对象为空!");}
if (string.IsNullOrEmpty(n_content)) {JS.JScript.Alert("n_content对象为空!");}
if (conn == null) {JS.JScript.Alert("conn对象为空!");}
if (insertCmd == null) {JS.JScript.Alert("ds对象为空!");}
if (string.IsNullOrEmpty(mysess)) {JS.JScript.Alert("Session对象为空!");}

// JS.JScript.Alert(ex.Message);

}
}
conn.Close();

}
else{//省略}
...全文
836 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihzsonny 2016-02-24
  • 打赏
  • 举报
回复
引用 7 楼 danding_ge 的回复:
[quote=引用 6 楼 lihzsonny 的回复:] 我把代码减到原来的四分之一,但还是报:未将对象引用设置到对象的实例,文本框的值就剩两个了,不应该是获取不到文本框值的问题了,大家再麻烦看下:
    protected void Button1_Click(object sender, EventArgs e)
    {
        string n_title, n_content, n_author, n_date,channel,lanmu,quotedfrom,quotedlink,subject,mysess;
        n_title = tx_title.Text.Trim();
        n_author = tx_author.Text.Trim();
		
		OleDbConnection conn=DBConnection.getConn();
		DataSet ds = new DataSet();  
		OleDbDataAdapter adapter = new OleDbDataAdapter();
		OleDbCommand insertCmd = new OleDbCommand();
		  insertCmd.CommandText= "insert into articles([title],[author]) values(?,?)";
		  insertCmd.CommandType=CommandType.Text;
		  insertCmd.Connection = conn;
		  insertCmd.Parameters.Add("?", OleDbType.VarWChar, 100, "title");
		  insertCmd.Parameters.Add("?", OleDbType.VarWChar, 10, "author");
		  adapter.InsertCommand=insertCmd;
		  
		  conn.Open();
		  using (OleDbTransaction tran = conn.BeginTransaction(IsolationLevel.ReadCommitted))  
		 {  
         adapter.InsertCommand.Transaction = tran;  
  
         try  
         {  
             
			 //insert  
             DataRow updateRow = ds.Tables["articles"].NewRow();  
             updateRow["title"] = n_title;  
             updateRow["author"] = n_author;
			 
             ds.Tables["articles"].Rows.Add(updateRow);  
             adapter.Update(ds, "articles");  
  
             tran.Commit();  
         }  
         catch (Exception ex)  
         {  
             try  
             {  
                 tran.Rollback();  
             }  
             catch (Exception exc)  
             {  
                 JS.JScript.Alert(ex.Message);
             }  
             JS.JScript.Alert(ex.Message);//未将对象引用设置到对象的实例 
				
         }  
     }  
     conn.Close();  
	 
	 }
错误详细信息是啥啊?你确定"articles"这个table能获取到么?[/quote] 查了一天,发现还真没有articles这个表,加上了,如题错误消失!感谢各位!
公西雒 2016-02-23
  • 打赏
  • 举报
回复
 ds.Tables["articles"]
             updateRow["title"] 
             updateRow["author"]
             updateRow["quotedfrom"]  
             updateRow["quotedlink"]
             updateRow["subject"]
             updateRow["update"]
             updateRow["content"]
             updateRow["channel"]
             updateRow["lanmu"]
             updateRow["fid"]
这里面肯定有为NULL的。
正怒月神 版主 2016-02-23
  • 打赏
  • 举报
回复
报了什么错?
lihzsonny 2016-02-23
  • 打赏
  • 举报
回复
逻缉是这样:如果没有上传附件,则进行单纯的数据库插入操作,问题出在插入事务中。
公西雒 2016-02-23
  • 打赏
  • 举报
回复
FileUpload1.FileName == ""
像这种被点.出来的FileName,一定要先检查点.前面的类是否为null,此处很可能FileUpload1就为null。 在此处按F9加断点,调试一下,看下FileUpload1的值是不是null。 而且报错应该显示了出错行数,很容易找到啊!
公西雒 2016-02-23
  • 打赏
  • 举报
回复
我给你的是查错的方法,不一定是问题所在。
江南小鱼 2016-02-23
  • 打赏
  • 举报
回复
1、F5,单步调试下,把抛异常的代码定位出来。
Ny-6000 2016-02-23
  • 打赏
  • 举报
回复
这种错误很容易跟踪下,一 步步的,就知道到哪行出错 就找到根原了
Ny-6000 2016-02-23
  • 打赏
  • 举报
回复
误详细信息是啥啊?你确定"articles"这个table能获取到么? 即便是取到了表, 可以确认,一定能存在是下面引用的2个列名吗
公西雒 2016-02-23
  • 打赏
  • 举报
回复
引用 6 楼 lihzsonny 的回复:
我把代码减到原来的四分之一,但还是报:未将对象引用设置到对象的实例,文本框的值就剩两个了,不应该是获取不到文本框值的问题了,大家再麻烦看下:
    protected void Button1_Click(object sender, EventArgs e)
    {
        string n_title, n_content, n_author, n_date,channel,lanmu,quotedfrom,quotedlink,subject,mysess;
        n_title = tx_title.Text.Trim();
        n_author = tx_author.Text.Trim();
		
		OleDbConnection conn=DBConnection.getConn();
		DataSet ds = new DataSet();  
		OleDbDataAdapter adapter = new OleDbDataAdapter();
		OleDbCommand insertCmd = new OleDbCommand();
		  insertCmd.CommandText= "insert into articles([title],[author]) values(?,?)";
		  insertCmd.CommandType=CommandType.Text;
		  insertCmd.Connection = conn;
		  insertCmd.Parameters.Add("?", OleDbType.VarWChar, 100, "title");
		  insertCmd.Parameters.Add("?", OleDbType.VarWChar, 10, "author");
		  adapter.InsertCommand=insertCmd;
		  
		  conn.Open();
		  using (OleDbTransaction tran = conn.BeginTransaction(IsolationLevel.ReadCommitted))  
		 {  
         adapter.InsertCommand.Transaction = tran;  
  
         try  
         {  
             
			 //insert  
             DataRow updateRow = ds.Tables["articles"].NewRow();  
             updateRow["title"] = n_title;  
             updateRow["author"] = n_author;
			 
             ds.Tables["articles"].Rows.Add(updateRow);  
             adapter.Update(ds, "articles");  
  
             tran.Commit();  
         }  
         catch (Exception ex)  
         {  
             try  
             {  
                 tran.Rollback();  
             }  
             catch (Exception exc)  
             {  
                 JS.JScript.Alert(ex.Message);
             }  
             JS.JScript.Alert(ex.Message);//未将对象引用设置到对象的实例 
				
         }  
     }  
     conn.Close();  
	 
	 }
错误详细信息是啥啊?你确定"articles"这个table能获取到么?
lihzsonny 2016-02-23
  • 打赏
  • 举报
回复
我把代码减到原来的四分之一,但还是报:未将对象引用设置到对象的实例,文本框的值就剩两个了,不应该是获取不到文本框值的问题了,大家再麻烦看下:
    protected void Button1_Click(object sender, EventArgs e)
    {
        string n_title, n_content, n_author, n_date,channel,lanmu,quotedfrom,quotedlink,subject,mysess;
        n_title = tx_title.Text.Trim();
        n_author = tx_author.Text.Trim();
		
		OleDbConnection conn=DBConnection.getConn();
		DataSet ds = new DataSet();  
		OleDbDataAdapter adapter = new OleDbDataAdapter();
		OleDbCommand insertCmd = new OleDbCommand();
		  insertCmd.CommandText= "insert into articles([title],[author]) values(?,?)";
		  insertCmd.CommandType=CommandType.Text;
		  insertCmd.Connection = conn;
		  insertCmd.Parameters.Add("?", OleDbType.VarWChar, 100, "title");
		  insertCmd.Parameters.Add("?", OleDbType.VarWChar, 10, "author");
		  adapter.InsertCommand=insertCmd;
		  
		  conn.Open();
		  using (OleDbTransaction tran = conn.BeginTransaction(IsolationLevel.ReadCommitted))  
		 {  
         adapter.InsertCommand.Transaction = tran;  
  
         try  
         {  
             
			 //insert  
             DataRow updateRow = ds.Tables["articles"].NewRow();  
             updateRow["title"] = n_title;  
             updateRow["author"] = n_author;
			 
             ds.Tables["articles"].Rows.Add(updateRow);  
             adapter.Update(ds, "articles");  
  
             tran.Commit();  
         }  
         catch (Exception ex)  
         {  
             try  
             {  
                 tran.Rollback();  
             }  
             catch (Exception exc)  
             {  
                 JS.JScript.Alert(ex.Message);
             }  
             JS.JScript.Alert(ex.Message);//未将对象引用设置到对象的实例 
				
         }  
     }  
     conn.Close();  
	 
	 }

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧