Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a gridview there i foreach all row in the grid and for each cell i add the values to the columns in the database, it seems when i save the values in the cells looses , because in sql i have no values only the column the "and they are empty", after debbuging the column in the datbase has null in all the columns.

how could i solve this?

C#
foreach (GridViewRow gvRowAssigment in GridViewAllAssigments.Rows)
            {
                var emp_assig_ref = new EMPLOYEES_ASSIGMENT_REFERENCE();
                emp_assig_ref.reference_name = gvRowAssigment.Cells[8].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();


And for adding textbox values to gridview on button i use this
C#
private void addassigment()
       {
           //var dt = new DataTable();
           if (Session["DataTable"] != null)
           {
               dt = (DataTable) Session["DataTable"];
           }
           else
           {
               DataColumn cl = new DataColumn("From");
               dt.Columns.Add(cl);
               cl = new DataColumn("To");
               dt.Columns.Add(cl);

               DataColumn cl1 = new DataColumn("Area");
               dt.Columns.Add(cl1);
               cl1 = new DataColumn("Sector");
               dt.Columns.Add(cl1);

               DataColumn cl2 = new DataColumn("CompanyName");
               dt.Columns.Add(cl2);
               cl2 = new DataColumn("Description");
               dt.Columns.Add(cl2);

               DataColumn cl3 = new DataColumn("Tools");
               dt.Columns.Add(cl3);
               cl3 = new DataColumn("Technology");
               dt.Columns.Add(cl3);

               DataColumn cl4 = new DataColumn("References");
               dt.Columns.Add(cl4);


           }



           string sToolsValue = string.Empty;
           string sTechnologyvalue = string.Empty;
           DataRow dr = dt.NewRow();
           dr[0] = TextBoxFrom.Text;
           dr[1] = TextBoxTo.Text;
           dr[2] = TextBoxArea.Text;
           dr[3] = TextBoxSector.Text;
           dr[4] = TextBoxCompanyName.Text;
           dr[5] = TextBoxDescription.Text;
           dr[8] = 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[6] = sToolsValue;
           dr[7] = sTechnologyvalue;


           dt.Rows.Add(dr);
           Session["DataTable"] = dt;
           GridViewAllAssigments.DataSource = dt;
           GridViewAllAssigments.DataBind();


and here is the code behind for the gridivew
C#
<Columns>
                                                    
                                               <asp:TemplateField HeaderText="From">
                                               <ItemTemplate>
                                               <asp:Label ID="LabelFrom"  runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"From")%>' />
                                                </ItemTemplate>
                                                </asp:TemplateField>
                                             

                                                <asp:TemplateField HeaderText="To">
                                               <ItemTemplate>
                                               <asp:Label ID="LabelTo" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"To")%>' />
                                                </ItemTemplate>
                                                </asp:TemplateField>
                                                
                                                <asp:TemplateField HeaderText="Area">
                                               <ItemTemplate>
                                               <asp:Label ID="Label3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Area")%>' />
                                                </ItemTemplate>
                                                </asp:TemplateField>
                                                
                                                <asp:TemplateField HeaderText="Sector">
                                               <ItemTemplate>
                                                   <asp:Label ID="Label4" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Sector")%>' />
                                                </ItemTemplate>
                                                </asp:TemplateField>
                                                
                                                <asp:TemplateField HeaderText="Company name">
                                               <ItemTemplate>
                                               <asp:Label ID="Label5" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CompanyName")%>'/>
                                                </ItemTemplate>
                                                </asp:TemplateField>
                                               
                                                <asp:TemplateField HeaderText="Description">
                                               <ItemTemplate>
                                               <asp:Label ID="Label6" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Description")%>'/>
                                                </ItemTemplate>
                                                </asp:TemplateField>
                                                
                                                <asp:TemplateField HeaderText="Tools">
                                               <ItemTemplate>
                                               <asp:Label ID="Label7" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Tools")%>'/>
                                                </ItemTemplate>
                                                </asp:TemplateField>
                                                
                                                <asp:TemplateField HeaderText="Technology">
                                               <ItemTemplate>
                                               <asp:Label ID="Label8" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Technology")%>'/>
                                                </ItemTemplate>
                                                </asp:TemplateField>
                                                
                                                <asp:TemplateField HeaderText="References">
                                               <ItemTemplate>
                                               <asp:Label ID="Label9" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"References")%>'/>
                                                </ItemTemplate>
                                                </asp:TemplateField>

                                                </Columns>

[Edit - further code from the OP and have added WCF to the tags]

C#
public void setEmployee(string attachmentUrl)
        {
            var db = new knowitCVdbEntities();

         // Lägger till Employee
            var emp = new EMPLOYEE
                          {

                              firstname = TextBoxFirstName.Text,
                              lastname = TextBoxLastName.Text,
                              about = TextBoxAboutYou.Text,
                              phone = TextBoxPhone.Text,
                              position = TextBoxPosition.Text,
                              email = TextBoxEmail.Text,
                              username = TextBoxUsername.Text,
                              date_of_birth = TextBoxDateOfBirth.Text,
                              image = attachmentUrl,
                              document_cv = "Document Cv"
                          };


            // lägger till languages
            foreach (var languages in ListBoxLanguages.Items)
            {
                var emplanguages = new EMPLOYEE_LANGUAGES();
                {
                    emplanguages.language_name = languages.ToString();
                }

                emp.EMPLOYEE_LANGUAGES.Add(emplanguages);
            }

            // Lägger till certifikat
            foreach (var certifications in ListBoxCertifications.Items)
            {
                var cert = new EMPLOYEES_CERTIFICATIONS();
                {
                    cert.certificate_name = certifications.ToString();
                }

                emp.EMPLOYEES_CERTIFICATIONS.Add(cert);

            }
            //Lägger till kurser
            foreach (var courses in ListBoxCourses.Items)
            {
                var course = new EMPLOYEES_COURSES();
                {
                    course.course_name = courses.ToString();

                }
                emp.EMPLOYEES_COURSES.Add(course);

            }
            // Lägger till previous work erfarenhet

            foreach (var prevworkex in ListBoxPreviousWorkExperience.Items)
            {
                var prevwork = new EMPLOYEES_PREV_WORK_EX();
                {
                    prevwork.prev_work_name = prevworkex.ToString();

                }
                emp.EMPLOYEES_PREV_WORK_EX.Add(prevwork);
            }

            //Lägger till businnes knowledge
            foreach (var businnesknow in ListBoxBusinnesKnowLedge.Items)
            {
                var businessknowledge = new EMPLOYEES_BUSINESS_KNOWLEDGE();
                {
                    businessknowledge.businees_knowledge_name = businnesknow.ToString();


                }
                emp.EMPLOYEES_BUSINESS_KNOWLEDGE.Add(businessknowledge);
            }

            //Lägger till educations
            foreach (var educ in ListBoxEducation.Items)
            {
                var education = new EMPLOYEES_EDUCATIONS();
                {
                    education.employee_education_name = educ.ToString();


                }
                emp.EMPLOYEES_EDUCATIONS.Add(education);

            }

            //Skriver ut database skill och level
            foreach (var lstdatabase in ListBoxDataBase.Items)
            {

                var mySkill = new TECHNICAL_SKILLS();
                {
                    mySkill.skill_name = lstdatabase.ToString().Split('-')[0];
                    mySkill.skill_type = LabelDataBase.Text;
                    mySkill.technical_skill_id = new Random().Next();
                }

                var myLevel = new TECHNICAL_SKILL_LEVEL();
                {
                    myLevel.skill_level = lstdatabase.ToString().Split('-')[1];
                    myLevel.technical_skill_level_id = new Random().Next();
                }

                var skill = new EMPLOYEES_TECHNICAL_SKILLS();
                {


                    skill.employee_id = emp.employee_id;
                    skill.TECHNICAL_SKILL_LEVEL = myLevel;
                    skill.TECHNICAL_SKILLS = mySkill;
                    skill.t
Posted
Updated 24-Mar-13 23:56pm
v5
Comments
[no name] 23-Mar-13 19:10pm    
Unfortunately, my mind reading helmet and my crystal ball are both in the shop this week so I would have no idea what you have done to cause this situation and what you could do to correct it.
PIEBALDconsult 23-Mar-13 20:02pm    
Some reading of documentation may help.
bbirajdar 24-Mar-13 1:47am    
Try debugging your code This may help you ^
CHill60 24-Mar-13 13:21pm    
Use the Improve question link to post the code that you are using to insert the grid rows into the database
Kurac1 24-Mar-13 14:25pm    
i have updated

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