Save Gridview Cell to database using asp and c#
I have som textboxes and a button when pressing button, i do this
private DataTable dt = new DataTable();
private void addassigment()
{
if (Session["DataTable"] != null)
{
dt = (DataTable) Session["DataTable"];
}
else
{
dt.Columns.Add("From");
dt.Columns.Add("To");
dt.Columns.Add("Area");
dt.Columns.Add("Sector");
dt.Columns.Add("CompanyName");
dt.Columns.Add("Description");
dt.Columns.Add("References");
dt.Columns.Add("Tools");
dt.Columns.Add("Technology");
//dt.Columns.Clear();
}
string sToolsValue = string.Empty;
string sTechnologyvalue = string.Empty;
DataRow dr = dt.NewRow();
dr["From"] = TextBoxFrom.Text;
dr["To"] = TextBoxTo.Text;
dr["Area"] = TextBoxArea.Text;
dr["Sector"] = TextBoxSector.Text;
dr["CompanyName"] = TextBoxCompanyName.Text;
dr["Description"] = TextBoxDescription.Text;
dr["References"] = TextBoxReferences.Text;
for (int i = 0; i < ListBoxAssignmentTools.Items.Count; i++)
{
sToolsValue += ListBoxAssignmentTools.Items[i];
}
for (int i = 0; i < ListBoxAssignmentTechnology.Items.Count; i++)
{
sTechnologyvalue += ListBoxAssignmentTechnology.Items[i];
}
dr["Tools"] = sToolsValue;
dr["Technology"] = sTechnologyvalue;
dt.Rows.Add(dr);
Session["DataTable"] = dt;
GridViewAllAssigments.DataSource = dt;
GridViewAllAssigments.DataBind();
//This add the textbox values to the gridview
The asp code for the gridview
<Columns>
<asp:TemplateField HeaderText="From">
<ItemTemplate>
<asp:Label ID="From" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"From")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="To">
<ItemTemplate>
<asp:Label ID="To" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"To")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Area">
<ItemTemplate>
<asp:Label ID="Area" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Area")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sector">
<ItemTemplate>
<asp:Label ID="Sector" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Sector")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company name">
<ItemTemplate>
<asp:Label ID="CompanyName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CompanyName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="Description" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Description")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tools">
<ItemTemplate>
<asp:Label ID="Tools" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Tools")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Technology">
<ItemTemplate>
<asp:Label ID="Technology" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Technology")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="References">
<ItemTemplate>
<asp:Label ID="References" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"References")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
// After that i want to save the gridview cells to sql
var emp=new EMPLOYEE();
foreach (GridViewRow gvRowAssigment in GridViewAllAssigments.Rows)
{
var emp_assig_ref = new EMPLOYEES_ASSIGMENT_REFERENCE();
Label lb =(Label) (gvRowAssigment.Cells[8].FindControl("Reference"));
emp_assig_ref.reference_name = lb.Text;
emp_assig_ref.reference_id = new Random().Next();
var emp_assig_teh = new EMPLOYEES_ASSIGMENT_TECHNOLOGY();
Label lb1 = (Label)(gvRowAssigment.Cells[7].FindControl("Technology"));
emp_assig_teh.technology_name = lb1.Text;
emp_assig_teh.assigment_technology_id = new Random().Next();
var emp_assig_tools = new EMPLOYEES_ASSIGMENT_TOOLS();
Label lb2 = (Label)(gvRowAssigment.Cells[6].FindControl("Tools"));
emp_assig_tools.tools_name = lb2.Text;
emp_assig_tools.assigment_tools_id = new Random().Next();
var emp_assigment=new EMPLOYEES_ASSIGNMENT();
Label lb3 = (Label) (gvRowAssigment.Cells[0].FindControl("From"));
emp_assigment.from_date = lb3.Text;
Label lb4 = (Label)(gvRowAssigment.Cells[1].FindControl("To"));
emp_assigment.to_date = lb4.Text;
Label lb5 = (Label)(gvRowAssigment.Cells[2].FindControl("Area"));
emp_assigment.area = lb5.Text;
Label lb6 = (Label)(gvRowAssigment.Cells[3].FindControl("Sector"));
emp_assigment.sector = lb6.Text;
Label lb7 = (Label)(gvRowAssigment.Cells[4].FindControl("CompanyName"));
emp_assigment.company_name = lb7.Text;
Label lb8 = (Label)(gvRowAssigment.Cells[5].FindControl("Description"));
emp_assigment.description = lb8.Text;
emp_assigment.employee_id = theEmp.employee_id;
emp_assigment.assigment_id = new Random().Next();
emp_assigment.EMPLOYEES_ASSIGMENT_TOOLS = emp_assig_tools;
emp_assigment.assigment_tools_id = emp_assig_tools.assigment_tools_id;
emp_assigment.EMPLOYEES_ASSIGMENT_TECHNOLOGY = emp_assig_teh;
emp_assigment.assigment_technology_id = emp_assig_teh.assigment_technology_id;
emp_assigment.EMPLOYEES_ASSIGMENT_REFERENCE = emp_assig_ref;
emp_assigment.reference_id = emp_assig_ref.reference_id;
emp.EMPLOYEES_ASSIGNMENT.Add(emp_assigment);
}
db.AddToEMPLOYEES(emp);
db.SaveChanges();
The problem is after saving i dont get the any values in the database och null on every cell, but i dont now why?