Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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. :(


XML
<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>



C#
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();
       
    }
Posted
Updated 25-Apr-12 20:27pm
v2
Comments
amolpatil2243 26-Apr-12 2:13am    
please elaborate your question????
heinhtataung 26-Apr-12 2:26am    
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. :(
tanweer 26-Apr-12 2:28am    
Hi, just add AutoGenerateColumns="True" in the Grid view.
I can see it is missing there....
heinhtataung 26-Apr-12 3:35am    
Yeach ! That's work .. :)
tanweer 26-Apr-12 2:31am    
and Do not write in bold, its means you are screaming

Hi,

Just remove quotes from the parameter name @Word.

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

instead of this:
C#
cmd.CommandText = "SELECT * FROM  Company WHERE CompanyName LIKE '@Word'";
 
Share this answer
 
Comments
heinhtataung 26-Apr-12 3:37am    
Yeach That's really helpful sir..!
So many thanks ... :)
C#
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();
        }


===================================================
XML
<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>
 
Share this answer
 
v2
Hi ,
Try this
C#
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();
   }

Best Regards
M.Mitwalli
 
Share this answer
 
Hi there,

Try this. This will work and solve your problem

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

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

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


All the best.
 
Share this answer
 
Add columns(bound field/template field) to your gridview and set headertext and datafield. datafield should be same as column name in your table


<asp:gridview id="gdView" runat="server" allowpaging="True" xmlns:asp="#unknown">
                                <columns>                                   
                                    <asp:boundfield headertext="Id" datafield="REGISTRATIONID">
                                    </asp:boundfield>
                                    <asp:boundfield datafield="Name" headertext="Name">
                                    </asp:boundfield>
                                    
                                </columns>
                                <emptydatatemplate>
                                    <asp:label id="lblEmptyMessage" runat="server" forecolor="Red" text="No Records Found"></asp:label>
                                </emptydatatemplate>
                                <headerstyle cssclass="GridHeader" />
                                <alternatingrowstyle cssclass="GridAltItem" />
                            </asp:gridview>
 
Share this answer
 
Comments
heinhtataung 26-Apr-12 3:36am    
Yeach That's work ! thanks... :)
hey didn't get you properly...first tell about template or column of grid where u want to bind your data...!!

here i am finding your grid with no item templates..
 
Share this answer
 
Comments
heinhtataung 26-Apr-12 2:22am    
Column fields are.. Company Name,Address, etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900