Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good afternoon

I am developing a custom made logistics application and in one of my forms i have 4 dropdown lists a textbox and gridview. The four dropdowns are populated with data from four different tables, while the textbox requires that data is inputed by the user.

My problem that i am finding is that when i go to save to the require table it is not saving any data.

The Dropdowns are a follows
Shipment No - display shipment number from table ShipmentNumbers
Client - lists all the clients
Brand - lists all the Brands
Currency - lists all the currenct currency's.
Season - textbox for user to enter relevant season eg. SU15

The below code is the Current Insert Statement using parameters.

VB
Public Sub InsertShipmentNoDetails(ByVal RegShipmentID As Integer, ByVal ClientID As Integer, ByVal Season As String, ByVal BrandID As Integer, ByVal CurrencyID As Integer)
       'This inserts a new transit document to the database table in question
       'A set of parameters are passed then connected to the command object
       Dim myCommand As New SqlCommand()

       myCommand.Connection = SQLCon

       myCommand.CommandText = "INSERT INTO RegisterShipmentNoDetails(RegShipmentID, ClientID, Season, BrandID, CurrencyID)values(@RegShipmentID,@ClientID,@Season,@BrandID,@CurrencyID)"
       'Parameters are passed and connected to the Command Object

       myCommand.Parameters.AddWithValue("@RegShipmentID", RegShipmentID)
       myCommand.Parameters.AddWithValue("@ClientID", ClientID)
       myCommand.Parameters.AddWithValue("@Season", Season)
       myCommand.Parameters.AddWithValue("@BrandID", BrandID)
       myCommand.Parameters.AddWithValue("@Currency", CurrencyID)

       'Here the command on the database is executed.
       myCommand.ExecuteNonQuery()

   End Sub


The above method is then called from the save button as follows.

VB
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
       'Validate if textbox is empty or not
       If ValidateData(txtSeason) = False Then
           ' MessageBox.Show("Please enter Required Textbox Details", ApplicationName, MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)

           Exit Sub
       End If

       Try
           mySQL.InsertShipmentNoDetails(txtSeason.Text, cboBrand.SelectedValue, cboClient.SelectedValue, cboCurrency.SelectedValue, cboShipmentNo.SelectedValue)

       Catch ex As Exception
           MessageBox.Show("Register Shipment Details Could Not Be Inserted," & ex.Message, ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error)
           Exit Sub
       End Try

       MessageBox.Show(" Register Shipments Details Successfully Inserted", ApplicationName, MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
       'Gridview is loaded
       dvShipmentRegister.Table = mySQL.LoadShipmentNoDetails()

   End Sub


Would appreciate if someone would explain what or where i am doing wrong in the above code.

Thanks.
Posted
Comments
[no name] 10-Jul-15 11:20am    
If something is going wrong, it won't be an Exception it will be an SqlException.

1 solution

The whole code isn't included in your post so this is somewhat guessing, but:
- ValidateData(txtSeason) returns false for some reason, you don't show any message in such case
- SQLCon points to a wrong database
- state of the already existing connection is wrong
- you have a ongoing transaction but you don't commit and so on

Any way, it's best to open the debugger and go through the code, line-by-line. This way you'll pinpoint the source of the problem in no time.
 
Share this answer
 
Comments
Etienne Zammit 10-Jul-15 11:43am    
Hi Mika,
Thanks for your suggestions,
- with regrads the ValidateData(txtSeason) it is calling a function which handles if the textbox is empty or not, so if it is empty it will exit and show a message to the user.
- Can you please explain what you mean in the the last three points as i got a bit mixed up.

Thanks
Wendelius 10-Jul-15 12:00pm    
For the last three bullets:
- you seem to use a connection which is opened somewhere else in your code. Is it opened to the correct database?
- is the connection open and valid
- do you use explicit transactions, if you do, where do you commit
Wendelius 10-Jul-15 12:01pm    
Also note that with bullet 1 I meant that the message box is commented out from your code, is it shown somewhere else or do you rely on this code block to show it?
Etienne Zammit 10-Jul-15 12:39pm    
- The connection is done through a class method and the called from the relevant form, which is working as manged to Insert data to the database from another form.
- The message box is called from the ValidateDate method which is found on the same form.

- I tried going through the code line by line and the following error message came up:

'Regsiter Shipment Details Could Not Be Inseted, Conversion from string 'FA15' to type integer is not valid.

Any advice on the above please.
Thanks
Wendelius 10-Jul-15 12:58pm    
Ok, so where did the FA15 come from? Probably from one of the comboboxes has that as selected value. First verify this with the debugger. Place a breakpoint where you call the InsertShipmentNoDetails method and have a look at each parameter value . Most likely one of the values you try to pass to the method isn't integer. BAsically one of these:
- ByVal RegShipmentID As Integer
- ByVal ClientID As Integer
- ByVal BrandID As Integer
- ByVal CurrencyID As Integer

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