...

View Full Version : Search Table of Databse with one Textbox



visi
01-18-2013, 12:45 AM
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, 02: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, 03:27 AM
I want to search into datagrid Surname , Name with one textbox

alykins
01-18-2013, 01:53 PM
sql


create proc usp_myDemo
@Surname varchar()
AS

SELECT [fieldA], [fieldB], ... [fieldN]
FROM tbl_Demo
WHERE [SURNAME] = @Surname



somewhere in aspx page


<asp:TextBox runat="server" ID=txtSur></asp:TextBox>
............
<asp:GridView runat="server" ID=gr></asp:GridView>


somewhere in C#


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, 09:17 PM
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:


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:


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, 09: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, 10:11 PM
It's of course just a guess. I think a non-native-English translation issue. Maybe we will find out, maybe not.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum