Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 单击“提交”按钮时,对象引用未设置为对象的实例_Asp.net - Fatal编程技术网

Asp.net 单击“提交”按钮时,对象引用未设置为对象的实例

Asp.net 单击“提交”按钮时,对象引用未设置为对象的实例,asp.net,Asp.net,我已经在asp.net中创建了一个网页 在右边,我有一个列表框,当页面加载时,它会绑定数据库中的数据,其Default.aspx代码如下所示 <asp:ListBox ID="ListOfSql" runat="server" SelectionMode="Single" DataTextField="sql_name" DataValueField="sql_text" style="margin-left: 0px; width:auto; h

我已经在asp.net中创建了一个网页

在右边,我有一个列表框,当页面加载时,它会绑定数据库中的数据,其Default.aspx代码如下所示

  <asp:ListBox ID="ListOfSql" runat="server"  
        SelectionMode="Single" DataTextField="sql_name" DataValueField="sql_text" 
        style="margin-left: 0px; width:auto; height:300px" EnableViewState="true">
 </asp:ListBox>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
using System.IO;
using System.Threading;

public partial class _Default : System.Web.UI.Page
{
    System.Data.OracleClient.OracleConnection conn = new OracleConnection("Data Source = orcl; user id=*****;password = *****; unicode = true;");
    //OracleConnection conn = new OracleConnection("Data Source=orcl;Persist Security Info=True;User ID=system;password = ravi_123;Unicode=True");
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {    
        }
        loadSqlList();
        lblLastRefresh.Text = DateTime.Now.ToShortTimeString();
        lblDate.Text = DateTime.Now.ToShortDateString();
        Response.Cache.SetAllowResponseInBrowserHistory(false);
        //=============================================  Checking user logged in or not ===================================
        //if (Session["txtUserName"] == null)
        //{
        //    Response.Write("<Script Language ='JavaScript'> alert('Session Expired, please login again')</script>");
        //    conn.Close();
        //    Response.Redirect("login.aspx");
        //}
        //=============================================  Checking user logged in or not (END) =================================== 


        //=====================User Ip Tracer =======================

        string UserIp = Request.ServerVariables["http_x_forwarded_for"];
        string UserHost = Request.ServerVariables["http_X_forwarded_for"];
        string userMacAdd = Request.ServerVariables["http_x_forwarde_for"];
        if (string.IsNullOrEmpty(UserIp))
        {
            UserIp = Request.ServerVariables["Remote_ADDR"];
            UserHost = System.Net.Dns.GetHostName();
        }
        LblLoc.Text = UserIp;
        LblHostName.Text = UserHost;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Response.AddHeader("Pragma", "no-cache");
        //=====================User Ip Tracer code end =======================

        if (!IsPostBack)
        {
        }

    }
    protected void onclick_logout(object sender, EventArgs e)
    {
        conn.Close();
        Session.Clear();
        Response.Redirect("login.aspx");
    }
    protected void exportToExcel_Click(object sender, EventArgs e)
    {
        try
        {

            conn.Open();
            string sql;
            sql = txtQuery.Text.ToString();
            OracleDataAdapter da = new OracleDataAdapter(sql, conn);

            //================  User Details Data Insertion in DataBase Ends here ===============
            da.Fill(dt);
            ExportTableData(dt);
            Response.Write("<Script>alert('Ip Locked in DB')</Script>");

        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message.ToString();

        }
    }
    protected void btnClear_clik(object sender, EventArgs e)
    {
        txtQuery.Text = string.Empty;

    }
    private void ExportTableData(DataTable dtdata)
    {
        string attach = "attachment;filename="+ListOfSql.SelectedItem.Text.ToString()+".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attach);
        Response.ContentType = "application/ms-excel";

        if (dtdata != null)
        {
            foreach (DataColumn dc in dtdata.Columns)
            {
                Response.Write(dc.ColumnName + "\t");
            }
            Response.Write(System.Environment.NewLine);
            foreach (DataRow dr in dtdata.Rows)
            {
                for (int i = 0; i < dtdata.Columns.Count; i++)
                {
                    Response.Write(dr[i].ToString() + "\t");
                }
                Response.Write("\n");
            }
            Response.End();
        }
    }
    // References for this page 
   // http://forums.asp.net/t/1768549.aspx





    protected void btnPreSqlExe_Click(object sender, EventArgs e)
    {

        txtQuery.Text = ListOfSql.SelectedItem.Text.ToString();
    }

    private void loadSqlList()
    {
        if (!IsPostBack)
        {
            conn.Open();
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            string preSql = "select sql_name, sql_text from cn_sql_log order by sql_name";
            OracleDataAdapter da = new OracleDataAdapter(preSql, conn);
            da.Fill(ds);
            ListOfSql.DataSource = ds;
            ListOfSql.DataTextField = "Sql_Name";
            ListOfSql.DataValueField = "sql_Text";
            ListOfSql.DataBind();
            ListOfSql.SelectedIndex = 0;
            conn.Close();
        }
    }
}
我正在调用加载页面上的loadSqlList(),以便从列表框中的数据库中获取列表。 现在我有一个提交按钮

<asp:Button ID="btnPreSqlExe" runat="server" Text="Sumbit" 
                onclick="btnPreSqlExe_Click">
 </asp:Button>
现在,当我想在点击提交按钮,然后所选项目的文本应出现在文本框,在左侧 当我这样做时,我会得到一个错误页面

default.aspx.cs页面的完整代码如下所示

  <asp:ListBox ID="ListOfSql" runat="server"  
        SelectionMode="Single" DataTextField="sql_name" DataValueField="sql_text" 
        style="margin-left: 0px; width:auto; height:300px" EnableViewState="true">
 </asp:ListBox>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
using System.IO;
using System.Threading;

public partial class _Default : System.Web.UI.Page
{
    System.Data.OracleClient.OracleConnection conn = new OracleConnection("Data Source = orcl; user id=*****;password = *****; unicode = true;");
    //OracleConnection conn = new OracleConnection("Data Source=orcl;Persist Security Info=True;User ID=system;password = ravi_123;Unicode=True");
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {    
        }
        loadSqlList();
        lblLastRefresh.Text = DateTime.Now.ToShortTimeString();
        lblDate.Text = DateTime.Now.ToShortDateString();
        Response.Cache.SetAllowResponseInBrowserHistory(false);
        //=============================================  Checking user logged in or not ===================================
        //if (Session["txtUserName"] == null)
        //{
        //    Response.Write("<Script Language ='JavaScript'> alert('Session Expired, please login again')</script>");
        //    conn.Close();
        //    Response.Redirect("login.aspx");
        //}
        //=============================================  Checking user logged in or not (END) =================================== 


        //=====================User Ip Tracer =======================

        string UserIp = Request.ServerVariables["http_x_forwarded_for"];
        string UserHost = Request.ServerVariables["http_X_forwarded_for"];
        string userMacAdd = Request.ServerVariables["http_x_forwarde_for"];
        if (string.IsNullOrEmpty(UserIp))
        {
            UserIp = Request.ServerVariables["Remote_ADDR"];
            UserHost = System.Net.Dns.GetHostName();
        }
        LblLoc.Text = UserIp;
        LblHostName.Text = UserHost;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Response.AddHeader("Pragma", "no-cache");
        //=====================User Ip Tracer code end =======================

        if (!IsPostBack)
        {
        }

    }
    protected void onclick_logout(object sender, EventArgs e)
    {
        conn.Close();
        Session.Clear();
        Response.Redirect("login.aspx");
    }
    protected void exportToExcel_Click(object sender, EventArgs e)
    {
        try
        {

            conn.Open();
            string sql;
            sql = txtQuery.Text.ToString();
            OracleDataAdapter da = new OracleDataAdapter(sql, conn);

            //================  User Details Data Insertion in DataBase Ends here ===============
            da.Fill(dt);
            ExportTableData(dt);
            Response.Write("<Script>alert('Ip Locked in DB')</Script>");

        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message.ToString();

        }
    }
    protected void btnClear_clik(object sender, EventArgs e)
    {
        txtQuery.Text = string.Empty;

    }
    private void ExportTableData(DataTable dtdata)
    {
        string attach = "attachment;filename="+ListOfSql.SelectedItem.Text.ToString()+".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attach);
        Response.ContentType = "application/ms-excel";

        if (dtdata != null)
        {
            foreach (DataColumn dc in dtdata.Columns)
            {
                Response.Write(dc.ColumnName + "\t");
            }
            Response.Write(System.Environment.NewLine);
            foreach (DataRow dr in dtdata.Rows)
            {
                for (int i = 0; i < dtdata.Columns.Count; i++)
                {
                    Response.Write(dr[i].ToString() + "\t");
                }
                Response.Write("\n");
            }
            Response.End();
        }
    }
    // References for this page 
   // http://forums.asp.net/t/1768549.aspx





    protected void btnPreSqlExe_Click(object sender, EventArgs e)
    {

        txtQuery.Text = ListOfSql.SelectedItem.Text.ToString();
    }

    private void loadSqlList()
    {
        if (!IsPostBack)
        {
            conn.Open();
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            string preSql = "select sql_name, sql_text from cn_sql_log order by sql_name";
            OracleDataAdapter da = new OracleDataAdapter(preSql, conn);
            da.Fill(ds);
            ListOfSql.DataSource = ds;
            ListOfSql.DataTextField = "Sql_Name";
            ListOfSql.DataValueField = "sql_Text";
            ListOfSql.DataBind();
            ListOfSql.SelectedIndex = 0;
            conn.Close();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Data.OracleClient;
使用系统数据;
使用System.Data.SqlClient;
使用系统配置;
使用System.Windows.Forms;
使用System.IO;
使用系统线程;
公共部分类\u默认值:System.Web.UI.Page
{
System.Data.OracleClient.OracleConnection conn=新的OracleConnection(“数据源=orcl;用户id=****;密码=****;unicode=true;”;
//OracleConnection conn=neworacleconnection(“数据源=orcl;持久安全信息=True;用户ID=system;密码=ravi_123;Unicode=True”);
DataTable dt=新的DataTable();
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{    
}
loadSqlList();
lblLastRefresh.Text=DateTime.Now.ToShortTimeString();
lblDate.Text=DateTime.Now.ToShortDateString();
Response.Cache.SetAllowResponseInBrowserHistory(false);
//=========================================================================检查用户是否登录===================================
//if(会话[“txtUserName”]==null)
//{
//响应。写入(“警报(‘会话已过期,请重新登录’)”;
//康涅狄格州关闭();
//重定向(“login.aspx”);
//}
//=============================================================================================================================================================================================================================================
//===============================用户Ip跟踪器=======================
字符串UserIp=Request.ServerVariables[“http_x_forwarded_for”];
字符串UserHost=Request.ServerVariables[“http_X_forwarded_for”];
字符串userMacAdd=Request.ServerVariables[“http_x_forwarde_for”];
if(string.IsNullOrEmpty(UserIp))
{
UserIp=Request.ServerVariables[“Remote_ADDR”];
UserHost=System.Net.Dns.GetHostName();
}
LblLoc.Text=UserIp;
LblHostName.Text=UserHost;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
AddHeader(“Pragma”,“无缓存”);
//===============================用户Ip跟踪代码结束=======================
如果(!IsPostBack)
{
}
}
受保护的void onclick\u注销(对象发送方,事件参数e)
{
康涅狄格州关闭();
Session.Clear();
重定向(“login.aspx”);
}
受保护的void exportToExcel\u单击(对象发送方,事件参数e)
{
尝试
{
conn.Open();
字符串sql;
sql=txtQuery.Text.ToString();
OracleDataAdapter da=新的OracleDataAdapter(sql,conn);
//======================在数据库中插入的用户详细信息数据到此结束===============
da.填充(dt);
输出表数据(dt);
响应。写入(“警报('Ip锁定在DB')”;
}
捕获(例外情况除外)
{
lblError.Text=ex.Message.ToString();
}
}
受保护的void btnClear\u clik(对象发送方,事件参数e)
{
Text=string.Empty;
}
私有void ExportTableData(DataTable dtdata)
{
string attach=“attachment;filename=“+ListOfSql.SelectedItem.Text.ToString()+”.xls”;
Response.ClearContent();
AddHeader(“内容处置”,附件);
Response.ContentType=“应用程序/ms excel”;
如果(dtdata!=null)
{
foreach(dtdata.Columns中的数据列dc)
{
Response.Write(dc.ColumnName+“\t”);
}
Response.Write(系统、环境、换行符);
foreach(dtdata.Rows中的数据行dr)
{
对于(int i=0;i
如果未进行选择,则SQL.SelectedItem的列表将为空,因此调用
.Text
将导致错误

尝试将代码更改为以下内容:

protected void btnPreSqlExe_Click(object sender, EventArgs e)
{
    if(ListOfSql.SelectedItem != null)
    {
        txtQuery.Text = ListOfSql.SelectedItem.Text.ToString();
    }
}

这样做,错误页面将被删除,但我不会将listbox的文本值获取到位于左侧的textbox。当选择某个项目时,您是否会收到该错误?当列表中的某个项目未被选中且按钮处于预设置状态时,我的解决方案将解决您的问题