| |

VerySource

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

Very vegetable problem, ask everyone to help! Urgent!

[Copy link]

5

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 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

2

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

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

Use magic Report

5

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 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

60

Posts

37.00

Credits

Newbie

Rank: 1

Credits
37.00

 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

60

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 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

2

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

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

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 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

60

Posts

37.00

Credits

Newbie

Rank: 1

Credits
37.00

 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.00

Credits

Newbie

Rank: 1

Credits
4.00

 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-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list