Hi everyone,
I have a form with one combo box and text box and buttons. please refer the snapshot with i have attached.
I have the following code:
My Stored Procedure for inserting the data:
/*
Name: usp_InsertBranchSetup
Description: Insert the Record to dbo.hrBranchSetup table
Author: Tashi
Modification: Insert
Description Date Changed By
Created Procedure 25/02/2011 Tashi
*/
CREATE PROCEDURE [dbo].[usp_InsertBranchSetup]
(
@BranchCode CHAR(4),
@OsID CHAR(4),
@BranchName VARCHAR(75),
@OnLineBrYN BIT,
@SetDate DATETIME,
@UserName VARCHAR(20),
@CMD_Flag CHAR(1)
)
WITH ENCRYPTION
AS
INSERT INTO [dbo].[hrBranchSetup] VALUES
(
@BranchCode,
@OsID,
@BranchName,
@OnLineBrYN,
@SetDate,
@UserName
)
INSERT INTO [dbo].[hrBranchSetup_Audit] VALUES
(
@BranchCode,
@OsID,
@BranchName,
@OnLineBrYN,
@CMD_Flag,
@SetDate,
@UserName
)
#Region "InsertCommand"
Private Sub InsertCMD()
Try
Dim sDate As String = CType(DateAndTime.Now, String)
Dim sIFlag As String = "C"
Dim cmd As New OleDbCommand("usp_InsertBranchSetup", SQLConn)
'ds.Clear() 'Clear Data Set
cmd.CommandType = CommandType.StoredProcedure
cmd.Transaction = trns
cmd.Parameters.Add("@BranchCode", OleDbType.Char).Value = brCode.Text
cmd.Parameters.Add("@OsID", OleDbType.Char).Value = cboOrganization.ValueMember
cmd.Parameters.Add("@BranchName", OleDbType.VarChar).Value = brName.Text
cmd.Parameters.Add("@OnLineBrYN", OleDbType.BigInt).Value = bsOnlineYNCheckBox.CheckState
cmd.Parameters.Add("@SetDate", OleDbType.Date).Value = sDate
cmd.Parameters.Add("@UserName", OleDbType.VarChar).Value = UCase(frmMainPanel.ShowCurrentUserStatusLabel.Text)
cmd.Parameters.Add("@CMD_Flag", OleDbType.Char).Value = sIFlag
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error No : " & Err.Number & vbCrLf _
& "Error : " & ex.Message & vbCrLf _
& "Source : " & Err.Source & vbCrLf _
, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
#End Region
Form Load Code:
Dim cmd As New OleDbCommand("select * from dbo.hrorganizationsetup where IsDefault = 1", SQLConn)
cmd.CommandType = CommandType.Text
cmd.Transaction = trns
cmd.Parameters.Add("@OsID", OleDbType.Char).Value = 0
Dim oDa As New OleDbDataAdapter(cmd) 'SQL Data Adapter object
oDa.Fill(ds, "dbo.hrorganizationsetup")
With cboOrganization
.DataSource = ds.Tables("dbo.hrorganizationsetup")
.ValueMember = "OsID"
.DisplayMember = "OsName"
End With
'cboOrganization.SelectedValue = intSelectedBranch
'Remove parameters
cmd.Parameters.Clear()
Save button code:
Try
If bNewData Then ' TRY WITH 'OR' Operator
If vilBranch.BranchCodeExists(brCode.Text.Trim) Then
MessageBox.Show("Branch Code [" & brCode.Text.Trim & "] already exists. Please Check.", "Data Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning)
brCode.Focus()
Exit Sub
ElseIf vilBranch.BranchNameExists(brName.Text.Trim) Then
MessageBox.Show("Branch Name [" & brName.Text.Trim & "] already exists. Please Check.", "Data Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning)
brName.Focus()
Exit Sub
Else
btnbsSave.Focus()
End If
SQLConn.Open()
trns = SQLConn.BeginTransaction
InsertCMD()
trns.Commit()
SQLConn.Close()
GetData()
bNewData = False
bEditData = False
Count()
MessageBox.Show("Record Saved Successfully", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
EnableControlsLoadMode(True)
ElseIf bEditData Then
If vilBranch.BranchNameExists(brName.Text.Trim) Then
MessageBox.Show("Branch Name [" & brName.Text.Trim & "] already exists. Please Check.", "Data Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning)
brName.Focus()
Exit Sub
Else
btnbsSave.Focus()
End If
SQLConn.Open()
trns = SQLConn.BeginTransaction
UpdateCMD()
trns.Commit()
SQLConn.Close()
GetData()
bNewData = False
bEditData = False
Count()
MessageBox.Show("Record Updated Successfully", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information)
EnableControlsLoadMode(True)
End If
Catch ex As Exception
trns.Rollback()
MessageBox.Show("Critical Error!" & ex.Message, "Critical Error.", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
I can populate the combo box but When i click on SAVE button i get this error message:
Error No: 5
Error: The statement has been terminated.
The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_hrBranchSetup_hrOrganizationSetup". The conflict occurred in
database "BDFCLHR", table "dbo.hrOrganizationSetup", column 'OsID'.
Source: Microsoft OLE DB Provider for SQL Server
When i debug this line under "Private Sub InsertCMD()"
cmd.Parameters.Add("@OsID", OleDbType.Char).Value = cboOrganization.ValueMember
i get value as "OsID" not the selected value.
PLEASE HELP ME, I AM STRUCK IN THIS AREA.
TasheeDuks