简体   繁体   中英

How to check for a null value in database

I am working in asp.net. I want that if the user has not uploaded his profile picture then he should be redirected to a 'profile picture uploading page'. For this we must check the database to see if that user's User_ID exists. If it doesn't exist in the database it means he has not uploaded yet. Otherwise it means he has already uploaded a picture and the page loads all of the user's information. I have a table for saving display picture:

Table: ProfilePic

Columns= ID    DP     User_ID

To check whether his user_id exists in the database, I use this code:

  str = "select * from ProfilePic where Profile_ID=" + userid + ";";
  cmd = new SqlCommand(str, con);
  SqlDataReader reader = cmd.ExecuteReader();
  reader.Read();
  if (reader["Profile_ID"] != DBNull.Value) 
     {
         LoadInfo();
         LoadData();

       }
   else
       {
          Response.Redirect("DP.aspx");
       }

But it's still saying "Invalid attempt to read when no data is present". How do I resolve this problem?

You can check the reader for rows like the following example:

SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
    while (reader.Read())
    {
        Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
        reader.GetString(1));
     }
 }
 else
 {
     Console.WriteLine("No rows found.");
 }
 reader.Close();

You say, "if that user's User_ID is not existing in the database it means he has not uploaded yet." That means when you run your query, it will either return a record or it won't. If it doesn't, looking for a null value in one of the fields is doomed to failure.

I see from Bjorn's answer that SqlDataReader has a HasRows property. Use it.

First of all, avoid:

Select * ...

when all you want to do is check if a particular value exists or not in a particular table. It has no benefit with respect to what about you're looking for and isn't good from a performance standpoint as well.

Now, coming to your question, there is no use of select * from . All you need to know is if a userID exists in a table or not. You see, this is a true or false scenario. Your query should also be designed to reflect this. So, essentially your query itself should return true or false and based on that you should be able to apply your business rules. You can also make use of just SELECT COUNT() . So here's the way I'd suggest to design your query:

string str = "SELECT CAST(COUNT(Profile_Id) AS bit)  As 'DoesUserIDexist' FROM ProfilePic WHERE Profile_Id = 4";

You can also make use of:

string str = "SELECT COUNT(Profile_Id) As 'DoesUserIDexist' FROM ProfilePic WHERE Profile_Id = 4";

Also, its always a good practice to make use of try catch when you need to read from the database.

Essentially, your code can simplified so much as to this:

string query = "SELECT CAST(COUNT(Profile_Id) AS bit)  As 'DoesUserIDexist' FROM ProfilePic WHERE Profile_Id = 4";
cmd = new SqlCommand(query, con);
try
{
   SqlDataReader reader = command.ExecuteReader();
   if (reader.Read())
   {
       LoadInfo();
       LoadData();
   }
   else
   {
       Response.Redirect("DP.aspx");
   }
}
catch
{
   //Your exception handling mechanism here
}
finally
{
   //Dispose your ADO.NET related objects here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM