用户代码未处理NullReferenceexception/OleDbException……问题
一using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data .OleDb ;
public partial class ReadNews : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int newsid = Convert.ToInt32(Request.QueryString["id"].ToString());//出错…用户代码未处理NullReferenceexception 未将对象引用设置到对象的实例
String strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection +=@"Data Source=E:\Newweb\Database\News.mdb";
OleDbConnection myconn = new OleDbConnection(strConnection);
myconn.Open();
String sql = "select A.Title,A.Content,A.AddTime,A.Author,A.ReadNumber, B.ClassName from News A,NewClass B where B.ClassID=A.ClassID and A.ID=" + newsid;
OleDbCommand mycmd = new OleDbCommand(sql, myconn);
OleDbDataReader dr = mycmd.ExecuteReader();//出错……………………用户代码未处理OleDbException 标准表达式中数据类型不匹配
if (dr.Read())
{
lbl_class.Text = dr["ClassName"].ToString();
lbl_content.Text = dr["Content"].ToString();
lbl_title.Text = dr["Title"].ToString();
lbl_info.Text = "作者:" + dr["Author"].ToString() + " 时间:" + dr["AddTime"].ToString() + " 阅读:" + dr["ReadNumber"].ToString() + "次";
}
int number = Convert.ToInt32(dr["ReadNumber"].ToString()) + 1;
dr.Close();
myconn.Close();
UpdateNews(newsid,number);
}
protected void UpdateNews(int id, int n)
{
String strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=E:\Newweb\Database\News.mdb";
OleDbConnection myconn = new OleDbConnection(strConnection);
myconn.Open();
String sql = "Update News set ReadNumber=" + n + " where ID=" + id;
OleDbCommand mycmd = new OleDbCommand(sql, myconn);
mycmd.ExecuteNonQuery();
myconn.Close();
}
}
二
protected void BtnOK_Click(object sender, EventArgs e)
{
String action = Request.QueryString["Action"];
String strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=E:\Newweb\Database\News.mdb";
OleDbConnection myconn = new OleDbConnection(strConnection);
myconn.Open();
if (action == "Add")
{
int id = ddl.SelectedIndex+1;
String sql = "insert into News(Title,Author,ClassID,Content) values('" + TxtTitle.Text + "','" + TxtAuthor.Text + "','" + id + "','" + TxtContent.Text + "')";
//Response.Write(sql);
OleDbCommand mycmd = new OleDbCommand(sql, myconn);
OleDbDataReader result = mycmd.ExecuteReader();//有错…用户代码未处理OleDbException 索引或主关键字不能包含一个空(Null)值。
result.Close();
myconn.Close();
Bind_Gridview();
}
else if (action == "Edit")
{
int classid = Convert.ToInt32(ddl.SelectedValue) ;
int id = Convert.ToInt32(Request.QueryString["ID"]);
// Response.Write(id);
String sql = "Update News set Title='" + TxtTitle.Text + "',Author='" + TxtAuthor.Text + "',ClassID=" + classid + ",Content='" + TxtContent.Text + "' where ID="+id;
OleDbCommand mycmd = new OleDbCommand(sql, myconn);
OleDbDataReader result = mycmd.ExecuteReader();
result.Close();
myconn.Close();
Bind_Gridview();
}
}
出错在:
int newsid = Convert.ToInt32(Request.QueryString["id"].ToString());//出错…用户代码未处理
OleDbDataReader dr = mycmd.ExecuteReader();//出错……………………用户代码未处理OleDbException 标准表达式中数据类型不匹配
OleDbDataReader result = mycmd.ExecuteReader();//有错…用户代码未处理OleDbException 索引或主关键字不能包含一个空(Null)值。
急求帮助,万分感激! --------------------编程问答-------------------- 先判断Request.QueryString["id"]存在不? --------------------编程问答-------------------- 朋友,能不能再具体些呢 --------------------编程问答-------------------- int newsid = Convert.ToInt32(Request.QueryString["id"].ToString());//出错…用户代码未处理NullReferenceexception 未将对象引用设置到对象的实例
加判断
int newsid;
if(Request.QueryString["id"].ToString()==null)
{
//处理逻辑
}
else
{
newsid=Convert.ToInt32(Request.QueryString["id"].ToString());
} --------------------编程问答-------------------- int newsid;
if(Request.QueryString["id"]==null)
{
//处理逻辑
}
else
{
newsid=Convert.ToInt32(Request.QueryString["id"].ToString());
}
--------------------编程问答-------------------- 断点看一下newsid有值不? --------------------编程问答-------------------- 帮 顶 --------------------编程问答-------------------- 1.
int newsid=0;
if(Request.QueryString["id"] != null)
{
newsid = Convert.ToInt32(Request.QueryString["id"].ToString());
}
else
{
}
2.String action = Request.QueryString["Action"];也要同样处理:
String action;
if(Request.QueryString["Action"]!= null)
{
action = Request.QueryString["Action"].ToString();
}
else
{
}
int id = ddl.SelectedIndex+1; 也要判断一下ddl.SelectedIndex是不是存在?
断点判断一下,你的SQL语句到底是什么?//String sql ...就这句
补充:.NET技术 , C#