Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
Posted
Updated 22-Nov-19 17:43pm

The problem is that the when there are no records in your table the result of the
VB.NET
cmd.ExecuteScalar()
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.
 
Share this answer
 
Comments
Member 12456202 13-Apr-16 6:27am    
rahul can u be little accurate...i didnt undertand you...plus i am doing my project in VB.net and i can show u screenshot of the error but i cant find any screenshot panel here 0_o?
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
 
Share this answer
 
Comments
Nigam,Ashish 13-Apr-16 6:32am    
can you send me screen shots or code of after change and error details.
Member 12456202 24-Apr-16 13:54pm    
ohh..ashish my error is solved brother...thank you....actually i first inserted the code at wrong place....it was my mistake...
now i have change correctly and it works erfectly...thank you once again..
:)
1014:SUB-AUA code is invalid" to type 'Integer' is not valid.Aadhaar Service
 
Share this answer
 
Comments
Richard Deeming 27-Nov-19 15:03pm    
Your error message is not a "solution" to someone else's error!

If you want to ask a question, then ASK A QUESTION[^]. But you'll need to provide a lot more information that an error message if you want anyone to be able to help you.

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