Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
In my project i dynamically creating table by giving table name (ex. student) and adding fields to that table and then save table. Now, my table is created in SQL Server database. Now assume table has data and i want to show the data of table (ex student) on WebGrid. But, the problem is i have no model for that table (ex. student). So, How i can create model for newly created table or how i can show the the data on WebGrid?


Thank You !
Posted
Comments
johannesnestler 3-Dec-13 10:16am    
I don't understand why you need a model if your data-layout is so dynamic. Anyway if your realy go down this road you could create your model by creating appropiate sourcecode as abbaspirmoradi showed you and compile it on the fly. You can load the new created model type and use it later. I once followed this approach but in the end it was a pain - and this was desktop. So I would use hard thinking to come up with another solution (use dynamic binding, use the datarows as they are, make multi-purpose models, ...). But it's impossible to suggest what will fit your need..
Vi(ky 5-Dec-13 10:11am    
thanks for your reply. I solved this with some different approach.

1- Create a model CurrentReport.cs
C#
public class CurrentReport
    {   
        //both property contain data from a two different table    
        public IEnumerable<tablesfield> OTablesFields { get; set; } //for webgrid header
        public List<dynamic> dataField { get; set; } //for grid data
    }


2- Now, create Actionmethod in controller like

C#
public ActionResult Report(int tableId)
        {
            ReportUtility OReportUtility = new ReportUtility();   //ReportUtility class Object
            CurrentReport OCurrentReport = new CurrentReport();   //model object

            OCurrentReport.dataField = OReportUtility.getTableData(tableId);   //grid data

            OCurrentReport.OTablesFields = OReportUtility.getColumns(tableId);  //grid column names

            return PartialView(OCurrentReport);
        }


3- Then create getTableData() & getColumns() in ReportUtility Class

C#
public List<dynamic> getTableData(int tableId)
        {
            try
            {
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter();
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NewConnection"].ConnectionString);
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT * FROM " + getTableName(tableId), con);
                da.SelectCommand = cmd;
                da.Fill(dt);

                da.Dispose();
                con.Close();

                var result = new List<dynamic>();
                foreach (DataRow row in dt.Rows)
                {
                    var obj = (IDictionary<string,>)new ExpandoObject();
                    foreach (DataColumn col in dt.Columns)
                    {
                        obj.Add(col.ColumnName, row[col.ColumnName]);
                    }
                    result.Add(obj);
                }               
                return result;
            }
            catch (Exception ex)
            {
                return null;
            }            
        }


C#
public string getTableName(int tableId)
       {
           var context = new DBEntities();

           var tname = (from t in context.Table
                        where t.Id == tableId
                        select t.tableName).First();

           return tname.ToString();
       }


C#
public IEnumerable<tablesfield> getColumns(int tableId)
       {
           var context = new DBEntities();
           var columns = from tf in context.TablesField
                         where tf.tableId == tableId
                         select tf;

           return columns;
       }



4- Now on View


HTML
@model WDCS.MODELS.CurrentReport

<div id="PartialPage">

@{
    var grid = new WebGrid( Model.dataField , rowsPerPage: 5, canPage: true, canSort: true, ajaxUpdateContainerId: "Grid");

        List<webgridcolumn> cols = new List<webgridcolumn>();
   
        foreach (var clm in Model.OTablesFields)
        {
            cols.Add(grid.Column(clm.fieldName, clm.displayName));        
        }        
                                                                                              
    }


    <div id="Grid">
          @grid.GetHtml(
                        tableStyle:"webGrid",
                        headerStyle:"header",
                        alternatingRowStyle:"alt",
                        columns:cols           
                       )
       
    </div>

</div>


That's it. just provide table id and it display any table data on webgrid.
 
Share this answer
 
v2
Comments
abbaspirmoradi 5-Dec-13 10:35am    
nice +5!
Vi(ky 5-Dec-13 10:38am    
thanks :)
johannesnestler 6-Dec-13 9:27am    
I think you found a good solution - great that you could solve it.
Hi,
look at this example.You can use a string for create your model.it string can have your table fields that you create into database.I hope it can help you:

C#
     string modelDefinition = @"using System;
     using System.CodeDom.Compiler;
     using System.Collections.Generic;
     using System.ComponentModel.DataAnnotations;
     using System.Web;
     using Microsoft.CSharp;

 namespace PhoneBookApplication.Models
  {
    public class Phonebook
    {
        public Phonebook(int id, string name, string Fname, string mail, String mobile, int age,     string address)
        {
            ID = id;
            Name = name;
            Email = mail;
            Mobile = mobile;
            Age = age;
            Address = address;
        }
        public Phonebook() { }
        [Key]
        public int ID { get; set; }

       
        public string Name { get; set; }


        public string Email { get; set; }

      
        public string Mobile { get; set; }

   
        public int Age { get; set; }

       
        public String Address { get; set; }
    }
}";

  var providerOptions = new Dictionary<string,>();

            providerOptions.Add("CompilerVersion", "v4.0");

            CSharpCodeProvider codeProvider = new CSharpCodeProvider(providerOptions);

            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters(new string[] { "System.dll", "System.ComponentModel.DataAnnotations.dll" });

            parameters.GenerateInMemory = true;
            CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, modelDefinition);
            if (results.Errors.Count > 0)
            {
                string err = string.Empty;
                foreach (CompilerError CompErr in results.Errors)
                {
                    err = err +
                                "Line number " + CompErr.Line +
                                ", Error Number: " + CompErr.ErrorNumber +
                                ", '" + CompErr.ErrorText + ";" +
                                Environment.NewLine + Environment.NewLine;
                }
                throw new Exception(err);
            }
            Type t = results.CompiledAssembly.GetType("PhoneBookApplication.Models.Phonebook");

            object model = Activator.CreateInstance(t);

            System.Reflection.PropertyInfo[] ModelProperties = model.GetType().GetProperties();
 
Share this answer
 
v4
Comments
Vi(ky 3-Dec-13 6:53am    
thank for your reply but in my case number of columns and column name is not predefind
abbaspirmoradi 3-Dec-13 9:30am    
ok you can create your string also dynamically at run time.when you create your table you can create this string dynamically.if you give me more detailes I can give you how do you do that.
Vi(ky 3-Dec-13 11:26am    
i don't want to create model when creating the table, it could be happened when showing the data of table.
abbaspirmoradi 3-Dec-13 12:04pm    
ok.Consider your table and create relative string for model that you want

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