从类型dbnull到类型integer的转换无效 [英] Conversion from type dbnull to type integer is not valid

查看:164
本文介绍了从类型dbnull到类型integer的转换无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了这个错误,我不知道如何解决这个问题.....做了研究,但未能得到合适的答案.....这是我的第一篇文章...对不起我的坏语< br $>


Private Sub btnNew_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnNew.Click



cnnOLEDB =新的SqlConnection(数据源= SHRAYAS-PC;初始目录= Bag_Shop;集成安全性=真)



cnnOLEDB.Open()



来电清除()



Dim cmd作为新的SqlCommand(从employee_details选择最大值(emp_id), cnnOLEDB)

Dim maxrows As Integer = cmd.ExecuteScalar()

maxrows = maxrows + 1

txt_id.Text = maxrows

'MsgBox(maxrows)

cmd =没什么



flag = 2

btnSave.Text =保存





btnNew.Enabled = Fals e />
btnDelete.Enabled = False

btnSearch.Enabled = False

btnFirst.Enabled = False

btnPrevious。 Enabled = False

btnNext.Enabled = False

btnLast.Enabled = False



End Sub



我尝试了什么:



这是我得到的异常错误详细信息: -



System.InvalidCastException未处理

消息=从类型'DBNull'转换为'Integer'类型无效。

Source = Microsoft.VisualBasic

StackTrace:

at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(Object Value)

位于C:\ Users \ SHRAYAS \Desktop \ Bag Shop中的Bag_Shop.employee_details.btnNew_Click(Object sender,EventArgs e)中的商店\ Bag_Shop\employee_details.vb:第74行
at System.Windows.Forms.Control.OnClick( EventArgs e)

在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

在System.Windows.Forms.Control.WmMouseUp(Message& m,MouseButtons按钮,Int32点击)

在System.Windows.Forms.Control.WndProc(消息& m)

在System.Windows.Forms.ButtonBase.WndProc(消息& m)

在System.Windows.Forms.Button.WndProc(消息& m)

在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m)

在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

在System.Windows.Forms.UnsafeNativeMethods。 DispatchMessageW(MSG& msg)

在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 reason,Int32 pvLoopData)
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)
在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 r)
在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
$中,$。
$ b at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String [] commandLine)

at Bag_Shop.My.MyApplication.Main(String [] Args)in 17d14f5c-a337-4978-8281- 53493378c1071.vb:第81行

at System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)

at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()<在System.Threading.ExecutionContext.Run上执行
(ExecutionContext executionContext,ContextCallback回调,对象状态)

at System.Threading.ThreadHelper.ThreadStart()

InnerException:

i got this error and i don't know how to solve this.....did research but failed to get an appropriate answer.....this is my first post...sorry for my bad language

Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click

cnnOLEDB = New SqlConnection("Data Source=SHRAYAS-PC; Initial Catalog=Bag_Shop; Integrated Security=True")

cnnOLEDB.Open()

Call clear()

Dim cmd As New SqlCommand("Select Max(emp_id) from employee_details", cnnOLEDB)
Dim maxrows As Integer = cmd.ExecuteScalar()
maxrows = maxrows + 1
txt_id.Text = maxrows
'MsgBox(maxrows)
cmd = Nothing

flag = 2
btnSave.Text = "Save"


btnNew.Enabled = False
btnDelete.Enabled = False
btnSearch.Enabled = False
btnFirst.Enabled = False
btnPrevious.Enabled = False
btnNext.Enabled = False
btnLast.Enabled = False

End Sub

What I have tried:

this the exception error details which i am getting :-

System.InvalidCastException was unhandled
Message=Conversion from type 'DBNull' to type 'Integer' is not valid.
Source=Microsoft.VisualBasic
StackTrace:
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(Object Value)
at Bag_Shop.employee_details.btnNew_Click(Object sender, EventArgs e) in C:\Users\SHRAYAS\Desktop\Bag Shop\Bag_Shop\employee_details.vb:line 74
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Bag_Shop.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

推荐答案

问题是你的tabl中没有记录的时候e
The problem is that the when there are no records in your table the result of the
cmd.ExecuteScalar()

的结果是 DbNull 。哪个是班级。并且你试图把它推到一个整数。



所以我的建议是首先把它的结果变成一个隐式类型的变量(比如C#中的var,不确定VB)然后检查它的 DbNull ,将0推到整数值,否则是实际值。



备用解决方案:使用Ashish建议的解决方案,并在查询中放置一个ISNULL。

is DbNull. Which is a class. And you are trying to push it to an integer.

So my suggestion would be to first take the result of this into an implicitly typed variable(like var in C#, not sure about VB) and then check if its DbNull, push 0 to the Integer value else the actual value.

Alternate solution: Use the solution which Ashish suggested and put an ISNULL in your query itself.


由于没有记录,您将收到此消息你的桌子(employee_details)



所以你可以做一件事在你的查询中使用Isnull功能这样



从employee_details中选择ISNULL(Max(emp_id),0)





第一条记录将返回0。





Ashish
This message you are getting due to there is no records in your table("employee_details")

so you can do one thing use Isnull function in your query like this

Select ISNULL(Max(emp_id),0) from employee_details


It will return 0 for first record.


Ashish


这篇关于从类型dbnull到类型integer的转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆