62,243
社区成员




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{//省略}
ds.Tables["articles"]
updateRow["title"]
updateRow["author"]
updateRow["quotedfrom"]
updateRow["quotedlink"]
updateRow["subject"]
updateRow["update"]
updateRow["content"]
updateRow["channel"]
updateRow["lanmu"]
updateRow["fid"]
这里面肯定有为NULL的。FileUpload1.FileName == ""
像这种被点.出来的FileName,一定要先检查点.前面的类是否为null,此处很可能FileUpload1就为null。
在此处按F9加断点,调试一下,看下FileUpload1的值是不是null。
而且报错应该显示了出错行数,很容易找到啊! 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();
}