0

Im uploading student details that are tab seperated within a text file. If a submitted ID was added sucessfully to the database i want to add the submitted details to a gridview to allow the admin to see the total of submitted students.

                    //student details from the submitted text file
                    var name = (Convert.ToString(splitString[0]));
                    var lname = (Convert.ToString(splitString[1]));
                    int stud_id = (Convert.ToInt32(splitString[2]));
                    var tutor = (Convert.ToString(splitString[3]));
                    int? count = 0;
                    int? studsAdded = 0;                        

                    using (apiUsersDataContext api = new apiUsersDataContext())
                    {
                        var result = api.importStudents(name, lname, stud_id, tutor, ref count, ref studsAdded);
                        //submitted the details of a single student to the stored procedure.

                        if (studsAdded != 0) //if a student was added
                        {
                            var data = from p in api.getUserbyID(studsAdded) //get the new students details from the database
                                       select new
                                       {
                                           p.stud_id,
                                           p.stud_Fname,
                                           p.stud_Lname,
                                           p.stud_Tutor
                                       };

                                       GV_submitted.DataSource = Data;
                          }

                          GV_submitted.DataBind();

i now want to bind the details of the student to a gridview, and then repeat the process for the next student, if i simply select datasource it only remembers the last student.

i have also tried to add the details to a list<> but again only recoreded the last student. Anyone got any ideas how i can submit each student to a gridview?

Regards.

1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by barriegrant1
0

Ok so after a number of hours, i discovered how to solve the issue using an array.

        DataTable dt = new DataTable();
        dt.Columns.Add("ID", Type.GetType("System.String"));
        dt.Columns.Add("FirstName", Type.GetType("System.String"));
        dt.Columns.Add("LastName", Type.GetType("System.String"));
        dt.Columns.Add("Tutor", Type.GetType("System.String"));


         foreach (var item in data) //foreach of the student details
               {                                    
                  string[,] arrMultiD = 
                  {
                     {item.stud_id.ToString(),item.stud_Fname.ToString(),item.stud_Lname.ToString(),item.stud_Tutor.ToString()} //store details in an array                                   
                  };                                    

                 for (int i = 0; i < arrMultiD.GetLength(0); i++) //for each item in the array 
                     {
                        dt.Rows.Add();//create a new row
                        dt.Rows[dt.Rows.Count - 1]["ID"] = arrMultiD[i, 0]; //add the details 
                        dt.Rows[dt.Rows.Count - 1]["FirstName"] = arrMultiD[i, 1];
                        dt.Rows[dt.Rows.Count - 1]["LastName"] = arrMultiD[i, 2];
                        dt.Rows[dt.Rows.Count - 1]["Tutor"] = arrMultiD[i, 3];                                        
                      }

                      gridview1.DataSource = dt;
                      gridview1.DataBind();
                }

asp.net page is as follows

    <asp:GridView ID="gridview1" runat="server"
    AutoGenerateColumns = "false" Width="100%"  
     BackColor="White" GridLines="None" 
    CellPadding="1"  ForeColor="Black">    
    <FooterStyle BackColor="#353535" BorderStyle="None"></FooterStyle>
    <PagerStyle ForeColor="Black" HorizontalAlign="Right" Width="10" BackColor="#F7F7DE"></PagerStyle>
    <HeaderStyle ForeColor="#c8e2ec" Font-Bold="True" BackColor="#2f385a"></HeaderStyle>
    <AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
   <Columns>
   <asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-Width = "150px"
     DataField = "ID" HeaderText = "Student ID" />
    <asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-Width = "150px"
     DataField = "FirstName" HeaderText = "First Name" />
    <asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-Width = "150px"
     DataField = "LastName" HeaderText = "Last Name" />
    <asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-Width = "150px"
     DataField = "Tutor" HeaderText = "Tutor Group" />    
   </Columns>
   <SelectedRowStyle ForeColor="White" Font-Bold="True" 
         BackColor="#CE5D5A"></SelectedRowStyle>
    <RowStyle BackColor="#f4f4f4"></RowStyle>
    <EmptyDataTemplate> <!-- no items --> 
   <a style="margin-left:10px;">Nothing Submitted</a>  
    </EmptyDataTemplate>
</asp:GridView>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.