Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created a gridview with a linkbutton
ASP.NET
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButtonDeleteAssignment" runat="server" OnClick="LinkButtonDeleteAssignmentClick" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>


and i am trying to delete true that button

C#
protected void LinkButtonDeleteAssignmentClick(object sender, EventArgs e)
       {
           int index = 0;
           LinkButton lb = (LinkButton)sender;

           GridViewRow row = (GridViewRow)lb.NamingContainer;

           if (row != null)
           {
               index = row.RowIndex;
           }


               dt.Rows[index].Delete();

           GridViewShowAssignments.DataSource = dt;
           GridViewShowAssignments.DataBind();


button for adding to gridview
C#
protected void ButtonAddAssignmentClick(object sender, EventArgs e)
       {
           {
               //Lägg till i dbn o binda till gridviewn
               using (var db = new knowitCVdbEntities())
               {
                   SPWeb theSite = SPControl.GetContextWeb(Context);
                   SPUser theUser = theSite.CurrentUser;
                   string strUserName = theUser.LoginName;

                   var theEmplAssignment = (
                                               from p
                                                   in db.EMPLOYEES
                                               where p.username == strUserName
                                               select p).FirstOrDefault();

                   _emp = theEmplAssignment;

                   if (_emp != null)
                   {
                       //Create assignment
                       var myAssignment = new EMPLOYEES_ASSIGNMENT
                                              {
                                                  assigment_id = new Random().Next(),
                                                  employee_id = _emp.employee_id,

                                                  reference_name = TextBoxReference.Text,
                                                  company_name = TextBoxCompanyName.Text,
                                                  sector = TextBoxSector.Text,
                                                  area = TextBoxArea.Text,
                                                  from_date = TextBoxFromDate.Text,
                                                  to_date = TextBoxToDate.Text,
                                                  description = TextBoxDesc.Text,
                                              };

                       //Create assignment tools
                       for (int i = 0; i < ListBoxAssignmentTools.Items.Count; i++)
                       {
                           var myTool = new EMPLOYEES_ASSIGMENT_TOOLS()
                                            {
                                                assigment_tools_id = new Random().Next(),
                                                assigment_id = myAssignment.assigment_id,
                                                employee_id = myAssignment.employee_id,

                                                tools_name = ListBoxAssignmentTools.Items[i].ToString()
                                            };

                           myAssignment.EMPLOYEES_ASSIGMENT_TOOLS.Add(myTool);
                       }

                       //Create assignment technology
                       for (int i = 0; i < ListBoxAssignmentTechnology.Items.Count; i++)
                       {
                           var myTech = new EMPLOYEES_ASSIGMENT_TECHNOLOGY()
                                            {
                                                assigment_technology_id = new Random().Next(),
                                                assigment_id = myAssignment.assigment_id,
                                                employee_id = myAssignment.employee_id,

                                                technology_name = ListBoxAssignmentTechnology.Items[i].ToString()
                                            };

                           myAssignment.EMPLOYEES_ASSIGMENT_TECHNOLOGY.Add(myTech);
                       }

                       //Add assignment
                       _emp.EMPLOYEES_ASSIGNMENT.Add(myAssignment);
                       db.SaveChanges();
                       var dt = new DataTable();
                       if (Session["DataTable"] != null)
                       {

                           dt = (DataTable) Session["DataTable"];
                       }
                           //Populate gridview
                       else
                       {
                           dt.Columns.Add("Company name");
                           dt.Columns.Add("Sector");
                           dt.Columns.Add("Area");
                           dt.Columns.Add("From");
                           dt.Columns.Add("To");
                           dt.Columns.Add("Tools");
                           dt.Columns.Add("Technology");
                           dt.Columns.Add("Description");
                           dt.Columns.Add("Reference");
                           dt.Rows.Clear();

                       }



                       DataRow dr = dt.NewRow();
                       dr["Company name"] = TextBoxCompanyName.Text;
                       dr["Sector"] = TextBoxSector.Text;
                       dr["Area"] = TextBoxArea.Text;
                       dr["From"] = TextBoxFromDate.Text;
                       dr["To"] = TextBoxToDate.Text;
                       dr["Description"] = TextBoxDesc.Text;
                       dr["Reference"] = TextBoxReference.Text;

                       string sToolsValue = string.Empty;
                       for (int i = 0; i < ListBoxAssignmentTools.Items.Count; i++)
                       {
                           sToolsValue += ", " + ListBoxAssignmentTools.Items[i];
                       }
                       dr["Tools"] = sToolsValue;

                       string sTechValue = string.Empty;
                       for (int i = 0; i < ListBoxAssignmentTechnology.Items.Count; i++)
                       {
                           sTechValue += ", " + ListBoxAssignmentTechnology.Items[i];
                       }
                       dr["Technology"] = sTechValue;

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

                       GridViewShowAssignments.DataSource = dt;
                       GridViewShowAssignments.DataBind();

                       TextBoxCompanyName.Text = string.Empty;
                       TextBoxArea.Text = string.Empty;
                       TextBoxSector.Text = string.Empty;
                       TextBoxFromDate.Text = string.Empty;
                       TextBoxToDate.Text = string.Empty;
                       TextBoxDesc.Text = string.Empty;
                       TextBoxReference.Text = string.Empty;
                       ListBoxAssignmentTools.Items.Clear();
                       ListBoxAssignmentTechnology.Items.Clear();
                   }
               }

           }




       }
Posted
Updated 2-Apr-13 3:23am
v2
Comments
ZurdoDev 2-Apr-13 9:21am    
What's the issue exactly?
Kurac1 2-Apr-13 9:22am    
that i get that it cannot find rows, but i dont why because i have created them
fjdiewornncalwe 2-Apr-13 12:57pm    
I would suggest using SQLProfiler to see if the inserts you are expecting are attempting to execute on the database. If they are and you are not seeing data, you will at least be able to see the sql commands that EF is attempting to send. With that you may see what the issue is. If you don't see anything in Profiler, then it is likely that your EF model does not have a valid repository context to work with.

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