Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Search Table of Databse with one Textbox

    How can i do this , to search table with one textbox in C# , how its the query for this, or if its possible give me a example like video , or just script explain , thanks a lot

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    ???

    Search what *PART* of a DB table?

    One of the fields in the table? Four of the fields in the table?

    You surely would *NEVER* search *ALL* the fields for a single value. Some of the fields are surely numbers, possibly even numbers that would mean nothing to a human being. Or they might be date or times.

    You need to be a lot more specific.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I want to search into datagrid Surname , Name with one textbox

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,776
    Thanks
    41
    Thanked 196 Times in 195 Posts
    sql
    Code:
    create proc usp_myDemo
        @Surname varchar()
    AS
    
    SELECT [fieldA], [fieldB], ... [fieldN]
    FROM tbl_Demo
    WHERE [SURNAME] = @Surname

    somewhere in aspx page
    Code:
    <asp:TextBox runat="server" ID=txtSur></asp:TextBox>
    ............
    <asp:GridView runat="server" ID=gr></asp:GridView>
    somewhere in C#
    Code:
    DataTable dt = new DataTable();
    SqlConnection conn = new SqlConnection(cnstr);
    using(conn)
    {
      SqlCommand cmd = new SqlCommand("usp_myDemo", conn);
      using(cmd)
      {
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(new SqlParameter("@Surname", txtSur.Text));
        // note this string is not cleaned- you need to clean it first 
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        dt.Load(dr);
        conn.Close();
       }
    }
    
    gr.DataSource = dt;
    gr.DataBind();

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    Ummm...I think *NOT QUITE*, Alykins.

    *IF* I understand him correctly:
    I want to search into datagrid Surname , Name with one textbox
    I think he means he has *TWO* fields in the table, Surname and Name and wants to search for a match in *EITHER* of them.

    So:
    Code:
    create proc usp_myDemo
        @name varchar()
    AS
    SELECT [fieldA], [fieldB], ... [fieldN]
    FROM tbl_Demo
    WHERE [SURNAME] = @name
    OR [NAME] = @name
    Or, if he means a more general search (e.g., Look for "ja" and find "James" or "Jane" or "Mojani") you would use:
    Code:
    create proc usp_myDemo
        @name varchar()
    AS
    SELECT [fieldA], [fieldB], ... [fieldN]
    FROM tbl_Demo
    WHERE [SURNAME] LIKE '%' + @name + '%'
    OR [NAME] LIKE '%' + @name + '%'
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,776
    Thanks
    41
    Thanked 196 Times in 195 Posts
    I was merely opening the door

    Edit: I just re-read that and see what you meant now (the or clause)
    Last edited by alykins; 01-18-2013 at 08:39 PM.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    It's of course just a guess. I think a non-native-English translation issue. Maybe we will find out, maybe not.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •