System.NullReferenceException:对象引用未设置为对象实例IN行OleDbCommand cmd = new OleDbCommand(" select * from notice_2 where slno =" + ... [英] System.NullReferenceException: Object reference not set to an instance of an object IN line OleDbCommand cmd = new OleDbCommand("select * from notice_2 where slno = " +...

查看:144
本文介绍了System.NullReferenceException:对象引用未设置为对象实例IN行OleDbCommand cmd = new OleDbCommand(" select * from notice_2 where slno =" + ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;

public partial class notice_link : System.Web.UI.Page
{
    OleDbConnection cn = new OleDbConnection();
    OleDbCommand cmd = new OleDbCommand();
    OleDbDataAdapter da = new OleDbDataAdapter();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        cn.ConnectionString = "provider=sqloledb;data source=DILRAJ\\SQLEXPRESS;user id=sa;password=123;initial catalog=TIS";

        OleDbCommand cmd = new OleDbCommand("select * from notice_2 where slno = " + Request.QueryString["slno"].ToString(), cn);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);



        Label3.Text = ds.Tables[0].Rows[0].ItemArray[1].ToString();
        Label4.Text = ds.Tables[0].Rows[0].ItemArray[2].ToString();
        Label2.Text = ds.Tables[0].Rows[0].ItemArray[3].ToString();
        TextBox5.Text = ds.Tables[0].Rows[0].ItemArray[6].ToString();
        Label1.Text = ds.Tables[0].Rows[0].ItemArray[7].ToString();

       
     
        }

推荐答案

您的Request.QueryString [slno] .ToString()可能为null请检查
your Request.QueryString["slno"].ToString() might be null please check it


我将验证QueryString值,如下所示

i would validate for QueryString value like below
protected void Page_Load(object sender, EventArgs e)
{
    // check whether you have value for ParamName
    var value = Page.Request.QueryString["ParamName"];
    if(!string.IsNullOrEmpty(value))
    {
       LoadPageData(value);
    }else
    {
       // ParamName not given, what you want to do here?
    }
}


这篇关于System.NullReferenceException:对象引用未设置为对象实例IN行OleDbCommand cmd = new OleDbCommand(" select * from notice_2 where slno =" + ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆