参数“表达式”无法转换为“DBNull”类型

时间:2013-07-31 09:48:05

标签: vb.net data-binding error-handling dbnull

我严重陷入了错误。当我尝试从Menu.vb调用一个表单(frmRecipe)它在行上给出了一个错误,我正在尝试从Recipe表中获取最大id,这是我的代码下面

 Function Get_Max_Id() As String
        Try
            Dim CMD As New SqlCommand("SELECT MAX(Recipe_ID) FROM Recipe", CONN)
            CMD.CommandType = CommandType.Text
            Dim dr As SqlDataReader
            dr = CMD.ExecuteReader
            If dr.Read Then
                Return Format(Val(dr(0)).ToString + 1, "0000")
            Else
                Return "0001"
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function

Recipe_id是数据库中Varchar的类型

1 个答案:

答案 0 :(得分:1)

试试这个:

Return Format(Val(dr(0).ToString) + 1, "0000")