I have a problem where I am unable to make a booking to a Sql server .mdf database, but I am able to read to it.
No error messages occur and I have tried everything I can think of. Any help will be highly appreciated and thanks in advance!
Here is the code used for the insert into command:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim SQLCode As New SQLCode
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim lrd As SqlDataReader
Dim inscmd As New SqlCommand
cmd.Parameters.AddWithValue("@StaffInitials", lblStaffInitials.Text.Trim())
lblStaffInitials.Text = lblStaffInitials.Text
cmd.Parameters.AddWithValue("@Week", lblWeek.Text.Trim)
lblWeek.Text = lblWeek.Text
cmd.Parameters.AddWithValue("@Period", (lblPeriod.Text.Trim))
lblPeriod.Text = lblPeriod.Text
cmd.Parameters.AddWithValue("@Day", lblDay.Text.Trim)
lblDay.Text = lblDay.Text
cmd.Parameters.AddWithValue("@Subject", lblSubject.Text.Trim)
lblSubject.Text = lblSubject.Text
cmd.Parameters.AddWithValue("@YearGroup", lblYear.Text.Trim)
lblYear.Text = lblYear.Text
cmd.Parameters.AddWithValue("@NumberOfPupils", (lblNoOfPupils.Text.Trim))
lblNoOfPupils.Text = lblNoOfPupils.Text
cmd.Parameters.AddWithValue("@ControlledAssesment", lblControlledAssesment.Text.Trim)
lblControlledAssesment.Text = lblControlledAssesment.Text
cmd.Parameters.AddWithValue("@Room", lblRoom.Text.Trim)
lblRoom.Text = lblRoom.Text
cmd.Parameters.AddWithValue("@Session", txtSession.Text.Trim)
txtSession.Text = txtSession.Text
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("RoomBookingSystem.My.MySettings.Database1ConnectionString1").ConnectionString
Try
Using cn As New SqlConnection(con.ConnectionString)
con.Open()
inscmd.CommandText = "INSERT INTO Booking (Week, Day, Period, Subject, YearGroup, StaffInitials,NumberOfPupils,Session,Room,ControlledAssesment) VALUES (@Week,@Day,@Period,@Subject,@YearGroup,@StaffInitials, @NumberOfPupils,@Session,@Room,@ControlledAssesment)"
inscmd.Connection = con
inscmd.ExecuteNonQuery()
inscmd.Parameters.Clear()
End Using
Finally
con.Close()
End Try
MsgBox("Your Booking Has Been Made Successfully")
Clicky = False
MainViewForm.btnBackToBooking.Visible = False
FormView.Show()
Me.Hide()
End Sub
As far as I can tell the problem lies within the databases location.
When the program is debugged a copy of your database is placed within the bin/debug folder, if you look in here you will see the copy and your code will right to this database and Not The original Database (if you click show all files then go to the bin/debug file and then open with database explorer you can see the data imputed on the last debug/running)
To fix this you need to do the following:
Your connection string
Will connect to the database within the bin folder mentioned previously.
You need to go into your app.config file find the specific location for your database and use this for you connection string, this will allow you to insert/update data from the non copy of the database.