0

I am trying to store data in the sql database using vb.net. In my code I am using parameters to add values.. Below is my code:

Dim lclAmount = txtQty.Text * txtUnitPrice.Text
con = New SqlConnection(connectionString)
' con.Open()

query = "INSERT INTO TBLExpensesList (ExpensesType, Purpose, Qty, UnitPrice, Amount, DoP, Description) VALUES (@val1, @val2, @val3, @val4, @val5, @val6, @val7)"

cmd = New SqlCommand(query, con)

cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val1"
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val2"
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val3"
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val4"
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val5"
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val6"
cmd.Parameters.Add(cmd.CreateParameter).ParameterName = "@val7"


cmd.Parameters("@val1").Value = ComboBox1.SelectedItem
cmd.Parameters("@val2").Value = txtPurpose.Text
cmd.Parameters("@val3").Value = txtQty.Text
cmd.Parameters("@val4").Value = txtUnitPrice.Text
cmd.Parameters("@val5").Value = lclAmount
cmd.Parameters("@val6").Value = dtpDOP.Value
cmd.Parameters("@val7").Value = txtDesc.Text



If con.State = ConnectionState.Closed Then
     con.Open()
End If
' cmd.CommandText = query
'                cmd.Connection = con
'Create Data Adaptor
'                sqladp.SelectCommand = cmd
'Create & Fill Data Set
'sqladp.Fill(ds, "TBLExpensesList")
'Get Data Table
'dtbl = ds.Tables("TBLExpensesList")

cmd.ExecuteNonQuery()

If con.State = ConnectionState.Open Then
    con.Close()
End If

MessageBox.Show("Data Inserted Successfully!", "", MessageBoxButtons.OK, MessageBoxIcon.None)


'Execute Command


Catch ex As Exception
      MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

End If

I am successfully able to login into my project when the useid and passwords matches the stored values in the database. But when i try to store values I got Inserted successfully and after that When I try to read the rows I got one row. When I close my project ad try to see the values it the database I got no value in the database.. I am also confusing that why the values are not being stored in the database????

13
  • If you work from a database file sometimes in debug mode it regenerates the file and hence the changes are only on the go. Try to compile into the release and run the exe from the folder directly. Commented May 22, 2017 at 8:48
  • hi, already in release mode Commented May 22, 2017 at 8:50
  • Are you debugging in release mode? Commented May 22, 2017 at 8:52
  • i have tried in debug and release mode both but I got the same issue. Commented May 22, 2017 at 8:53
  • If you read my message I asked you go to the release folder and directly launch the Exe not to debug Commented May 22, 2017 at 8:56

1 Answer 1

1

You should read up on Working with local databases. A quote from there (emphasis mine):

At design-time, MyProject\Data.mdf is used by the data tools. At run-time, the app will be using the database under the output folder. As a result of the copy, many people have the impression that the app did not save the data to the database file. In fact, this is simply because there are two copies of the data file involved. Same applies when looking at the schema/data through the database explorer. The tools are using the copy in the project, not the one in the bin folder.

Sign up to request clarification or add additional context in comments.

1 Comment

hi, i agree with you. but no one wants to execute the .exe from release to check either the data is there or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.