Skip to content

HOW TO CODE FOR THE THIS STOREDPROCEDURE

parthiban karnan

Asked May 27, 2013, 2:45 AM EDT5 answers
Share
LinkedInX
I GOT A STORED PROCEDURE FOR RETRIEVING DATA FROM FILTERED SEARCH.. A DROPDOWN WITH FIRSTNAME,LASTNAME AND  EMAIL AND A TEXTBOX WHICH FILTERS WITH EXACT SEARCH..

BELOW IS SP.. HOW TO CODE FOR THIS...

IF I CHOOSE FIRTSNAME FROM DDL AND ENTER FIRSTNAME IN TEXTBOX.. IT HAS TO FILTER AND BIND WITH EXACT DETAILS IN GRIDVIEW...SAME AS  FOR LASTNAME AND EMAIL...


Create Procedure [dbo].[spSearch]
( 
@firstName nvarchar(50) = null,
@lastName nvarchar(50) = null,
@email nvarchar(50) = null
) 
AS 
BEGIN

If @firstName is not null and Len(@firstName )=0 Set @firstName = null
If @lastName is not null and Len(@lastName )=0 Set @lastName = null
If @email is not null and Len(@email )=0 Set @email = null

Select 
*
From userinf
Where 
( @firstName is null or firstName Like @firstName )
and ( @lastName is null or lastName Like @lastName )
and ( @email is null or email Like @email )
END 

GO

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Sunny  Sharma
    0
    so, where are you stuck?? I mean what's the problem?
    0
  • 0
    CODE NOT WORKING : WHEN I CHOOSE A ITEM FROM DROPDOWN AND ENTER SEARCH KEYWORD IN TEXTBOX.. DATA NOT DISPLAYING

    C#
     public void BindData()
            {
    
    
                //con.Open();
    
    
                SqlCommand cmd;
                cmd = new SqlCommand("spsearch", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;
              // cmd.Parameters.Add(new SqlParameter("@roll", SqlDbType.VarChar)).Value = DropDownList1.SelectedValue;
                cmd.Parameters.Add(new SqlParameter("@firstName", SqlDbType.VarChar)).Value = DropDownList1.SelectedValue;
                cmd.Parameters.Add(new SqlParameter("@lastname", SqlDbType.VarChar)).Value = DropDownList1.SelectedValue;
                cmd.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar)).Value = DropDownList1.SelectedValue;
    
    
               con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
               // DataSet ds = new DataSet();
                DataTable dt = new DataTable("USERINF");
    
    
                da.Fill(dt);
               con.Close();
                //con.Close();
              // if (ds.Tables[1].Rows.Count > 0)
                //{
    
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
    
    
                }
    0
  • Sunny  Sharma
    0
    have you tried implementing the code to search in TextBox_TextChanged event?
    0
  • Sunny  Sharma
    0
    Hi parthibhan,

    Below is the most efficient SP you should use to filter:
    --------------------------------------------------------------------------------------
    CREATE PROC [dbo].[SearchStudent](@ColType varchar(10), @data varchar(100)) as
    BEGIN
    IF LEN(@data)>0
    BEGIN
    IF @ColType='fname'
    SELECT * FROM students WHERE fname like ('%'+@data) or fname like ('%'+@data+'%') or fname like(@data+'%') or fname like @data ;
    ELSE IF @ColType='lname'
    SELECT * FROM students WHERE lname like ('%'+@data) or lname like ('%'+@data+'%') or lname like(@data+'%') or lname like @data ;
    ELSE
    SELECT * FROM students WHERE email like ('%'+@data) or email like ('%'+@data+'%') or email like(@data+'%') or email like @data ;

    END
    END

    GO

    (I've named this SP SearchStudent, use whatever you want it to be)
    -----------------------------------------------------------------------------------
    Here I assume you have three types of input: first name, last name and email. You can just pass the input type and data you want to search for that type that I've demonstrated in above SP.

    EXEC SearchStudent 'fname','ra'  
    the statement above will list all the entries in firstname containing text 'ra'.



    Happy Coding :)


    Please don't forget to accept this as answer if it helps!
    Thanks
    0
  • 1 short reply