I AM A NEWBIE TO VB.NET. RECENTLY I HAVE STARTED PROGRAMING AN APPLICATION CALLED "OFFICE-MANAGER". THIS IS A PROGRAM WHICH DEALS WITH ACCESS 2003 DATABASE. I HAVE A VARIETY OF OPTIONS LIKE FILE MANAGER, MAIL MANAGER, ETC. I AM STILL AT THE STARTING LEVEL. BASIC USER INTERFACE IS READY AND CURRENTLY I AM WORKING ON WRITING CODES FOR ADDING NEW FILES AND VIEWING THEM IN TWO DIFFERENT FORMS.

MY FIRST PROBLEM IS WITH ADDING NEW FILES TO DATABASE. MY CODE GOES LIKE THIS: -

Imports System.Windows.Forms
Imports System.Data.OleDb

Public Class formcuFiles
  Dim dc As OleDbCommand
  Dim da As OleDbDataAdapter
  Dim cb As OleDbCommandBuilder
  Dim conn As OleDbConnection
  Dim dt As DataTable
  Dim sql as String
  Dim i as Integer

  Private Sub Add_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
    Dim dateStr As String = dateFormater.customizeDate(Me.dtopen.Value)
    conn = New OleDbConnection(My.Settings.officeDataCon)
    conn.open()
    Dim a1, a2, a3, a4, a5, a6, a7 as String
    a1 = fileno.Text.ToString
    a2 = ref.Text.ToUpper.ToString
    a3 = part.Text.ToUpper.ToString
    a4 = sclass.Text.ToUpper.ToString
    a5 = subject.Text.ToUpper.ToString
    a6 = dateStr.ToUpper.ToString
    a7 = remks.Text.ToUpper.ToString
    'Whereas fileno, ref, part, sclass, subject, dtopen, remks are Textboxes
    'dtopen is a date field
    Try
      sql = "INSERT INTO CUFILES (FILE_NO, REFERENCE, PART, SCLASS, SUBJECT, DATE_OPENED, REMARKS) VALUES ('" & a1 & "', '" & a2 & "', '" & a3 & "', '" & a4 & "', '" & a5 & "', '" & a6 & "', '" & a7 & "')"
      dc = New OleDbCommand(sql, conn)
      i = dc.ExecuteNonQuery()
    Catch ex As Exception
      Trace.WriteLine(ex.ToString)
    Finally
      dc.Dispose()
      conn.Close()
      conn.Dispose()
    End Try
    viewCuFiles.Refresh()
    dialogDone.Show()
    'this shows a dialog that the requested operation is successfully completed
    Me.Close()
  End Sub
End Class

IN THE ABOVE CODE EVERYTHING GOES ON CORRECTLY AND EVEN IN THE VIEWCUFILES FORM THE DATE IS SHOWN IN THE LAST ROW. HOWEVER, AFTER CLOSING THE VIEWCUFILES & OPENING IT TWICE, THE NEW DATA VANISHES. ALSO, WHEN I HAVE CHECKED THE MDB FILE, THE NEWLY POSTED DATA IS NOT BEING SAVED THERE. I AM ABSOLUTELY SURE THAT THE CONNECTION IS OK. THE APPLICATION IS DEFINETLY INTERACTING WITH THE DATABASE, HOWEVER, ITS NOT SAVING THE DATA.

MY SECOND PROBLEM WITH GIVING A VALUE TO THE EDIT BUTTONS IN THE FILE VIEW FORM. HERE I AM CREATING NEW BUTTONS WITH NAME 'EDITBTN', I HAVE ALSO WRITTEN A BUTTON CLICK EVENT HANDLER FOR THIS. THE PROBLEM IS THAT FOR EACH ROW A NEW EDITBTN IS ADDED IN THE LAST COLUMN OF THAT ROW. AND EACH TIME THIS BUTTON IS ADDED I WANT THAT BUTTON TO PERFORM AN ACTION, BUT UNFORTUNATELY ONLY THE BUTTON ADDED IN THE LAST ROW IS PERFORMING IT.

I HAVE A VIEWCUFILES FORM IN WHICH I HAVE PUT A TABLELAYOUTPANEL WITH 1 ROW AND 9 COLUMNS. MY CODE WRITTEN BELOW SHOULD PICKUP DATA CORRESPONDING TO EACH COLUMN AND PUT THEM IN THE TABLELAYOUTPANEL AND A NEW ROW SHOULD BE CREATED IF THERE ARE MORE THAN ONE ROW IN THE DATABASE.

I HAVE WRITTEN THE FOLLOWING CODE FOR IT.

Imports System.Data.OleDb

Public Class viewCuFiles
  Private dataTable1 As DataTable
  Dim fileidStr as String
  Friend WithEvents editBtn As System.Windows.Forms.Button

  Private Sub viewCuFiles_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    dataTable1 = fetchCUFileList()
    For i As Integer = 0 To dataTable1.Rows.Count - 1
      For j As Integer = 0 to dataTable1.Columns.Count - 1
        Dim lblData As System.Windows.Forms.Label
        lbldata = New System.Windows.Forms.Label
        If j = 0 Then
          lblData.Text = dataTable1.Rows(i)("file_id").ToString
        ElseIf j = 1 Then
          lblData.Text = dataTable1.Rows(i)("file_no").ToString
        ElseIf j = 2 Then
          lblData.Text = dataTable1.Rows(i)("reference").ToString
        ElseIf j = 3 Then
          lblData.Text = dataTable1.Rows(i)("part").ToString
        ElseIf j = 4 Then
          lblData.Text = dataTable1.Rows(i)("sclass").ToString
        ElseIf j = 5 Then
          lblData.Text = dataTable1.Rows(i)("subject").ToString
        ElseIf j = 6 Then
          lblData.Text = dataTable1.Rows(i)("date_opened").ToString
        ElseIf j = 7 Then
          lblData.Text = dataTable1.Rows(i)("remarks").ToString
        End If
        TableLayoutPanel2.Controls.Add(lblData, j, i)
        fileidStr = dataTable1.Rows(i)("file_id").ToString
      Next
      editBtn = New System.Windows.Forms.Button
      editBtn.Text = fileidStr
      TableLayoutPanel2.Controls.Add(editBtn, 9, i)
    Next
  End Sub

  Private Function fetchCUFileList() As DataTable
    Dim conn As OleDbConnection
    conn = New OleDbConnection(My.Settings.officeDataCon)
    conn.Open()
    Try
      Dim sql As String = "SELECT * FROM CUFILES ORDER BY FILE_ID"
      Dim da As OleDbDataAdapter = New OleDbDataAdapter(sql, conn)
      Try
        da.Fill(dt)
      Finally
        da.Dispose()
      End Try
      Return dt
    Finally
      conn.Close()
      conn.Dispose()
    End Try
  End Function

  Private Sub editBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles editBtn.Click
    MsgBox("You Clicked on = " & fileidStr)
  End Sub
End Class

I WANT THE EDIT BUTTON AVAILABLE IN ALL ROWS TO PERFORM AN ACTION I.E. TO OPEN A FORM CALLED UPDCUFILES AND PASS THE FILE_ID THERE SO THAT THE UPDCUFILES FORM SHOWS ME ALL THE DETAILS OF THE PARTICULAR FILE IN TEXTBOXES, WHICH I WANT TO DESIGN IN MY OWN STYLE.

I HAVE TRIED MY LEVEL BEST, BUT I AM UNABLE TO SOLVE THIS ISSUE. IT IS THE MAIN THING WHICH LETS ME GO THROUGH A LONG WAY IN MY APPLICATION.

ANY HELP IN THIS REGARD IS HIGHLY SOLICITED.

THANKS IN ADVANCE.

After some r&d, I some how figured out a way to to get the editBtns successfully perform the required action. In my above coding after

TableLayoutPanel2.Controls.Add(editBtn, 9, i)

I have added the following code

AddHandler editBtn.Click AddressOf editBtn_Click

Even after this, a new problem arises. When I click on the editbtn in the last row, it fires two editBtn_Click events, because of which two updcufile forms are opened. Above the last row all the buttons are working perfectly, i.e. firing only one instance of updcufile form.

I dont want the editbtn in the last row to perform twice on a single click. How to stop this ?

Also when I have published the application and tried it, my coding given above for inserting data into database was working perfectly. however, i have noticed that it is not working in the Debug mode.

advise if you are able to add new records in database.
if not then today i replied for 2 threads, go through sane and apply. its very simple coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.