不要在网格视图中显示结果 [英] Don't Show the Result in my Grid View

查看:68
本文介绍了不要在网格视图中显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表(dbo.Company),该表具有字段(CompanyName,Address,Phone,Email等).
我想通过单击搜索按钮(btnSearch)从TextBox(txtWord)中搜索公司.
我尝试了..但最终Gridview(gdView)没有显示结果. :(


I have the table ( dbo.Company) which have the fields (CompanyName,Address,Phone,Email,etc).
I wanna search the Company from TextBox(txtWord) by clicking the Search Button(btnSearch).
I tried .. but finally the Gridview(gdView) don''t show the result. :(


<asp:TextBox ID="txtWord" runat="server"></asp:TextBox>
       <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
       <br />
       <asp:GridView ID="gdView" runat="server">
       </asp:GridView>







protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void BindGridData()
    {
        string constring = WebConfigurationManager.ConnectionStrings["ydConnectionString"].ToString();
        SqlConnection connection = new SqlConnection(constring);

        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.SelectCommand = cmd;
        cmd.Connection = connection;
        DataSet ds = new DataSet();


        cmd.CommandText = "SELECT * FROM  Company WHERE CompanyName LIKE '@Word'";
        cmd.Parameters.Add(new SqlParameter("@Word", SqlDbType.Text));
        cmd.Parameters["@Word"].Value = txtWord.Text + "%";

        try
        {
            connection.Open();
            da.Fill(ds);
        }
        catch (SqlException ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            connection.Close();
        }
        gdView.DataSource = ds.Tables[0]; 
        gdView.DataBind();

    }
    protected void btnSearch_Click(object sender,EventArgs e)
    {
        BindGridData();
       
    }

推荐答案



只需删除参数名称@Word中的引号即可.

使用此命令:
Hi,

Just remove quotes from the parameter name @Word.

Use this:
cmd.CommandText = "SELECT * FROM  Company WHERE CompanyName LIKE @Word";


代替这个:


instead of this:

cmd.CommandText = "SELECT * FROM  Company WHERE CompanyName LIKE '@Word'";


string connectionString = "Data Source=HP-PC; Initial Catalog=dbMahato; User id=sa; password=softech";
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        try
        {
            string psql = "Select *from tblEmployee";
            SqlCommand cmd = new SqlCommand(psql, con);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable ds = new DataTable();
            da.Fill(ds);
            ds.Columns.Add("SNo");
            for (int i = 0; i <= ds.Rows.Count - 1; i++)
            {
                ds.Rows[i]["SNo"] = i + 1;
            }
            gvEmployeeDetails.DataSource = ds;
            gvEmployeeDetails.DataBind();



        }
        catch (Exception ex) { throw ex; }
        finally
        {
            con.Close();
            con.Dispose();
        }



================================================== =



===================================================

<asp:GridView ID="gvEmployeeDetails" runat="server" AutoGenerateColumns="false" DataKeyNames="empID"

                                        Style="margin-left: 0px" CssClass="fullTable zebraStrips" Width="100%"

                                        AllowPaging="True" PageSize="25" GridLines="Horizontal" >
                                        <Columns>
                                            <asp:TemplateField HeaderText="S.No">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblSNo" runat="server" Text='<%#Bind("SNo") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Emp No.">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblEmpNo" runat="server" Text='<%#Bind("empID") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Name">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblRegistrationNo" runat="server" Text='<%#Bind("empName") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Bank A/C">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblName" runat="server" Text='<%#Bind("bankAcNo") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Gender" Visible="false">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblOpdNo" runat="server" Text='<%#Bind("gender") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Department">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblCitizenshipNo" runat="server" Text='<%#Bind("deptName") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Designation">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblDistrict" runat="server" Text='<%#Bind("desigName") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="DOB(NEP)">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblIssueLetter" runat="server" Text='<%#Bind("dob_Nep") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Edit/Delete" ItemStyle-HorizontalAlign="Center">
                                                <ItemTemplate>
                                                    <asp:LinkButton ID="lnkEdit" runat="server" CommandName="EditThis" Text="Edit" CommandArgument='<%#Bind("empID") %>'></asp:LinkButton>
                                                    &nbsp;
                                                    <asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" Text="Delete"

                                                        CommandArgument='<%#Bind("empID") %>' OnClientClick="return confirm('Are you confirm to Delete?')"></asp:LinkButton>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                         <EmptyDataTemplate>
                                            No Records Fetched!
                                        </EmptyDataTemplate>
                                    </asp:GridView>



试试这个
Hi ,
Try this
public void BindGridData()
   {
       using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))        {
           con.Open();
           using (SqlCommand cmd = new SqlCommand("select * from  dbo.Items where Item_name LIKE '%' + @aaa +'%' ", con))
           {
               try
               {
                   cmd.Parameters.AddWithValue("@aaa", txtWord.Text);
                   SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                   DataTable dt = new DataTable();
                   adpt.Fill(dt);
                   gdView.DataSource = dt;
                   gdView.DataBind();
               }
               catch (Exception ex)
               {
                   //
               }
           }
       }
   }

   protected void btnSearch_Click(object sender, EventArgs e)
   {
       BindGridData();
   }


最好的问候
米特瓦里(M.Mitwalli)


Best Regards
M.Mitwalli


这篇关于不要在网格视图中显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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