Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 foreach gridviewrows and cells to add in database

Author  Topic 

mamor90
Starting Member

5 Posts

Posted - 2013-03-23 : 16:43:45
i have two textboxes and 1 button that addes the values from the two textboxes in a gridview, now i am trying to
foreach (GridViewRow gvRowAssigment in GridViewAllAssigments.Rows)
// and then cell for cell in the gridview add them to columns in my database like this
var emp_assig_ref = new EMPLOYEES_ASSIGMENT_REFERENCE();
emp_assig_ref.reference_name = gvRowAssigment.Cells[0].Text;

But i dont gent any values in the columns its empty , what could be the problem?

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-23 : 17:13:15
Don't know if this is the only problem or not, but you probably need curly braces as shown below in red:
foreach (GridViewRow gvRowAssigment in GridViewAllAssigments.Rows)
{
// and then cell for cell in the gridview add them to columns in my database like this
var emp_assig_ref = new EMPLOYEES_ASSIGMENT_REFERENCE();
emp_assig_ref.reference_name = gvRowAssigment.Cells[0].Text;
}
Go to Top of Page

mamor90
Starting Member

5 Posts

Posted - 2013-03-23 : 17:38:55
i have it , here u can se hole my code

foreach (GridViewRow gvRowAssigment in GridViewAllAssigments.Rows)
{
var emp_assig_ref = new EMPLOYEES_ASSIGMENT_REFERENCE();
emp_assig_ref.reference_name = gvRowAssigment.Cells.Text;
emp_assig_ref.reference_id = new Random().Next();

var emp_assig_teh = new EMPLOYEES_ASSIGMENT_TECHNOLOGY();
emp_assig_teh.technology_name = gvRowAssigment.Cells[7].Text;

emp_assig_teh.assigment_technology_id = new Random().Next();

var emp_assig_tools = new EMPLOYEES_ASSIGMENT_TOOLS();
emp_assig_tools.tools_name = gvRowAssigment.Cells[6].Text;
emp_assig_tools.assigment_tools_id = new Random().Next();


var emp_assigment = new EMPLOYEES_ASSIGNMENT

{

from_date = gvRowAssigment.Cells[0].Text,
to_date = gvRowAssigment.Cells[1].Text,
area = gvRowAssigment.Cells[2].Text,
sector = gvRowAssigment.Cells[3].Text,
company_name = gvRowAssigment.Cells[4].Text,
description = gvRowAssigment.Cells[5].Text,


employee_id = emp.employee_id,
assigment_id = new Random().Next(),


EMPLOYEES_ASSIGMENT_TOOLS = emp_assig_tools,

assigment_tools_id = emp_assig_tools.assigment_tools_id,

EMPLOYEES_ASSIGMENT_TECHNOLOGY = emp_assig_teh,
assigment_technology_id = emp_assig_teh.assigment_technology_id,

EMPLOYEES_ASSIGMENT_REFERENCE = emp_assig_ref,
reference_id = emp_assig_ref.reference_id

};



emp.EMPLOYEES_ASSIGNMENT.Add(emp_assigment);
}




db.AddToEMPLOYEES(emp);
db.SaveChanges();
}
Go to Top of Page
   

- Advertisement -