| |

VerySource

 Forgot password?
 Register
Search
View: 10|Reply: 8

Very vegetable problem, ask everyone to help! Urgent!

[Copy link]

4

Threads

5

Posts

6

Credits

Newbie

Rank: 1

Credits
6

 China

Post time: 2020-3-24 09:30:01
| Show all posts |Read mode
string select = "SELECT MIN (PhotoID) FROM Photo WHERE AlbumID = 1";
SqlCommand cmd = new SqlCommand (select, con);
object o = cmd.ExecuteScalar ();
con.Close ();
  if (o == null)
  {
        return 0;
  }
  else
 {
     return Convert.ToInt32 (o); Error: Object cannot be converted from DBNull to another type.

  }
After I set the breakpoint, I can be sure o is correct (o = 1), why is this ?? What should I do ???
Thank you everyone !!!
Reply

Use magic Report

1

Threads

5

Posts

6

Credits

Newbie

Rank: 1

Credits
6

 China

Post time: 2020-7-3 22:00:01
| Show all posts
Change to return Convert.ToInt32(o.ToString());
Reply

Use magic Report

4

Threads

5

Posts

6

Credits

Newbie

Rank: 1

Credits
6

 China

 Author| Post time: 2020-7-4 23:15:01
| Show all posts
No, return Convert.ToInt32(o.ToString()); There will be an exception of "the output string format is incorrect"!
Reply

Use magic Report

1

Threads

41

Posts

27

Credits

Newbie

Rank: 1

Credits
27

 China

Post time: 2020-7-5 00:15:01
| Show all posts
Try this

        object o = m_SqlCommand.ExecuteScalar();
        if (Convert.ToString(o)== "")
        {
            Response.Write("dfsdfds");
        }
        else
        {
            Response.Write(Convert.ToInt32(o));
        }
Reply

Use magic Report

0

Threads

50

Posts

17

Credits

Newbie

Rank: 1

Credits
17

 China

Post time: 2020-7-5 12:00:01
| Show all posts
else
{
     return Convert.ToInt32(o); Error: Object cannot be converted from DBNull to other types.

  }

is this OK
      return (int)o;
Reply

Use magic Report

1

Threads

5

Posts

6

Credits

Newbie

Rank: 1

Credits
6

 China

Post time: 2020-7-5 19:15:01
| Show all posts
What type are you returning?
Reply

Use magic Report

0

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-7-10 18:30:01
| Show all posts
object o = cmd.ExecuteScalar();
STRING P=O.TOSTRING();
con.Close();
  if (P == null)
  {
        return 0;
  }
  else
{
     return Convert.ToInt32(P); Error: Object cannot be converted from DBNull to other types.

  }
Reply

Use magic Report

1

Threads

41

Posts

27

Credits

Newbie

Rank: 1

Credits
27

 China

Post time: 2020-7-10 21:30:01
| Show all posts
Didn't I write the code above for you? If your sql statement has no value, the return value is empty, so your judgment is wrong, no problem according to what I wrote above
Reply

Use magic Report

0

Threads

3

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

Post time: 2020-7-12 14:30:01
| Show all posts
string o = Convert.ToString(cmd.ExecuteScalar());
con.Close();
  if (o == "")
  {
        return 0;
  }
  else
{
     return Convert.ToInt32(o);

  }
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list