Question

I'am using Stored procedure and a method to search.I'am sending a parameter which named @tag.

   @tag varchar 

AS

SELECT top 50
comment_id, 
comment_post_id, 
comment_user_id, 
comment_text, 
comment_like, 
comment_dislike, 
comment_reply_to_id, 
comment_date,
UserId,
UserName,
profil_foto_kucuk,
profil_foto_buyuk,
post_etiket


FROM         
comment
inner join aspnet_Users on comment.comment_user_id = aspnet_Users.UserID
inner join profil on comment.comment_user_id = profil.profil_user_id
inner join post on comment.comment_post_id=post.post_id
WHERE

comment_text like '%'+ @tag +'%'
ORDER BY comment_date DESC

For example there is just one result for @tag= akdeniz in database. When I manually execute sp for comment_text like '%'+ akdeniz+'%'
returns one result. But when I send parameter from asp.net side returns all of values. Asp.net side;

  <SelectParameters>
    <asp:QueryStringParameter Name="tag" QueryStringField="tag"  Type="String"  />
    </SelectParameters>

Url;

http://localhost:52137/Search.aspx?tag=akdeniz

And method;

 public static List<Yorum> SearchYorumlariGetir(string tag)
    {
        List<Yorum> yorum_listesi = new List<Yorum>();
        try
        {
            SqlConnection baglanti = new SqlConnection(data_works.dbconnect());
            try
            {
                SqlCommand komut = new SqlCommand("sp_search_yorum_getir", baglanti);
                komut.CommandType = CommandType.StoredProcedure;
                komut.Parameters.AddWithValue("@tag", tag);
                Yorum yorum = null;
                baglanti.Open();
                SqlDataReader dr = komut.ExecuteReader(CommandBehavior.CloseConnection);
                try
                {
                    while (dr.Read())
                    {
                        yorum = new Yorum();
                        yorum.Yorum_ID = dr.GetInt32(dr.GetOrdinal("comment_id"));
                        yorum.Post_ID = dr.GetInt32(dr.GetOrdinal("comment_post_id"));
                        yorum.User_ID = dr.GetGuid(dr.GetOrdinal("comment_user_id"));
                        yorum.Yorum_UserName = dr.GetString(dr.GetOrdinal("UserName"));
                        yorum.Yorum_Text = dr.GetString(dr.GetOrdinal("comment_text"));
                        yorum.Yorum_Like = dr.GetInt32(dr.GetOrdinal("comment_like"));
                        yorum.Yorum_Dislike = dr.GetInt32(dr.GetOrdinal("comment_dislike"));
                        yorum.Yorum_ReplyTo_ID = dr.GetInt32(dr.GetOrdinal("comment_reply_to_id"));
                        yorum.Yorum_Date = dr.GetDateTime(dr.GetOrdinal("comment_date"));
                        yorum.Yorum_Post_Etiket = dr.GetString(dr.GetOrdinal("post_etiket"));
                        yorum.Yorum_Profil_Foto_Kucuk = dr.GetString(dr.GetOrdinal("profil_foto_kucuk"));
                        yorum.Yorum_Profil_Foto_Buyuk = dr.GetString(dr.GetOrdinal("profil_foto_buyuk"));
                        yorum_listesi.Add(yorum);
                    }
                    dr.Close();
                }
                finally
                {
                    if (dr != null)
                    {
                        ((IDisposable)dr).Dispose();
                    }
                }
            }
            finally
            {
                if (baglanti != null)
                {
                    ((IDisposable)baglanti).Dispose();
                }
            }
        }
        catch (Exception exception)
        {
            throw exception;
        }
        List<Yorum> yorums = yorum_listesi;
        return yorums;
    }

I couldn't understand that why happening.

Was it helpful?

Solution

When you have a varchar parameter to a stored procedure you need to specify a length, if not it will just use the first character.

eg:

@tag varchar(48)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top