CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   ASP.NET (http://www.codingforums.com/forumdisplay.php?f=40)
-   -   Search Table of Databse with one Textbox (http://www.codingforums.com/showthread.php?t=285971)

visi 01-17-2013 11:45 PM

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

Old Pedant 01-18-2013 01:31 AM

???

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.

visi 01-18-2013 02:27 AM

I want to search into datagrid Surname , Name with one textbox

alykins 01-18-2013 12:53 PM

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();


Old Pedant 01-18-2013 08:17 PM

Ummm...I think *NOT QUITE*, Alykins.

*IF* I understand him correctly:
Quote:

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 + '%'


alykins 01-18-2013 08:37 PM

I was merely opening the door :)

Edit: I just re-read that and see what you meant now (the or clause)

Old Pedant 01-18-2013 09:11 PM

It's of course just a guess. I think a non-native-English translation issue. Maybe we will find out, maybe not.


All times are GMT +1. The time now is 08:43 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.