1

I have this query:

CREATE PROCEDURE [dbo].[spGetUserAvatar]
    ( @userid int)  
     AS 
      BEGIN
        select avatar
          from t_user
             where (userID = @userid) AND (avatar<>null)
       end

and this C# code to execute this query:

string ConnectionString = SafaConnectionString.ConnectionString;
    SqlConnection Connection = new SqlConnection(ConnectionString);
    SqlCommand cmd = new SqlCommand("spGetUserAvatar", Connection);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@userid", userid);
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter();
    try
    {
        cmd.Connection = Connection;
        Connection.Open();            
       sda.SelectCommand = cmd;
        sda.Fill(dt);
        if (dt.Rows.Count > 0)
            return dt;
        else
            return null;
    }

It worked fine,but now returns dt.Rows.Count == 0.In the event that, t_users.avatar Is not empty. Have I got the query wrong?

Gholamreza Asadi
  • 95
  • 1
  • 3
  • 10

1 Answers1

0

You cannot compare NULL with <> operator use IS operator to validate NULL values

where userID = @userid AND avatar is not null
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172