Click here to Skip to main content
15,923,910 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am doing with master detail.
i have created the page of showing details which show the both the master and detail record.but without entityfram work its giving

What I have tried:

how to loop here to fetch detail record plz help

public ActionResult Details(int id)
        {
             DataTable dtblUser = new DataTable();
          
               string sql = " SELECT dbo.DiningArea.DiningSysSeq, dbo.DiningArea.DiningCode, dbo.DiningArea.DiningName, dbo.DiningArea.Floor, dbo.DiningTables.TableCode," ;
                       sql = sql + "  dbo.DiningTables.TableName, dbo.DiningTables.NOofSeat FROM dbo.DiningArea INNER JOIN ";
                       sql = sql + "  dbo.DiningTables ON dbo.DiningArea.DiningSysSeq = dbo.DiningTables.DiningSysSeq and  DiningArea.DiningSysSeq = " + id;

            ClsDiningArea clsDining=new ClsDiningArea();
            dtblUser = obj_Fun.getAll_Records(sql);
          
            if (dtblUser.Rows.Count == 1)
            {
                clsDining.DiningSysSeq = Convert.ToInt32(dtblUser.Rows[0][0]);
                clsDining.DiningCode = dtblUser.Rows[0][1].ToString();
                clsDining.DiningName = dtblUser.Rows[0][2].ToString();
                clsDining.Floor = dtblUser.Rows[0][3].ToString();
            }
            if (clsDining.DiningSysSeq > 0)
            {
            foreach ( <clsDining.DiningTables> in  ClsDiningTable clsDetail)
            { 
            }
                      


                return View();
            }
Posted
Updated 7-Oct-19 10:40am
Comments
Samps Pro 6-Oct-19 18:04pm    
my class is
public class ClsDiningArea
{

public Int32 DiningSysSeq { get; set; }
[Required(ErrorMessage = "Please Enter Dining Area Code")]
public string DiningCode { get; set; }
public string DiningName { get; set; }
public string Floor { get; set; }
// public Byte[] DealImage { get; set; }
public virtual ICollection<clsdiningtable> DiningTables { get; set; }

public IEnumerable<clsdiningtable> ClsDiningTable { get; set; }
}

Front and foremost is that your query is Vulnerable to SQL Injection. NEVER EVER build a query concatenating strings together.
While this controller action only accepts an INTeger as the input value and you have some protection, it is a bad habit to get into. This vulnerability is over 20 years old, and apathetic code like this keeps this vulnerability within the Top 10 security issues to this day.
ORMs (e.g. EntityFramework) and LINQ became front and center to protect you from these types of errors.

The proper way to add a variable to a query is via the Command.Parameter collection.
C#
string query = "SELECT * FROM Table WHERE TableNDX = @Value";
SqlCommand cmd = new SqlCommand(query, connection);
cmd.Parameters.AddWithValue("@Value", value);

In your case, it looks like you are using some simple data-access class called obj_fun with a getAll_Records method. You may want to look into this method to see if it has an overload available to accept parameters, and if not create one and use it.

What does your query return if you run this in an Sql Client such as SSMS?
While you are in there; I would recommend trimming your query down by using aliases. It is a lot easier to read
SQL
SELECT     a.DiningSysSeq, a.DiningCode, a.DiningName, a.Floor
     ,     t.TableCode, t.TableName, t.NOofSeat 
FROM       dbo.DiningArea   as A
INNER JOIN dbo.DiningTables as T
            ON a.DiningSysSeq = t.DiningSysSeq
           AND a.DiningSysSeq = @Value
You may also want to consider creating a VIEW within your database once you get the query fine tuned to make it a cleaner call.

Finally, run this in DEBUG mode and step through this Action step by step, to see what values are where within it. This is going to be the best way to check everything out.
 
Share this answer
 
Comments
Samps Pro 7-Oct-19 11:43am    
my issue is not with query.my issue is with controller as im new in mvc.
my issue is i am not getting an idea how to sent master detail both models at once in view from controller while making the detail page to show the record of master detail table without using entity framwork
MadMyche 7-Oct-19 14:50pm    
Well in the MVC that is going to display details, you need to pass a Model to the View; this would typically be done using return View(model);.

Generally, a "Detail" view would return a Model representing a single instance of an object; and a "List" view would return a collection of a single Model type.
Samps Pro 7-Oct-19 16:41pm    
thanks for your response but i am confusing how to send a single instance of model and list view both from controlerr to model.
can you please help me in writing the actionRessult detail in controller that return both master and detail with out entityframwork.my master class is posted above and detail class is following
public class ClsDiningTable
{

    public Int32 DiningSysSeq { get; set; }
    public string  TableCode { get; set; }
    public string  TableName { get; set; }
    public double NoofSeat { get; set; }

    public virtual ClsDiningArea DiningArea { get; set; }

}


query for master is:: select * from diningarea where DiningSysSeq= id
query for detail::select * from diningtables where DiningSysSeq =id
Samps Pro 7-Oct-19 17:27pm    
i try this one

public ActionResult Details(int id)


{
List<clsdiningtable> clsTable = new List<clsdiningtable>();

DataTable dtblUser = new DataTable();

string str="Select * from DiningArea where DiningSysSeq = " + id;

ClsDiningArea clsDining=new ClsDiningArea();

dtblUser = obj_Fun.getAll_Records(str);

if (dtblUser.Rows.Count > 0)
{
clsDining.DiningSysSeq = Convert.ToInt32(dtblUser.Rows[0][0]);
clsDining.DiningCode = dtblUser.Rows[0][1].ToString();
clsDining.DiningName = dtblUser.Rows[0][2].ToString();
clsDining.Floor = dtblUser.Rows[0][3].ToString();
}

if (clsDining.DiningSysSeq > 0)
{
string sql = "Select * from DiningTables where DiningSysSeq = " + id;


SqlDataReader myreader = null;

myreader = obj_Fun.GetRecordByQuery(sql);

if (myreader.HasRows)
{
while (myreader.Read())
{
clsTable.Add(new ClsDiningTable
{
DiningSysSeq = Convert.ToInt32(myreader["DiningSysSeq"]),
TableCode = myreader["TableCode"].ToString(),
TableName = myreader["TableName"].ToString(),
NoofSeat = Convert.ToInt16(myreader["NoofSeat"])

});


}




}

}
else
{

ViewBag.Message = "No IDataRecord found";
}

return View(clsTable);
}
thanks for your response but i am confusing how to send a single instance of model and list view both from controlerr to model.
can you please help me in writing the actionRessult detail in controller that return both master and detail with out entityframwork.my master class is posted above and detail class is following

public class ClsDiningTable
   {


       public Int32 DiningSysSeq { get; set; }
       public string  TableCode { get; set; }
       public string  TableName { get; set; }
       public double NoofSeat { get; set; }

       public virtual ClsDiningArea DiningArea { get; set; }

   }


query for master is:: select * from diningarea where DiningSysSeq= id
query for detail::select * from diningtables where DiningSysSeq =id
 
Share this answer
 

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