Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have datagridview with checkbox, checkbox represent as attendence of employee when admin check all or some checkboxes and click on submit button. according to selected checkbox in Datagridview values of present and absent is inserted in database table.
how to do this m try this but m getting failed
help me...

this function is called at form_load event to bind tables with datagridview ,with checkBox i have created at dynamic

VB
Private Sub LoadRecord()

        cmdselect = "select ID,Name,Gender,Position from tblcouriercompany"
        da = New SqlDataAdapter(cmdselect, con)
        dt = New DataTable()
        da.Fill(dt)
        DataGridView1.DataSource = dt

    End Sub


https://www.dropbox.com/s/r7fwhlllrkerlli/Untitled.png?m=[^]

form looks like this please help me in coding m not understanding how to link checkbox with database column
Posted
Comments
Krunal Rohit 2-Mar-14 7:06am    
Is it asp or form ?

-KR
Tarun Jaiswal 2-Mar-14 7:15am    
its windows form .resx lang written in vb.net
if you know solution then plz help me

Please Create two tables.

1. mst_employees

SQL
USE [MyDatabase]
GO

/****** Object:  Table [dbo].[mst_employees]    Script Date: 03/02/2014 19:11:10 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[mst_employees](
    [Emp_ID] [int] NULL,
    [Name] [varchar](50) NULL,
    [Gender] [char](10) NULL,
    [Position] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO






2. emp_attendance (Sql script is here)

SQL
USE [MyDatabase]
GO

/****** Object:  Table [dbo].[emp_attendance]    Script Date: 03/02/2014 19:12:16 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[emp_attendance](
    [Att_ID] [bigint] IDENTITY(1,1) NOT NULL,
    [Emp_ID] [int] NULL,
    [Att_Date] [date] NULL,
    [Present_Absent] [int] NULL
) ON [PRIMARY]

GO




VB
Imports System.Data
Imports System.Data.SqlClient

Public Class Form7
    Public oCn As New System.Data.SqlClient.SqlConnection("Data Source=(local);Initial Catalog=MyDatabase;Uid=sa")

    Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
    End Sub

    Sub LoadData()
        If oCn.State = ConnectionState.Closed Then
            oCn.Open()
        End If
        Dim cmd As New SqlClient.SqlCommand("Select * from mst_employees", oCn)
        Dim da As New SqlClient.SqlDataAdapter(cmd)
        Dim ds As New DataSet("bpl")
        Dim i As Integer = 0
        Me.DataGridView1.Rows.Clear()
        Try
            da.Fill(ds, "bpl")
            If ds.Tables(0).Rows.Count > 0 Then
                While (i <> ds.Tables(0).Rows.Count)
                    Me.DataGridView1.Rows.Add()
                    Me.DataGridView1.Item(0, i).Value = i + 1
                    Me.DataGridView1.Item(1, i).Value = ds.Tables(0).Rows(i).Item("Emp_id").ToString
                    Me.DataGridView1.Item(2, i).Value = ds.Tables(0).Rows(i).Item("Name").ToString
                    Me.DataGridView1.Item(3, i).Value = ds.Tables(0).Rows(i).Item("Gender").ToString
                    Me.DataGridView1.Item(4, i).Value = ds.Tables(0).Rows(i).Item("Position").ToString
                    Me.DataGridView1.Item(5, i).Value = Format(Date.Today, "dd-MMM-yyyy")
                    i = i + 1
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
        Dim oCom As New SqlClient.SqlCommand
        Dim oRead As SqlClient.SqlDataReader
        If oCn.State = ConnectionState.Closed Then
            oCn.Open()
        End If
        Dim i As Integer = 0

        oCom.Connection = oCn
        If Me.DataGridView1.Rows.Count > 0 Then
            While (i <> Me.DataGridView1.Rows.Count)
                oCom.CommandText = "Insert into emp_attendance(Emp_ID,Att_Date,Present_Absent) Values(" & Me.DataGridView1.Item(1, i).Value & ",'" & Me.DataGridView1.Item(5, i).Value & "'," & IIf(Me.DataGridView1.Item(6, i).Value = True, 1, 0) & ")"
                oRead = oCom.ExecuteReader()
                oRead.Close()
                i = i + 1
            End While
        End If

    End Sub

    Private Sub btn_checkall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_checkall.Click
        Dim i As Integer = 0
        If Me.DataGridView1.Rows.Count > 0 Then
            While (i <> Me.DataGridView1.Rows.Count)
                Me.DataGridView1.Item(6, i).Value = True
                i = i + 1
            End While
        End If
    End Sub
End Class
 
Share this answer
 
hey sohail awr m trying to attendence to coding you provided me
but unfurtunately its not working here is video of my screen the how it doesnt work
help me to solve this error, i also tried to solve error but i failed

https://www.dropbox.com/s/ueijyare9ii4u13/attendence.mp4[^]
 
Share this answer
 
Comments
sohail awr 3-Mar-14 10:41am    
I am unable to view your video. you just drop an error screen shot as image so I will be able to find it. But I am giving some steps you use these steps and try the above code.

Step - 1

Create a Database Named: MyDatabase

Step - 2

Copy and paste of Create Table Query both queries step by step and execute it.

After you have successfully created tables.

Insert some data in mst_employees table manually. The table emp_attendance should be empty.

Step - 3

Create a form in your solution Named Form7

Add following controls in the form

1. One Datagridview Named 'Datagridview1' with 7 columns the last column should be Checkbox Column

2. Add Two Buttons - Named 'btn_submit' and 'btn_checkall'


Now Copy the above form7 code and paste it on your Form7 code remember first remove all lines available in the code section of your form7 then paste my code.

Now build your solution if any errors found then drop your error sreenshot so I will be able to give more help.
Tarun Jaiswal 5-Mar-14 6:36am    
hey bro thank you u r co-operating with me.
now i have just created a new project with same name and attribute you said actually just like your codes xerox copy but then also there is error
here snapshot of my screen
error screen
https://www.dropbox.com/s/f78bhtkpsbh6mvd/attendenceerror.png?m=

this is database screen
https://www.dropbox.com/s/7hrf085kmnd7uri/attendencedatabase.png
sohail awr 5-Mar-14 9:39am    
The main reason is in Attendance date field. So check your table emp_attendance - att_date

It its data type is datetime so change it to date. If again this error occurs so change it as varchar(10).

I have seen screen shot where you can see why query is failed due to Conversion from date time to string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900