How to Display data in Asp.net/MVC Grid
How to display any file in asp.net/mvc?
How to get data from database into razor view (Asp.net/MVC)?
Step 1: In our preview lecture we insert a data into category table with he following parameter (Name, Description, Thumbnail) Now here in this lecture we have to display same table data into grid. Used the same previous model for display,
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web;
namespace Pick_n_Buy.Models
{
public class MdlCategory
{
[Key]
public int ID { get; set; }
[Required]
[Display(Name = “Name”)]
public string Name { get; set; }
[Required]
public string Description { get; set; }
public string Thumbnail { get; set; }
}
}
Step 2: Defined controller method with the following code based on this method we have to generate a view page by selecting the following option
public ActionResult Category()
{
try
{
BLL_Category obj = new BLL_Category();
List<MdlCategory> CategoriesList = obj.BLL_ReadCategory();
return View(CategoriesList);
}
catch (Exception)
{
throw;
}
}
Step 3: Defined Bll Class method with the following code
public List<MdlCategory> BLL_ReadCategory()
{
DAL_Category obj = new DAL_Category();
return obj.DAL_ReadCategory();
}
Step 4: Defined the DAL method with the following code
public List<MdlCategory> DAL_ReadCategory()
{
try
{
DataTable oTable = SqlHelper.ExecuteTable(this._ConnString, CommandType.StoredProcedure, “SPW_Read_All_Category”, null);
List<MdlCategory> CategoriesList = new List<MdlCategory>();
foreach (DataRow oRow in oTable.Rows)
{
MdlCategory ctg = new MdlCategory();
ctg.ID = Model.Common.CheckIntegerNull(oRow[“ID”]);
ctg.Description = Model.Common.CheckStringNull(oRow[“Description”]);
ctg.Name = Model.Common.CheckStringNull(oRow[“Name”]);
ctg.Thumbnail = Model.Common.CheckStringNull(oRow[“Thumbnail”]);
CategoriesList.Add(ctg);
}
return CategoriesList;
}
catch (Exception)
{
throw;
}
}
Step 5: defined following methods in sqlhelper class
public static DataTable ExecuteTable(string connString, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
using (SqlConnection conn = new SqlConnection(connString))
{
try
{
SqlCommand cmd = new SqlCommand();
//SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter oDataAdapter = new SqlDataAdapter();
DataTable oDataTable = new DataTable();
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
oDataAdapter.SelectCommand = cmd;
oDataAdapter.Fill(oDataTable);
cmd.Parameters.Clear();
return oDataTable;
}
catch (SqlException ex)
{
throw ex;
}
catch (Exception exx)
{
throw exx;
}
finally
{
conn.Close();
conn.Dispose();
}
}
}
Defined the following method in sqlhelper also
private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters)
{
//if the provided connection is not open, we will open it
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
//associate the connection with the command
command.Connection = connection;
//set the command text (stored procedure name or SQL statement)
command.CommandText = commandText;
//if we were provided a transaction, assign it.
if (transaction != null)
{
command.Transaction = transaction;
}
//set the command type
command.CommandType = commandType;
//attach the command parameters if they are provided
if (commandParameters != null)
{
AttachParameters(command, commandParameters);
}
return;
}
Step 6: Defined the following Stored procedure for this call
Create procedure [dbo].[SPW_Read_All_Category]
as
begin
select * from Tbl_Category
end
1 thought on “How to Display data in Asp.net/MVC Grid”
Leave a Reply
You must be logged in to post a comment.
How do we Show the image in layout page that we stored in DB ny using the httpwebbasedlink.?