473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Update command with parameters problem

12
Expand|Select|Wrap|Line Numbers
  1. Private Function AddCompanyOvertime() As Integer
  2.         Dim companyID As Integer = GetCompanyID()
  3.         Console.WriteLine(companyID)
  4.         Dim paramCompanyID As New SqlParameter("@CompanyID", SqlDbType.Int)
  5.         Dim paramCompanyOvertimeID As New SqlParameter("@companyOvertimeID", SqlDbType.Int)
  6.         Dim paramDate As New SqlParameter("@Date", SqlDbType.DateTime)
  7.         Dim paramOvertimePrice As New SqlParameter("@OvertimePrice", SqlDbType.Float)
  8.         Dim paramAdditionalRoutePrice As New SqlParameter("@AdditionalRoutePrice", SqlDbType.Float)
  9.         Dim paramAdditionalRoute As New SqlParameter("@AdditionalRoute", SqlDbType.Text)
  10.         Dim paramRoute As New SqlParameter("@Route", SqlDbType.Text)
  11.         Dim paramNewRecordID As New SqlParameter("@NewRecordID", SqlDbType.Int)
  12.         Dim newCompanyOvertimeID As New Integer
  13.         newCompanyOvertimeID = 0
  14.  
  15.         paramCompanyID.Direction = ParameterDirection.Input
  16.         paramCompanyID.Value = companyID
  17.         paramDate.Direction = ParameterDirection.Input
  18.         'actual date of record, ie, clientDate
  19.         paramDate.Value = ClientDateTBox.Text()
  20.         paramRoute.Direction = ParameterDirection.Input
  21.         paramRoute.Value = clientOvertimeRouteTBox.Text
  22.         paramOvertimePrice.Direction = ParameterDirection.Input
  23.         If companyFOPriceTBox.Enabled = True Then
  24.             paramOvertimePrice.Value = companyFOPriceTBox.Text
  25.         Else
  26.             paramOvertimePrice.Value = companyVOPriceTBox.Text
  27.         End If
  28.         If clientARouteTBox.Enabled = True Then
  29.             paramAdditionalRoute.Direction = ParameterDirection.Input
  30.             paramAdditionalRoute.Value = clientARouteTBox.Text
  31.             paramAdditionalRoutePrice.Direction = ParameterDirection.Input
  32.             paramAdditionalRoutePrice.Value = companyARPriceTBox.Text
  33.         Else
  34.             paramAdditionalRoute.Direction = ParameterDirection.Input
  35.             paramAdditionalRoute.Value = System.DBNull.Value
  36.             paramAdditionalRoutePrice.Direction = ParameterDirection.Input
  37.             paramAdditionalRoutePrice.Value = System.DBNull.Value
  38.         End If
  39.         paramNewRecordID.Direction = ParameterDirection.Output
  40.         Try            
  41.             If standbyCB.Checked = False Then
  42.                 SqlInsertCommandCompanyOvertime.Parameters.Add(paramCompanyID)
  43.                 SqlInsertCommandCompanyOvertime.Parameters.Add(paramDate)
  44.                 SqlInsertCommandCompanyOvertime.Parameters.Add(paramOvertimePrice)
  45.                 SqlInsertCommandCompanyOvertime.Parameters.Add(paramRoute)
  46.                 SqlInsertCommandCompanyOvertime.Parameters.Add(paramAdditionalRoute)
  47.                 SqlInsertCommandCompanyOvertime.Parameters.Add(paramAdditionalRoutePrice)
  48.                 SqlInsertCommandCompanyOvertime.Parameters.Add(paramNewRecordID)
  49.                 SqlInsertCommandCompanyOvertime.Connection.Open()
  50.                 SqlInsertCommandCompanyOvertime.ExecuteNonQuery()
  51.                 SqlInsertCommandCompanyOvertime.Connection.Close()
  52.                 newCompanyOvertimeID = SqlInsertCommandCompanyOvertime.Parameters.Item("@NewRecordID").Value()
  53.                 Console.WriteLine(newCompanyOvertimeID)
  54.                 SqlInsertCommandCompanyOvertime.Parameters.Clear()
  55.             Else
  56.                 paramCompanyOvertimeID.Direction = ParameterDirection.Input
  57.                 paramCompanyOvertimeID.Value = AddCompanyStandby()
  58.  
  59.                 SqlUpdateCommandCompanyOvertime.Parameters.Add(paramOvertimePrice)
  60.                 SqlUpdateCommandCompanyOvertime.Parameters.Add(paramRoute)
  61.                 SqlUpdateCommandCompanyOvertime.Parameters.Add(paramAdditionalRoute)
  62.                 SqlUpdateCommandCompanyOvertime.Parameters.Add(paramAdditionalRoutePrice)
  63.                 SqlUpdateCommandCompanyOvertime.Parameters.Add(paramCompanyOvertimeID)
  64.                 SqlUpdateCommandCompanyOvertime.Connection.Open()
  65.                 SqlUpdateCommandCompanyOvertime.ExecuteNonQuery()
  66.                 SqlUpdateCommandCompanyOvertime.Connection.Close()
  67.                 SqlUpdateCommandCompanyOvertime.Parameters.Clear()
  68.             End If
  69.         Catch ex As Exception
  70.             Console.WriteLine(ex.StackTrace)
  71.         End Try
  72.         Return newCompanyOvertimeID
  73.     End Function
  74.  
Hi all! got a problem with my code when am running an UPDATE command. Basically what am doing in this code is calling a function AddCompanyStandby() that will return me a newly entered record id as an Integer. I want to use that id for the update command to insert new data in that same column but am getting errors. Any ideas?


A first chance exception of type 'System.FormatException' occurred in System.Data.dll
4
4
4
4
55
at System.Data.SqlClient.SqlParameter.CoerceValue(Obj ect value, MetaType destinationType)
at System.Data.SqlClient.SqlParameter.GetCoercedValue ()
at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc)
at System.Data.SqlClient.SqlCommand.SetUpRPCParameter s(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderT ds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader( CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at WindowsApplication1.Form1.AddCompanyOvertime() in C:\Documents and Settings\Root\My Documents\Visual Studio 2008\Projects\WindowsApplication1\WindowsApplicati on1\Form2.vb:line 91
Oct 25 '08 #1
3 3628
debasisdas
8,127 Expert 4TB
you need to pass the parameter of the correct data type to avoid the error.
Oct 25 '08 #2
kpeeroo
12
i dont understand. I have commented all of the parameters i am passing to my update command except that of price, ie, only one parameter of type float. By the way am using stored procedure and the type is float there as well. But now getting this error:


A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
at System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteRead er(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderT ds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader( CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at WindowsApplication1.Form1.AddCompanyOvertime() in C:\Documents and Settings\Root\My Documents\Visual Studio 2008\Projects\WindowsApplication1\WindowsApplicati on1\Form2.vb:line 91
Oct 25 '08 #3
kpeeroo
12
Hi guys

just sorted out the problem. It was with my stored procedure parameters. I cannot leave a variable in the file without declaring it because sql database server will expect it. So better comment every variable that is not to be passed to the sql command! Also, I had to check the parameters in the properties in design mode of my SqlDataAdapter. In my case, I had to clear everything that was being generated.
Oct 28 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jason Huang | last post by:
Hi, The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is working for update, but SqlParameter myPM =new SqlParameter ("@Address",SqlDbType.NVarChar,90,txtAddress.Text) is...
3
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
8
by: rriness | last post by:
I'm getting an inconsistent failure when trying to save data in ADO.Net. I'm using an Access database with a simple query - SELECT StudentID, FirstName, LastName FROM Students - and have no...
4
by: William | last post by:
After much frustration I was able to update my data store via code only. Using the data adapter was the only way I was able to set up all the objects written in my code. Basically, I cheated by...
8
by: Zorpiedoman | last post by:
I keep getting a concurrency exception the second time I make a change and attempt to update a dataadapter. It appears this is by design, so there must be something I can do to avoid it. ...
2
by: explode | last post by:
I made nova oledbdataadapter select update insert and delete command and connection veza. dataset is Studenti1data, I made it by the new data source wizard,and made datagridview and bindingsource...
1
by: Selvakumar | last post by:
hai friends, I am new to .net programming. I did the inserting data into MS-access database but i couldn't able to perform the update command. I used only textbox and command button for inserting...
2
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to...
4
by: ahling | last post by:
Hi all. As mentioned in my first post, I'm currently doing an asp.net blog where user can update their profile, and update their blog entry. But I encountered problem with the update of entry...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.