Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server WCF
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?
 
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
 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
<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]
 
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 23-Mar-13 14:08pm
Kurac1635
Edited 25-Mar-13 0:56am
v5
Comments
ThePhantomUpvoter at 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 at 23-Mar-13 20:02pm
   
Some reading of documentation may help.
aspnet_regiis -i at 24-Mar-13 1:47am
   
Try debugging your code This may help you ^
CHill60 at 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 at 24-Mar-13 14:25pm
   
i have updated
CHill60 at 24-Mar-13 14:33pm
   
There's no code to do any inserts to the database - that might be your problem?!?!
Kurac1 at 24-Mar-13 14:37pm
   
it is i foreach the gridivew and want to insert the cells?
CHill60 at 24-Mar-13 14:41pm
   
Is it the line db.SaveChanges(); that saves the changes to the database? What is db, what code is in SaveChanges()
Kurac1 at 24-Mar-13 14:38pm
   
like here
var emp_assig_ref = new EMPLOYEES_ASSIGMENT_REFERENCE();
emp_assig_ref.reference_name = gvRowAssigment.Cells[8].Text;
 
i am trying to insert cells 8 in the column rdeference_name in the database?
CHill60 at 24-Mar-13 14:43pm
   
That's assigning it to a property of an object - there is no code shown here that will write to a sql server database
Kurac1 at 24-Mar-13 14:57pm
   
alright , how should i do then?
CHill60 at 24-Mar-13 19:15pm
   
what is db? Is db.SaveChanges() what is supposed to save to the database? If so then share the code in that function
Kurac1 at 25-Mar-13 4:19am
   
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
CHill60 at 25-Mar-13 5:57am
   
I've amended your question to add the code from this comment and also added WCF to the tags. I was basing my questions on C# and SQL-Server only. Hopefully now someone who knows WCF will spot this at the top of the list

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

  Print Answers RSS
0 Maciej Los 315
1 OriginalGriff 233
2 Aajmot Sk 224
3 Richard MacCutchan 220
4 Marcin Kozub 210
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 25 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100