搜索框使用asp.net C# [英] search box using asp.net C#

查看:82
本文介绍了搜索框使用asp.net C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对象引用未设置为对象的实例。

描述:在执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.NullReferenceException:对象引用未设置为一个对象的实例。



来源错误:





第131行:protected void Button4_Click1(对象发送者,EventArgs e)

第132行:{

第133行:if(dr.HasRows)

第134行:{

第135行:dr.Read();





ASP代码



Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 131: protected void Button4_Click1(object sender, EventArgs e)
Line 132: {
Line 133: if(dr.HasRows)
Line 134: {
Line 135: dr.Read();


ASP code

<table><tr>
           <td>
       <asp:TextBox ID="TextBox1" runat="server" placeholder="Search id"></asp:TextBox>
               </td>
               <td>

                   <asp:Button ID="Button4" runat="server" Text="Go" OnClick="Button4_Click1" />
               </td>
           <td>
       <asp:Button ID="Button3" runat="server" Text="Delete" OnClientClick="javascript:validateCheckBoxes()" OnClick="Button3_Click"/>
       </td>
                   </tr>
           <tr>
               <td>

                   <asp:Label ID="Label1" runat="server" ForeColor="red"></asp:Label>
               </td>
           </tr>
       </table>









C#代码









C# code


private void bind()
    {

        cmd = new SqlCommand("select * from getezeeregister where username like '" + TextBox1.Text + "%'",con);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }

    protected void Button4_Click1(object sender, EventArgs e)
    {
        if(dr.HasRows)
        {
            dr.Read();
            bind();
            GridView1.Visible = true;
            TextBox1.Text = "";
            Label1.Text = "";

        }
        else
        {
            GridView1.Visible = false;
            Label1.Visible = true;
            Label1.Text = "Searched records "+TextBox1.Text+" is not avilable";

        }

    }

推荐答案

使用此更新的代码:



Use this updated Code:

private void bind()
    {

        cmd = new SqlCommand("select * from getezeeregister where username like '" + TextBox1.Text + "%'",con);
        da = new SqlDataAdapter(cmd);
        //ds = new DataSet();
        //da.Fill(ds);
        //GridView1.DataSource = ds;
    //GridView1.DataMember = ds.Tables[0];
        //GridView1.DataBind();

     ds = new DataSet();
         da.Fill(ds, "getezeeregister");

         // Bind the data table to the data grid
         dataGrid1.SetDataBinding(ds, "getezeeregister");
    }

    protected void Button4_Click1(object sender, EventArgs e)
    {
        if(dr.HasRows)
        {
            dr.Read();
            bind();
            GridView1.Visible = true;
            TextBox1.Text = "";
            Label1.Text = "";

        }
        else
        {
            GridView1.Visible = false;
            Label1.Visible = true;
            Label1.Text = "Searched records "+TextBox1.Text+" is not avilable";

        }

    }


这篇关于搜索框使用asp.net C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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