mardi 4 avril 2017

How to display details from database to the single user in mvc

I am trying to get the details from database to display in the View. Here I need to display the records for particular user who logged currently. But unfortunately all the data which ever in DB table coming into the view. But I dont want to display all the data.

In DB I have two tables for tblEmployee and TaskDetails This is My Model, public class TaskDetails { public string ProjectID { get; set; } public string ProjectName { get; set; } public DateTime StartDate { get; set; } public DateTime EstimatedDate { get; set; } public string TaskDescription { get; set; } }

    This is My controller,
To fetch the data here I am using ADO.Net,
            public ActionResult TaskDetails(string td)
            {           
                string connectionstring = "data source = NLTI37; initial catalog = Data; integrated security = True; MultipleActiveResultSets = True; App = EntityFramework";
                string sql = "select * from TaskDetail";
                SqlConnection conn = new SqlConnection(connectionstring);
                SqlCommand cmd = new SqlCommand(sql, conn);
                var Details = new List<TaskDetails>();
                { 
                    conn.Open();
                    SqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        var ProjectDetail = new TaskDetails();
                        ProjectDetail.ProjectID = rdr["ProjectID"].ToString();
                        ProjectDetail.ProjectName = rdr["ProjectName"].ToString();
                        ProjectDetail.StartDate = DateTime.Parse(rdr["StartDate"].ToString());
                        ProjectDetail.EstimatedDate = DateTime.Parse(rdr["EstimatedDate"].ToString());
                        ProjectDetail.TaskDescription = rdr["TaskDescription"].ToString();
                        Details.Add(ProjectDetail);
                    }
              }
                return View(Details);
            }
    Here I am using ADO.Net to fetching the details. And these details I need to display on the View for user who currently logged in.

    This is my View,
    <tbody>
                    @foreach (var ProjectDetail in Model)
                {   
                        <tr>
                            <td>@ProjectDetail.ProjectID</td>
                            <td>@ProjectDetail.ProjectName</td>
                            <td>@ProjectDetail.StartDate</td>
                            <td>@ProjectDetail.EstimatedDate</td>
                            <td>@ProjectDetail.TaskDescription</td>
                        </tr>
                    }
                </tbody>

Here I need to display the records of the user who loggedin currently. But unfortunately all the user details I am getting here. As per my understanding SQL statement is wrong. Correct Me if wrong. How to get the details for that particular user details.  Can any one help on this.

Aucun commentaire:

Enregistrer un commentaire