Click here to Skip to main content
14,304,321 members
Rate this:
Please Sign up or sign in to vote.
See more:
Try
    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
            cmd.Parameters.Clear()
            Dim reg_id = 0
            reg_id = cmd.ExecuteScalar
            'If rdr.Read Then
            If IsDBNull(reg_id) Then
                iret = 1

            Else
                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())
            blog_img.SaveAs(srcFile)
            pathnm = iret & strExt
        Else

            Page.RegisterStartupScript("ScriptDescription", _
                                        "<Script type=""text/javascript"">alert('Please Select Proper image" & _
                                        "');</Script>")
            Exit Sub
        End If
    Else
        '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 + " " & _
                                    "');</Script>")

    Exit Sub
End Try



Please help me as the above code shows error :- conversion from type dbnull to type integer is not valid
Posted
Updated 21-Feb-12 20:13pm
v2
Comments
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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

PES
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Your
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
ELSE
   'Failure
End If

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100