【问题标题】:Searched data not loading to the data grid view in asp.net form?搜索到的数据未加载到 asp.net 表单中的数据网格视图?
【发布时间】:2021-01-07 06:13:00
【问题描述】:

当我点击搜索时,它将在表格上按名称搜索。(参考图 1)

Image 1

我的搜索代码

protected void srchbtn_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrWhiteSpace(this.TextBox1.Text))
            {
                Response.Write("<script>alert('Search name is empty!')</script>");
               
            }
            else
            {

                con.Open();
                adapt = new SqlDataAdapter("select Convert(nvarchar(10),Date,121) as Date,Department,KaizenLead,Type,Observation,Onetimereapeat,Typeofwaste,Rootcase,ImageBefore,ImageAfter,Before,After,Improvement,Savings,Impleteam,validateby,Rootcase,safetyadmin,convert(nvarchar(10),Implementationdate,121) as Implementationdate,Status,Reason,FinanceRef  from GembaKaizen where KaizenLead like '" + TextBox1.Text + "'", con);
                dt = new DataTable();
                adapt.Fill(dt);
                gv_case.DataSource = dt;//gv_gase-grid view name
                con.Close();

                
            }
           
        }
        catch(SqlException)
        {
            Response.Write("<script>alert('Database error!')</script>");
        }
    }

【问题讨论】:

    标签: c# mysql asp.net search datagridview


    【解决方案1】:

    我不确定我是否理解问题,但我可以看到您的代码存在两个问题:

    1. sql查询中的like子句需要%否则就是相等
    like '%" + TextBox1.Text + "%'"
    
    1. Sql 注入 始终在查询中添加用户输入作为参数,否则容易发生 sql 注入

    【讨论】:

      【解决方案2】:
                          con.Open();
                          adapt = new SqlDataAdapter("select Convert(nvarchar(10),Date,121) as Date,Department,KaizenLead,Type,Observation,Onetimereapeat,Typeofwaste,Rootcase,ImageBefore,ImageAfter,Before,After,Improvement,Savings,Impleteam,validateby,Rootcase,safetyadmin,convert(nvarchar(10),Implementationdate,121) as Implementationdate,Status,Reason,FinanceRef  from GembaKaizen where KaizenLead like '" + TextBox1.Text + "'", con);
                          DataSet ds = new DataSet();
                          adapt.Fill(ds);
                          gv_case.DataSource = ds;
                          gv_case.DataBind();
                          con.Close();
      

      【讨论】:

      • 我找到了答案:)
      • 您的问题是您将 datatable 声明为 datatable() - 这意味着数据表数组,这将不起作用。但是 dataset() 已经是一个表数组。再一次,它应该被声明为 dataSet,而不是 dataset()。当您使用一个查询时,我会返回声明一个数据表,并且它更清晰,更好的是,如果您编写代码,那么您不需要(或想要)表限定符,例如 ds.tables( 0).Rows,你可以使用ds.Rows。因此,在墙上扔泥巴,从数据表更改为 dataset() 数组是可行的,最好在代码中清晰并使用数据表
      猜你喜欢
      • 2012-05-28
      • 2012-06-16
      • 2012-06-10
      • 1970-01-01
      • 2013-08-23
      • 2012-04-22
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多