Click here to Skip to main content
15,897,166 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two combobox on my window form, one is batch combobox when user will select a batch related students will be filled in datagridview,and other combo is for employee who will take attendance. and a datagridview , gridview has three columns one is Checkbox column and one is Text box column and third is showing students name(student column will be filled on basis of batch selection, i want at single button click to save all records in database, and also i fetched the students name from database and want to save their keys not their names, How it can be implemented,
please help,below is my code but it is not working
C#
da.ProcName = "AddStudentsAttendance";
foreach(Object items in dg.Rows)
{
    DataRow dr = items as DataRow;
    da.OpenDBConnection();
    da.CreateCommandObject();
    da.Parameters("@Date", txtDate.Text);
    da.Parameters("@keyEmployee", int.Parse(txtEmployee.SelectedValue.ToString()));
    da.Parameters("@AddmissionNo",dr["Admission_No"].ToString() );
    da.Parameters("@Present", dr["Present"].ToString());
    da.Parameters("@Reason", dr["Reason"].ToString());
    da.ExecuteNonQuery();

}
Posted
Updated 27-May-14 20:33pm
v4
Comments
DamithSL 28-May-14 2:31am    
Any exceptions?
Abdullah Kundi 28-May-14 2:36am    
Object reference not set to an instance of an object.
the error is genarated at da.parameters("@AdmissionNO",..........) line
Naz_Firdouse 28-May-14 2:44am    
Make sure there exists a column named "Admission_No" in dr. Debug and check whether 'items' contains Admission_No field.
Abdullah Kundi 28-May-14 2:48am    
I have changed the column to Student,
problem is with foreach(object item in dg.rows)
dataRow dir = item as datarow;
here in dir null value ( if debugger is used)
Naz_Firdouse 28-May-14 2:55am    
As Damith specified in the below answer, use DataGridViewRow instead of Object class

1 solution

iterate rows as below
C#
foreach (DataGridViewRow row in dg.Rows)
{
   var cellValue =row.Cells["ColumnName"].Value;
}
 
Share this answer
 
Comments
Abdullah Kundi 28-May-14 2:57am    
thanks for reply but .Cells is not supported what to do?
DamithSL 28-May-14 3:00am    
Abdullah Kundi 28-May-14 3:38am    
Thanks a lot, it works

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



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