0

I'm beginner and I need your help in this project

My idea is creating a program for a tiny company which it'll allows them to add all employees and their data to this program, modify it, delete it and extract it to an excel file. First, I created a form in which I put labels and textboxes to enter data into Data Grid View, where the project was previously linked to the Microsoft database, but I do not know how to link the TextBox box to the columns of the database so that the data is entered directly using a button and correctly so that it can be modified Later in the same way in reverse, please help me as much as you can and thank you

Imports System.Data.SqlClient
Imports System.Data

Public Class Form1
    Dim cmd As New SqlCommand
    Dim conn = New SqlConnection("Server=localhost; Data Source=DESKTOP-J9KT12C; Database=r3d$3@$taff; Integrated Security= Yes")

    Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs)




    End Sub

    Private Sub Table_1BindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Table_1BindingNavigatorSaveItem_Click_1(sender As Object, e As EventArgs) Handles Table_1BindingNavigatorSaveItem.Click


    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim table As New DataTable("table")
        table.Columns.Add("Oracle ID", Type.GetType("System.Int32"))
        table.Columns.Add("Iqama/ID", Type.GetType("System.String"))
        table.Columns.Add("Firstname", Type.GetType("System.String"))
        table.Columns.Add("Lastname", Type.GetType("System.String"))
        table.Columns.Add("Gender", Type.GetType("System.String"))
        table.Columns.Add("Mobile No.", Type.GetType("System.String"))
        table.Columns.Add("Starting Date", Type.GetType("System.DateTime"))
        table.Columns.Add("Order", Type.GetType("System.String"))

        Dim cmd As New SqlCommand("", conn)
        With cmd
            .Connection = conn
            .CommandText = "SELECT * FROM Table_1 
                            WHERE Oracle ID = @Oracle ID,
                            Iqama/ID= @Iqama/ID,
                            Firstname= @Firstname,
                            Lastname= @Lastname,
                            Gender= @Gender,
                            Mobile No.= @Mobile No.,
                            Starting Date= @Starting Date,         
                            Order= @Order"

            cmd.Parameters.Add("@Oracle ID", SqlDbType.VarChar).Value = Oracle_IDTextBox.Text
            cmd.Parameters.Add("@Iqama/ID", SqlDbType.VarChar).Value = Iqama_IDTextBox.Text
            cmd.Parameters.Add("@Firstname", SqlDbType.VarChar).Value = FirstnameTextBox.Text
            cmd.Parameters.Add("@Lastname", SqlDbType.VarChar).Value = LastnameTextBox.Text
            cmd.Parameters.Add("@Gender", SqlDbType.VarChar).Value = GenderComboBox.Text
            cmd.Parameters.Add("@Mobile No.", SqlDbType.VarChar).Value = Mobile_No_TextBox.Text
            cmd.Parameters.Add("@Starting Date", SqlDbType.DateTime).Value = Starting_DateDateTimePicker.Value
            cmd.Parameters.Add("@Order", SqlDbType.VarChar).Value = OrderTextBox.Text
        End With

        Dim da As New SqlDataAdapter
        da.SelectCommand = cmd
        Dim dt As New DataTable
        dt.Clear()
        da.Fill(dt)
        Table_1DataGridView.DataSource = dt
        Table_1DataGridView.AllowUserToAddRows = False
        Table_1DataGridView.Columns(0).HeaderText = "Oracle ID"
        Table_1DataGridView.Columns(1).HeaderText = "Iqama/ID"
        Table_1DataGridView.Columns(2).HeaderText = "Firstname"
        Table_1DataGridView.Columns(3).HeaderText = "Lastname"
        Table_1DataGridView.Columns(4).HeaderText = "Gender"
        Table_1DataGridView.Columns(5).HeaderText = "Mobile No."
        Table_1DataGridView.Columns(6).HeaderText = "Starting Date"
        Table_1DataGridView.Columns(7).HeaderText = "Order"

        conn = New SqlConnection("Server=localhost; Data Source=DESKTOP-J9KT12C; Database=r3d$3@$taff; Integrated Security= Yes")
        Dim Query As String = "SELECT * From Table_1"
        cmd = New SqlCommand(Query, conn)
        'Dim da As New SqlDataAdapter(cmd)
        'Dim dt As New DataTable()
        da.Fill(dt)
        Table_1DataGridView.DataSource = dt

    End Sub

    Private Sub MobileNo_Label_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
        cmd.CommandText = "INSERT INTO Table_1 (Oracle ID, Iqama/ID, Firstname, Lastname, Gender, Mobile No., Starting Date, Order) VALUES (@Oracle ID, @Iqama/ID, @Firstname, @Lastname, @Gender, @Mobile No., @Starting Date, @Order)"

        cmd.Parameters.Add("@Oracle ID", SqlDbType.VarChar)
        cmd.Parameters.Add("@Iqama/ID", SqlDbType.VarChar)
        cmd.Parameters.Add("@Firstname", SqlDbType.VarChar)
        cmd.Parameters.Add("@Lastname", SqlDbType.VarChar)
        cmd.Parameters.Add("@Gender", SqlDbType.VarChar)
        cmd.Parameters.Add("@Mobile No.", SqlDbType.VarChar)
        cmd.Parameters.Add("@Starting Date", SqlDbType.DateTime)
        cmd.Parameters.Add("@Order", SqlDbType.VarChar)
        cmd.Parameters("@Oracle ID").Value = Oracle_IDTextBox.Text
        cmd.Parameters("@Iqama/ID").Value = Iqama_IDTextBox.Text
        cmd.Parameters("@Firstname").Value = FirstnameTextBox.Text
        cmd.Parameters("@Lastname").Value = LastnameTextBox.Text
        cmd.Parameters("@Gender").Value = GenderComboBox.Text
        cmd.Parameters("@Mobile No.").Value = Mobile_No_TextBox.Text
        cmd.Parameters("@Starting Date").Value = Starting_DateDateTimePicker.Text
        cmd.Parameters("@Order").Value = OrderTextBox.Text
        conn.Open()
        cmd.Connection = conn

        For i As Integer = 0 To Table_1DataGridView.Rows.Count - 1
            cmd.Parameters(0).Value = Table_1DataGridView.Rows(i).Cells(0).Value
            cmd.Parameters(1).Value = Table_1DataGridView.Rows(i).Cells(1).Value
            cmd.Parameters(2).Value = Table_1DataGridView.Rows(i).Cells(2).Value
            cmd.Parameters(3).Value = Table_1DataGridView.Rows(i).Cells(3).Value
            cmd.Parameters(3).Value = Table_1DataGridView.Rows(i).Cells(4).Value
            cmd.Parameters(3).Value = Table_1DataGridView.Rows(i).Cells(5).Value
            cmd.Parameters(3).Value = Table_1DataGridView.Rows(i).Cells(6).Value
            cmd.Parameters(3).Value = Table_1DataGridView.Rows(i).Cells(7).Value
            'cmd.ExecuteNonQuery()
        Next
        MsgBox("Data Saved")
    End Sub

    Private Sub OracleIDTextBox_TextChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub Starting_DateDateTimePicker_ValueChanged(sender As Object, e As EventArgs) Handles Starting_DateDateTimePicker.ValueChanged

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Table_1BindingSource1.AddNew()
        Me.BindingNavigatorAddNewItem.Name = "bindingNavigatorAddNewItem"
        Me.BindingNavigatorAddNewItem.Text = "Add new"
    End Sub


    Private Sub Button5_Click_1(sender As Object, e As EventArgs) Handles Button5.Click
        Table_1BindingSource1.RemoveCurrent()
        Me.BindingNavigatorDeleteItem.Name = "bindingNavigatorDeleteItem"
        Me.BindingNavigatorDeleteItem.Text = "Delete"
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Me.Close()
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        'Dim Sql As String = "Select * From Table_1"
        'Dim Conn As New SqlConnection(ConnectionString)
        'Dim DataAdapter As New SqlDataAdapter(Sql, Conn)
        'Dim DS As New DataSet
        'conn.Open()
        'DataAdapter.Fill(DS, "Table_1")
        'Table_1DataGridView.DataSource = DS
        'Table_1DataGridView.DataMember = "Table_1"


    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        For i As Integer = 0 To Table_1DataGridView.Rows.Count - 1
            Dim cmd As New SqlCommand("", conn)
            cmd.CommandText = "update Table_1 set Oracle ID= '" & Table_1DataGridView.Rows(i).Cells(1).Value & "', Iqama ID='" & Table_1DataGridView.Rows(i).Cells(2).Value & "', Firstname='" & Table_1DataGridView.Rows(i).Cells(3).Value & "', Gender ='" & Table_1DataGridView.Rows(i).Cells(4).Value & "', Mobile No.= '" & Table_1DataGridView.Rows(i).Cells(5).Value & "', Starting Date= '" & Table_1DataGridView.Rows(i).Cells(6).Value & "', Order='" & Table_1DataGridView.Rows(i).Cells(7).Value & "' "
            conn.Open()

        Next
    End Sub
End Class
Steve
  • 213,761
  • 22
  • 232
  • 286
Sara
  • 9
  • 1
  • 1
  • 4
  • 1
    Sara, you are asking us to help you write your application. What you need to do is go and read up on using the Data Grid (see the documentation and examples) and how to run parameterised queries and do databinding. – Fawlty Jun 25 '22 at 14:40
  • There are many resources like https://spreadsheetlight.com/ that will simplify your task – John Cappelletti Jun 25 '22 at 17:27
  • 1
    One thing I can tell you upfront: those variable names are questionable, and should definitely not have `.` or a space or `/` in them – Charlieface Jun 26 '22 at 02:11
  • The following may be helpful: https://stackoverflow.com/questions/52898/what-is-the-use-of-the-square-brackets-in-sql-statements – Tu deschizi eu inchid Jun 26 '22 at 15:29

1 Answers1

0

Microsoft give a full and working example of databinding the grid view here. https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-bind-data-to-the-windows-forms-datagridview-control?view=netframeworkdesktop-4.8

There are a lot of examples of exporting data to excel. There is one such tutorial here: http://vb.net-informations.com/excel-2007/vb.net_export_from_database_to_excel.htm

Fawlty
  • 451
  • 2
  • 9