Format of local variable as input parameter in stored procedure in sql server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shanboy
    New Member
    • Jul 2010
    • 20

    Format of local variable as input parameter in stored procedure in sql server

    Stored procedure

    ALTER proc [dbo].[spSearchCombo](@searchBy nvarchar(50),@s earchKey nvarchar(50)) as
    select * from CD_DETAILS where @searchBy like '%' +@searchKey+ '%'


    I'm using ASP.net2.0 with c# to extract rows basedon search key from a text box and searchBy for the column to be searched.

    When i use column name instead of @searchBy which comes from value selected from a ddropdownlist i get the desired result . There seems to be a problem with format of @searchBy and i get a blank page.Plz help me out.

    Code:
    protected void btnSearch_Click(object sender, EventArgs e)
        {
                    string constring = ConfigurationManager.AppSettings.Get("con").ToString();
            SqlConnection conn = new SqlConnection(constring);
            conn.Open();
    
    
            SqlCommand cmdSP = new SqlCommand("spSearchCombo", conn);
            cmdSP.CommandType=CommandType.StoredProcedure;
    
            cmdSP.Parameters.Add(new SqlParameter("@searchBy",SqlDbType.NVarChar,50));
            cmdSP.Parameters["@searchBy"].Value=ddlSearchBy.SelectedValue.ToString();
    
            cmdSP.Parameters.Add(new SqlParameter("@searchKey", SqlDbType.NVarChar, 50));
            cmdSP.Parameters["@searchKey"].Value=txtSearch.Text.Trim();
    
    
            SqlDataAdapter da=new SqlDataAdapter(cmdSP);
            DataSet ds=new DataSet();
            da.Fill(ds);
    
            this.dgv1.DataSource=ds.Tables[0].DefaultView;
            dgv1.DataBind();
    
    
            
        }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Are you sure your stored procedure works?
    Have you tried it using Query Analyzer or something?

    -Frinny

    Comment

    • shanboy
      New Member
      • Jul 2010
      • 20

      #3
      It works in stored procedure but not the right result is got

      Hi Frinny
      Thanx for responding
      i have heard of query analyzer
      But haven't used it yet,plz guide me if u can

      My stored procedure works when I provide column name CD_NAME and win as search string in New query of SQL server 2005 and it gives a blank table with field names and no record

      [code]execute spSearchCombo CD_NAME,win

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Open Query Analyzer and try to execute the store procedure. Query Analyzer comes with MS SQL Server.

        -Frinny

        Comment

        Working...