Combo box shows default index(-1)

  • Thread starter Thread starter Habib ur rehman
  • Start date Start date
H

Habib ur rehman

Guest
Hi,

I am making a selection in Combo box while retrieving data.

During this retrieval the combo box is changing its index to default(-1) or something is making this change.

I tried to find what is causing this issue using step by step debugging but didn't caught anything.

I checked the values properly loads into combo box using Step by step debugging.

The combo box does not contain my value while confirming via code in the retrieval process, but the value is shown(Not Selected) in the combo box when retrieval process ends.

"UD_CB_CNIC.SelectedItem = CusmrCnic" This is the combo box where the problem is occurring.

Here is my code if someone point out my mistake. Also I need to know is there any method that could reveal this problem.

I deleted the entire code and rewrite from the begging but the problem is still there.

Private Sub Btn_UD_Load_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_UD_Load.Click

Using cmd As New SqlCommand
Dim transaction As SqlTransaction
If con.State = ConnectionState.Closed Then con.Open() 'Connection Open
transaction = con.BeginTransaction("SampleTransaction")
cmd.Connection = con
cmd.Transaction = transaction
UD_DGV_Items.Rows.Clear()
Try
cmd.CommandText = "Select * from SR where Invoice_ID = '" & TB_UD_Load.Text.Trim() & "'"
Using dr = cmd.ExecuteReader()
If dr.Read() Then
UD_TB_SR.Text = dr.Item("Invoice_ID")
ID = dr.Item("CID")
UD_TB_IN.Text = dr.Item("Invoice_No")
UD_DTP_Inv_Date.Text = dr.Item("Invoice_Date")
Dim data As Byte() = DirectCast(dr("Picture"), Byte())
Dim ms As New MemoryStream(data)
UD_PictureBox.Image = Image.FromStream(ms)
End If
End Using

cmd.CommandText = "Select * from SRItems where Invoice_ID = '" & TB_UD_Load.Text.Trim() & "'"
Using dr = cmd.ExecuteReader()
While dr.Read
ItmNo = dr("Item_No")
Ietm = dr("Item")
mitrs = dr("Meters")
Price = dr("ItemTAmount")
Dim row As String()
row = New String() {ItmNo, Ietm, mitrs, Price}
UD_DGV_Items.Rows.Add(row)
UD_DGV_Items_Edit.Text = "Edit"
UD_DGV_Items_Edit.UseColumnTextForButtonValue = True
UD_DGV_Items_Del.Text = "Delete"
UD_DGV_Items_Del.UseColumnTextForButtonValue = True
End While
PreviousRows = UD_DGV_Items.RowCount - 1
End Using
cmd.CommandText = "Select * from SRPayment where Invoice_ID = '" & TB_UD_Load.Text.Trim() & "'"
Using dr = cmd.ExecuteReader()
If dr.Read() Then
UD_ChkBox = dr.Item("Payment_Method")
If UD_ChkBox = UD_RB_Cash.Text Then
UD_RB_Cash.Checked = True
ElseIf UD_ChkBox = UD_RB_CashNChq.Text Then
UD_RB_CashNChq.Checked = True
ElseIf UD_ChkBox = UD_RB_CshNChqNCred.Text Then
UD_RB_CshNChqNCred.Checked = True
ElseIf UD_ChkBox = UD_RB_CashNCred.Text Then
UD_RB_CashNCred.Checked = True
ElseIf UD_ChkBox = UD_RB_Cheque.Text Then
UD_RB_Cheque.Checked = True
ElseIf UD_ChkBox = UD_RB_ChqNCred.Text Then
UD_RB_ChqNCred.Checked = True
ElseIf UD_ChkBox = UD_RB_Credit.Text Then
UD_RB_Credit.Checked = True
End If
UD_TB_PID.Text = dr.Item("Payment_ID")
UD_TB_SRTA.Text = dr.Item("SRTAmount")
UD_TB_AmntPad.Text = dr.Item("CashPaid")
UD_TB_ChqPad.Text = dr.Item("ChequePaid")
UD_TB_InvCredit.Text = dr.Item("Invoice_Credit")
UD_TB_RecName.Text = dr.Item("Receivers_Name")
UD_TB_ChqNo.Text = dr.Item("ChequeNo")
UD_DTP_ChqDate.Text = dr.Item("ChequeDate")
UD_TB_BnkNam.Text = dr.Item("BankName")
UD_DTP_PDD.Text = dr.Item("PaymentDeliveryDate")
End If
End Using
transaction.Commit()
With CNameCNICByCID(ID)
CusmrName = .Item1
CusmrCnic = .Item2
End With

UD_CB_CrName.SelectedItem = CusmrName
CustCNIC()
UD_CB_CNIC.SelectedItem = CusmrCnic 'Here is the problem

UD_TB_PrevBalance_Enter(sender, e)
UD_TB_TBalance_Enter(sender, e)
MessageBox.Show("""Invoice"" has been loaded Successfully.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(String.Format("Error: {0}", ex.Message))
Try
transaction.Rollback()
Catch ex2 As Exception
MessageBox.Show(String.Format("Rollbacking: {0}", ex2.Message))
End Try
End Try
End Using
End Sub


Private Sub CustCNIC()
Dim caler As System.Reflection.MethodBase
caler = New System.Diagnostics.StackTrace().GetFrame(1).GetMethod()
Try
If caler.Name = "CB_CNIC_Enter" And CB_CrName.SelectedItem <> Nothing Then
Using com As New SqlCommand("Select CNIC from Customers where CName = " _
& "'" & CB_CrName.SelectedItem & "' ORDER BY CName ASC", con)
If con.State = ConnectionState.Closed Then con.Open() 'Connection Open
Using dr = com.ExecuteReader
CB_CNIC.Items.Clear()
While dr.Read
CB_CNIC.Items.Add(dr("CNIC"))
End While
End Using
End Using
ElseIf caler.Name = "UD_CB_CNIC_Enter" And UD_CB_CrName.SelectedItem <> Nothing Or caler.Name = "Btn_UD_Load_Click" Then
Using com As New SqlCommand("Select CNIC from Customers where CName = " _
& "'" & UD_CB_CrName.SelectedItem & "' ORDER BY CName ASC", con)
If con.State = ConnectionState.Closed Then con.Open() 'Connection Open
Using dr = com.ExecuteReader
UD_CB_CNIC.Items.Clear()
While dr.Read
UD_CB_CNIC.Items.Add(dr("CNIC"))
End While
End Using
End Using
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub UD_TB_PrevBalance_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UD_TB_PrevBalance.Enter

If UD_CB_CrName.SelectedItem = Nothing Or UD_CB_CNIC.SelectedItem = Nothing Then
MessageBox.Show("Please select Customer/CNIC No.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Hand)
Else
CustID = CusID(UD_CB_CrName.SelectedItem, UD_CB_CNIC.SelectedItem)
UD_TB_PrevBalance.Text = PrevBlnc(CustID)
End If

End Sub

Private Sub UD_TB_TBalance_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UD_TB_TBalance.Enter
If UD_TB_SRTA.Text <> "" And UD_TB_PrevBalance.Text <> "" Then
Me.UD_TB_TBalance.Text = CULng(UD_TB_InvCredit.Text) + CULng(UD_TB_PrevBalance.Text)
End If
End Sub



Thanks in advance.
Habib Ur Rehman

Continue reading...
 
Back
Top