0

I cant read any data from database, could you please look at my code and find a problem?

 cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = @"SELECT ImageData, " 
                                 + " ContentType, " 
                                 + " ImageName " 
                                 + " FROM UsersImage "
                                 + " WHERE UserName = @UserName ";

                cmd.Parameters.Add(new SqlParameter("@UserName", ThreadUserName));

                reader = cmd.ExecuteReader();
            if (reader["ContentType"] != DBNull.Value)
            {
                ContentType = Convert.ToString(reader["@ContentType"]);
            }

            if (reader["ImageName"] != DBNull.Value)
            {
                ImageName = Convert.ToString(reader["@ImageName"]);
            }

            if (reader["ImageData"] != DBNull.Value)
            {
                ImageData = Convert.ToByte(reader["@ImageData"]);
            }

            int affectedRows = cmd.ExecuteNonQuery();
            if (affectedRows != 1)
            {

            }

            reader.Close();

My table name is: UsersImage.

My columns, ImageData(image), ContentType(nvarchar50), ImageName(nvarchar50), UserName (varchar20)

4
  • 1
    Do you get an exception? Does it work at all? It's not possible for us to debug your code by looking at it. Commented Aug 25, 2010 at 16:26
  • 1
    you definitely have rows in the db? you might also think about using an ORM - Castle ActiveRecord is super easy to get moving with - beats all this messing about with inline sql :) Commented Aug 25, 2010 at 16:26
  • 1
    Amen on ORM, have used Active Record and easy enough once you get going. I would say this type of coding should be retired ASAP. Is very inefficient and subject to pain (like you are feeling now) Commented Aug 25, 2010 at 16:28
  • exaption says that i have no entries in database, but i have it for username which is in parameter Commented Aug 25, 2010 at 16:38

2 Answers 2

3

Add in reader.Read().

 cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = @"SELECT ImageData, " 
                                 + " ContentType, " 
                                 + " ImageName " 
                                 + " FROM UsersImage "
                                 + " WHERE UserName = @UserName ";

                cmd.Parameters.Add(new SqlParameter("@UserName", ThreadUserName));

using (IDataReader reader = cmd.ExecuteReader())
{

    if (reader.Read())
    {
        if (reader["ContentType"] != DBNull.Value)
        {
            ContentType = Convert.ToString(reader["ContentType"]);
        }

        if (reader["ImageName"] != DBNull.Value)
        {
            ImageName = Convert.ToString(reader["ImageName"]);
        }

        if (reader["ImageData"] != DBNull.Value)
        {
            ImageData = Convert.ToByte(reader["ImageData"]);
        }

        int affectedRows = cmd.ExecuteNonQuery();
        if (affectedRows != 1)
        {

        }
    }
}

EDIT: I added in a using as well to replace the reader.Close().

Sign up to request clarification or add additional context in comments.

1 Comment

i find out that my only problem was that i read with @. it schould be without @ like (reader["ImageName"]) instead (reader["@ImageName"]). that it.
0
    string sql = @"SELECT ImageData, ContentType, ImageName FROM UsersImage WHERE UserName = @UserName";

    using (var cn = new SqlConnection("[YOUR CONNECTION STRING]"))
    using (var cmd = new SqlCommand(sql, cn))
    {
        // Set some properties on the cmd object
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@UserName", ThreadUserName);

        // Open the connection
        cn.Open();

        // Execute your command and get back a data reader
        using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
        {
            if (reader.HasRows)
            {
                reader.Read();

                if (reader["ContentType"] != DBNull.Value)
                {
                    ContentType = reader["ContentType"].ToString();
                }
                if (reader["ImageName"] != DBNull.Value)
                {
                    ImageName = reader["ImageName"].ToString();
                }
                if (reader["ImageData"] != DBNull.Value)
                {
                    ImageData = (byte[])reader["ImageData"];
                }
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.