[RESOLVED]bind data to a view in aspx page in asp.net mvc using C# and sql server stored prodcedure

Hi Experts,

I am totally new to ASP.NET MVC.

So Please try to help me out here?

I Created a ASP.NET MVC 4 Application with ASPX view.

I have a database and have a stored procedure in it named usp_getstudents which retrieve all the student information like Studentid, studentname , studentage and studentemail.

I want to display these details in  a view.

So how to do achieve this using Model, control and View.

How the design should be?

Can any one help me here with sample code snippets.

I am using ASP.NET MVC,C# and ASPX views and SQL Server Stored Procedure

Thanks a lot in advance.

 

 

Hi,

As you are using ASPX view, you can probably use GridView kind of controls as explained in this article:

http://www.binaryintellect.net/articles/59b91531-3fb2-4504-84a4-9f52e2d65c20.aspx

Note: Source code can also be downloaded at the end of the page.

Thanks for your valuable reply. But Sorry as this was not the concern i asked. It was just to display some text from database in html in MVC.

Hi,

In fact the example has asp:GridView control which is a bit advanced control.

You can even place asp:Label controls in the page and you can fill in Page_Load event (If you see in the example, gridView is being populated with it’s DataSource in Page_Load event).

In your case, you would want to just set the Text properties to the appropriate values from DB.

With in page_load you can invoke your DB layer to get the required objects and fill in the UI controls (setting Text properties for example) with the returned object from Data access layer.

The close sample I could find is of MVC2 (as most of the samples are on razor) but this article is a good starter to understand how MVC works (especially for aspx engine) across layers in single article:
http://weblogs.asp.net/scottgu/asp-net-mvc-framework-part-1

There are quite a few things changed though later, for example, ControllerAction attribute is not required now, RenderView method is replaced with View() method etc.

If you would like to see the current syntax for aspx engine, you can try Scaffolding feature and generate the code and look at the generated code for View and you can get an idea of the syntax and samples more.

Also note, in the example, Entity Framework is used, but you can write ADO.NET code or use any other convenient data access frameworks to accomplish the task according to your requirement.

Hi vickeynet,

For this requirement, you could use WebGird.

# WebGrid with custom paging and sorting in MVC4

http://www.dotnet-tricks.com/Tutorial/mvc/GK2W110113-WebGrid-with-custom-paging-and-sorting-in-MVC4.html

Best Regards

Starain

Hello Vickey,

There are plenty of ways to achieve this requirement , a common implementation is shown below:try to understand:

1) Create a model that will help communicate values between view and the controller.

public class Student
{
    public string StudentID {get;set;}
    public string StudentName {get;set;}    
    ....
}

2) Get the values from database in a specific controller:

public ActionResult Students()
{
    String connectionString = "<THE CONNECTION STRING HERE>";
    String sql = "SELECT * FROM students";
    SqlCommand cmd = new SqlCommand(sql, conn);

    using(SqlConnection conn = new SqlConnection(connectionString))
    {
        conn.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        var model = new List<Student>();
        while(rdr.Read())
        {
            var student = new Student();
            student.StudentID= rdr["StudentID"];
            student.StudentName= rdr["StudentName"];           
            ....
            model.Add(student);
        }
    }
    return View(model);
}

3) Lastly take the records and display it in respective view:

@model List<Student>

<h1>Student</h1>

<table>
    <tr>
        <th>StudentID</th>
        <th>StudentName</th>
        ...
    </tr>
    @foreach(var student in Model)
    {
    <tr>
        <td>@student.StudentID</td>  
        <td>@student.StudentName</td>  
        ...
    </tr>
    }
</table>

Hope This Helps.

Feel free to ask any question regarding the same.

Have a great day. :)

Leave a Reply