Hi,
I have the following Save function, that works but the logic is not correct:
1) Insert data to the tblproperties table - this working OK
2) Pick up the PropertyID just created and display on the txtPropertID - Not correct
3) Insert to tblunits - but attach the rows from the datagridview to the PropertyID created - as a result of 2 (PropertyID is not recorded correctly)
The issue is the Max(PropertyID) function is not storing the newly created propertyID instead it picks from the database the last created propertyID, I know the issue is that my approach to retain the PropertyID is not correct and inserting it with the records for the second insert is not true.
How can tackle this better?
Thanks
I have the following Save function, that works but the logic is not correct:
Code:
Private Sub Save()
Dim conn As SqlConnection = GetDbConnection()
Dim query As String
Dim cmd As New SqlCommand
Dim valid As Boolean = True
Dim Message As String = String.Empty
If TxtTUnits.Text = "" Or IsNumeric(TxtTUnits.Text) = False Then
MessageBox.Show("Enter total number of Units or Value entered is not valid", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information)
TxtTUnits.Focus()
Return
End If
If Not valid Then
MsgBox(Message, MsgBoxStyle.Information, "RCMS")
Else
Try
Dim command As New SqlCommand("INSERT INTO dbo.tblproperties (propertytypeID, landlordregID, buildingname, plotno, location, totalunits) VALUES (@propertytypeID, @landlordregID, @buildingname, @plotno, @location, @totalunits)", conn)
command.Parameters.Add("@propertytypeID", SqlDbType.Int).Value = cmbPType.SelectedValue
command.Parameters.Add("@landlordregID", SqlDbType.Int).Value = cmbLandlordname.SelectedValue
command.Parameters.Add("@buildingname", SqlDbType.NVarChar).Value = TxtBName.Text
command.Parameters.Add("@plotno", SqlDbType.NVarChar).Value = TxtPlotNo.Text
command.Parameters.Add("@location", SqlDbType.VarChar).Value = TxtLoc.Text
command.Parameters.Add("@totalunits", SqlDbType.Int).Value = TxtTUnits.Text
Try
query = "SELECT Max(PropertyID) FROM dbo.tblproperties"
cmd = New SqlCommand(query, conn)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "RCMS")
End Try
Dim MaxPropertyID As String
MaxPropertyID = cmd.ExecuteScalar()
txtPropertyID.Text = MaxPropertyID
command.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "RCMS")
End Try
End If
For i As Integer = 0 To DgUnits.Rows.Count - 1 Step +1
Try
Dim command As New SqlCommand("INSERT INTO dbo.tblunits (propertyID, flattype, units, rentamt, vacant) VALUES (@propertyID, @flattype, @units, @rentamt, @vacant)", conn)
command.Parameters.Add("@propertyID", SqlDbType.Int).Value = CInt(txtPropertyID.Text)
command.Parameters.Add("@flatType", SqlDbType.VarChar).Value = DgUnits.Rows(i).Cells(0).Value.ToString()
command.Parameters.Add("@units", SqlDbType.NVarChar).Value = DgUnits.Rows(i).Cells(1).Value.ToString()
command.Parameters.Add("@rentamt", SqlDbType.Decimal).Value = DgUnits.Rows(i).Cells(2).Value.ToString()
command.Parameters.Add("@vacant", SqlDbType.VarChar).Value = DgUnits.Rows(i).Cells(3).Value.ToString()
command.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "RCMS")
End Try
Next
conn.Close()
conn.Dispose()
MessageBox.Show("New Property has been added", "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Close()
End Sub
1) Insert data to the tblproperties table - this working OK
2) Pick up the PropertyID just created and display on the txtPropertID - Not correct
3) Insert to tblunits - but attach the rows from the datagridview to the PropertyID created - as a result of 2 (PropertyID is not recorded correctly)
The issue is the Max(PropertyID) function is not storing the newly created propertyID instead it picks from the database the last created propertyID, I know the issue is that my approach to retain the PropertyID is not correct and inserting it with the records for the second insert is not true.
How can tackle this better?
Thanks