Click here to Skip to main content
15,943,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
    If blog_img.HasFile Then
        Dim strExt As String = IO.Path.GetExtension(blog_img.FileName).ToLower
        If strExt = ".jpg" Or strExt = ".jpeg" Or strExt = ".gif" Then

            cmd.CommandType = Data.CommandType.Text
            cmd.CommandText = "Select max(blog_id) From tbl_goshima_add_blogs"
            cmd.Connection = objcon.ConObj
            Dim reg_id = 0
            reg_id = cmd.ExecuteScalar
            'If rdr.Read Then
            If IsDBNull(reg_id) Then
                iret = 1

                reg_id = reg_id + 1
                'lblmsg = reg_id
                iret = reg_id
            End If

            Dim srcFile, destFile As String
            srcFile = Server.MapPath("../blog_img" & iret & System.IO.Path.GetExtension(blog_img.FileName).ToLower())
            pathnm = iret & strExt

            Page.RegisterStartupScript("ScriptDescription", _
                                        "<Script type=""text/javascript"">alert('Please Select Proper image" & _
            Exit Sub
        End If
        'Page.RegisterStartupScript("ScriptDescription", _
        '                          "<Script type=""text/javascript"">alert('Please Select blog image" & _
        '                            "');</Script>")
        'Exit Sub
    End If
Catch ex As Exception
    Dim msg = ex.Message
    Page.RegisterStartupScript("ScriptDescription", _
                                    "<Script type=""text/javascript"">alert('" + msg + " " & _

    Exit Sub
End Try

Please help me as the above code shows error :- conversion from type dbnull to type integer is not valid
Updated 21-Feb-12 20:13pm
Varun Sareen 22-Feb-12 2:13am    
edit for: added pre tag
Varun Sareen 23-Feb-12 11:55am    
the solution helped you out or not.. Please mark if it helped you out

The error conversion from type dbnull to type integer is not valid is thrown when you try to assign a DBnull to an integer type variable. This usually happens when the database field has no value, which is dbnull, and the value is assigned to a variable without checking. It is difficult to exactly pin point from your code. It is better to use the break point in debugging mode and step through the code to find where this error is thrown. After finding the code line then check for DBnull and assign appropriate value.

The problem could be in this statement

reg_id = cmd.ExecuteScalar

Because on execution of cmd.ExecuteScalar, if the datbase field does not have a value, it will try to assign DBNull to integer type variable,reg_id, which will result in the above error.

If your problem is solved, you may accept and vote the solution, otherwise please post your queries

Share this answer
If IsDBNull(reg_id) Then

is giving the error as the above condition is not able to cast the DBNULL type to integer type.

try this:-

If NOT IsDbNull(myItem("sID")) AndAlso myItem("sID") = sId Then
   'Do success
End If

You can also use the Convert.ToString() and Convert.ToInteger() methods to convert items with DB null effectivly as NULL exception is handled by these.

I hope this will help you out.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900