| |

VerySource

 Forgot password?
 Register
Search
View: 2741|Reply: 28

The specified conversion is invalid !! Online etc ...

[Copy link]

1

Threads

16

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-2-17 14:00:01
| Show all posts |Read mode
The following piece of code appears:
"System.InvalidCastException: The specified conversion is invalid."
Troubleshooting tips:
"When a number is coerced, the value must be a number less than infinity
Make sure the source type can be converted to the target type "

Ask for help, give points immediately, if the explanation is clear to everyone, I am willing to add points!

public IList <Bbs_PostDetailsInfo> GetPostDetails (string postid)
{
    IList <Bbs_PostDetailsInfo> postlist = new List <Bbs_PostDetailsInfo> ();
    strSql = "SELECT * FROM V_Bbs_PostDetails WHERE Post_Id =" + postid;
    sdr = GetDataReader (strSql);
    while (sdr.Read ())
    {
            postlist.Add (new Bbs_PostDetailsInfo (sdr.GetInt32 (0), sdr.GetString (1), sdr.GetString (2), sdr.GetInt32 (3),
            sdr.GetString (4), sdr.GetString (5), sdr.GetDateTime (6), sdr.GetInt32 (7), sdr.GetString (8), sdr.GetDateTime (9),
            sdr.GetString (10), sdr.GetString (11), sdr.GetString (12), sdr.GetString (13), sdr.GetString (14), sdr.GetString (15),
            sdr.GetInt32 (16), sdr.GetInt32 (17), sdr.GetInt32 (18), sdr.GetInt32 (19), sdr.GetInt32 (20), sdr.GetInt32 (21),
            sdr.GetString (22), sdr.GetDateTime (23), sdr.GetInt32 (24), sdr.GetInt32 (25), sdr.GetString (26), sdr.GetInt32 (27), sdr.GetInt32 (28), sdr .GetInt32 (29)));
    }
    sdr.Close ();
    sdr.Dispose ();
    return postlist;
}
Reply

Use magic Report

1

Threads

16

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

 Author| Post time: 2020-4-19 23:00:01
| Show all posts
To avoid null values ​​I changed to:

public IList <Bbs_PostDetailsInfo> GetPostDetails (string postid)
        {
            IList <Bbs_PostDetailsInfo> postlist = new List <Bbs_PostDetailsInfo> ();
            strSql = "SELECT * FROM V_Bbs_PostDetails WHERE Post_Id =" + postid;
            sdr = GetDataReader (strSql);
            while (sdr.Read ())
            {
                postlist.Add (new Bbs_PostDetailsInfo (sdr.IsDBNull (0)? 0: sdr.GetInt32 (0), sdr.IsDBNull (1)? "": sdr.GetString (1), sdr.IsDBNull (2)? "": sdr.GetString (2), sdr.IsDBNull (3)? 0: sdr.GetInt32 (3),
                    sdr.IsDBNull (4)? "": sdr.GetString (4), sdr.IsDBNull (5)? ​​"": sdr.GetString (5), sdr.GetDateTime (6), sdr.IsDBNull (7)? 0: sdr.GetInt32 (7), sdr.IsDBNull (8)? "": sdr.GetString (8), sdr.GetDateTime (9),
                    sdr.IsDBNull (10)? "": sdr.GetString (10), sdr.IsDBNull (11)? "": sdr.GetString (11), sdr.IsDBNull (12)? "": sdr.GetString (12) , sdr.IsDBNull (13)? "": sdr.GetString (13), sdr.IsDBNull (14)? "": sdr.GetString (14), sdr.IsDBNull (15)? "": sdr.GetString (15 ),
                    sdr.IsDBNull (16)? 0: sdr.GetInt32 (16), sdr.IsDBNull (17)? 0: sdr.GetInt32 (17), sdr.IsDBNull (18)? 0: sdr.GetInt32 (18), sdr. IsDBNull (19)? 0: sdr.GetInt32 (19), sdr.IsDBNull (20)? 0: sdr.GetInt32 (20), sdr.IsDBNull (21)? 0: sdr.GetInt32 (21),
                    sdr.IsDBNull (22)? "": sdr.GetString (22), sdr.GetDateTime (23), sdr.IsDBNull (24)? 0: sdr.GetInt32 (24), sdr.IsDBNull (25)? 0: sdr .GetInt32 (25), sdr.IsDBNull (26)? "": Sdr.GetString (26), sdr.IsDBNull (27)? 0: sdr.GetInt32 (27), sdr.IsDBNull (28)? 0: sdr. GetInt32 (28), sdr.IsDBNull (29)? 0: sdr.GetInt32 (29)));
            }
            sdr.Close ();
            sdr.Dispose ();
            return postlist;
        }


The result is the same!
Reply

Use magic Report

1

Threads

16

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

 Author| Post time: 2020-4-20 05:30:01
| Show all posts
strSql = "SELECT * FROM V_Bbs_PostDetails WHERE Post_Id =" + postid;
    sdr = GetDataReader (strSql);
Smooth execution!
See special values ​​in the result!
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-4-20 14:30:01
| Show all posts
You add the code to determine whether it is null before the GetString method?
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-4-21 09:00:01
| Show all posts
This error is to convert dbnull.value to int or string
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-4-21 10:00:02
| Show all posts
By the way, not only these two types, but conversion to other types is also a mistake.
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-4-21 11:30:02
| Show all posts
Note that there is also a place for GetDateTime
Reply

Use magic Report

1

Threads

16

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

 Author| Post time: 2020-4-21 13:15:01
| Show all posts
Can you explain it more clearly?

Go out and buy something to eat

Come back soon!

Haha!
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-4-21 18:30:01
| Show all posts
Every place of value is judged whether it is DBNull.Value.
Reply

Use magic Report

1

Threads

16

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 United States

 Author| Post time: 2020-4-23 17:45:01
| Show all posts
Is this the same?
public IList <Bbs_PostDetailsInfo> GetPostDetails (string postid)
{
    IList <Bbs_PostDetailsInfo> postlist = new List <Bbs_PostDetailsInfo> ();
    strSql = "SELECT * FROM V_Bbs_PostDetails WHERE Post_Id =" + postid;
    sdr = GetDataReader (strSql);
    DateTime strda = DateTime.Today;
    while (sdr.Read ())
    {
        postlist.Add (new Bbs_PostDetailsInfo (sdr.IsDBNull (0)? 0: sdr.GetInt32 (0), sdr.IsDBNull (1)? "": sdr.GetString (1), sdr.IsDBNull (2)? sdr.GetString (2), sdr.IsDBNull (3)? 0: sdr.GetInt32 (3),
            sdr.IsDBNull (4)? "": sdr.GetString (4), sdr.IsDBNull (5)? ​​"": sdr.GetString (5), sdr.IsDBNull (6)? strda: sdr.GetDateTime (6), sdr.IsDBNull (7)? 0: sdr.GetInt32 (7), sdr.IsDBNull (8)? "": sdr.GetString (8), sdr.IsDBNull (9)? strda: sdr.GetDateTime (9),
            sdr.IsDBNull (10)? "": sdr.GetString (10), sdr.IsDBNull (11)? "": sdr.GetString (11), sdr.IsDBNull (12)? "": sdr.GetString (12) , sdr.IsDBNull (13)? "": sdr.GetString (13), sdr.IsDBNull (14)? "": sdr.GetString (14), sdr.IsDBNull (15)? "": sdr.GetString (15 ),
            sdr.IsDBNull (16)? 0: sdr.GetInt32 (16), sdr.IsDBNull (17)? 0: sdr.GetInt32 (17), sdr.IsDBNull (18)? 0: sdr.GetInt32 (18), sdr. IsDBNull (19)? 0: sdr.GetInt32 (19), sdr.IsDBNull (20)? 0: sdr.GetInt32 (20), sdr.IsDBNull (21)? 0: sdr.GetInt32 (21),
            sdr.IsDBNull (22)? "": sdr.GetString (22), sdr.IsDBNull (23)? strda: sdr.GetDateTime (23), sdr.IsDBNull (24)? 0: sdr.GetInt32 (24), sdr .IsDBNull (25)? 0: sdr.GetInt32 (25), sdr.IsDBNull (26)? "": Sdr.GetString (26), sdr.IsDBNull (27)? 0: sdr.GetInt32 (27), sdr. IsDBNull (28)? 0: sdr.GetInt32 (28), sdr.IsDBNull (29)? 0: sdr.GetInt32 (29)));
    }
    sdr.Close ();
    sdr.Dispose ();
    return postlist;
}
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