Go Back   CodingForums.com > :: Server side development > ASP.NET

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-17-2013, 11:45 PM   PM User | #1
visi
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
visi is an unknown quantity at this point
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
visi is offline   Reply With Quote
Old 01-18-2013, 01:31 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
???

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.
Old Pedant is offline   Reply With Quote
Old 01-18-2013, 02:27 AM   PM User | #3
visi
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
visi is an unknown quantity at this point
I want to search into datagrid Surname , Name with one textbox
visi is offline   Reply With Quote
Old 01-18-2013, 12:53 PM   PM User | #4
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
alykins is offline   Reply With Quote
Old 01-18-2013, 08:17 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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 + '%'
__________________
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.
Old Pedant is offline   Reply With Quote
Old 01-18-2013, 08:37 PM   PM User | #6
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
I was merely opening the door

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

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

Last edited by alykins; 01-18-2013 at 08:39 PM..
alykins is offline   Reply With Quote
Old 01-18-2013, 09:11 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:22 PM.


Advertisement
Log in to turn off these ads.