Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want To Display Data from Multiple Relational DB Tables But it Doesn't Work Correctly... Please Help Me to Find This...

Thanks In Advance...

Here Is My Repository Class Method

C#
// Method For Display All Records
    public List<advertisement> getAll()
    {
        List<advertisement> List = new List<advertisement>();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_advertisement_display_all", connection);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();

            connection.Open();
            da.Fill(dt);
            connection.Close();

            List = (from DataRow dr in dt.Rows
                    select new advertisement
                    {
                        adsId = Convert.ToInt32(dr["adsId"]),
                        adsImgPath = Convert.ToString(dr["adsImgPath"]),
                        adsTypeId = Convert.ToInt32(dr["adsTypeId"]),
                        adsStartDate = Convert.ToDateTime(dr["adsStartDate"]),
                        adsExpiryDate = Convert.ToDateTime(dr["adsExpiryDate"]),
                        adsPostBy = Convert.ToInt32(dr["adsPostBy"]),
                        adsDeactivateBy = Convert.ToInt32(dr["adsDeactivateBy"]),
                        clientId = Convert.ToInt32(dr["clientId"]),

                        client = new client 
                        {
                            clientId = Convert.ToInt32(dr["clientId"]),
                            clientEmail = Convert.ToString(dr["clientEmail"]),
                        },

                        masterUser = new masterUser   // AdsPostBy & adsDeactivateBy
                        {
                            mUserId = Convert.ToInt32(dr["mUserId"]),
                            mUserEmail = Convert.ToString(dr["mUserEmail"]),
                        },

                        adsType = new adsType 
                        {
                            adsTypeId = Convert.ToInt32(dr["adsTypeId"]),
                            adsTypeName = Convert.ToString(dr["adsTypeName"])
                        }

                    }).ToList();
            return List;
        }
        catch
        {
            connection.Close();
            return List;
        }
        finally
        {
            connection.Close();
        }
    }


Here Is My Controller Method

C#
//Display All Records
    [Authorize(Roles = "Master, Admin")]
    public ActionResult Display()
    {
        if (Session["UserId"] != null)
        {
            if (Session["UserRole"].ToString() == "Master" || Session["UserRole"].ToString() == "Admin" || Session["UserRole"].ToString() == "Agent" || Session["UserRole"].ToString() == "Accountant" || Session["UserRole"].ToString() == "Developer")
            {
                return View(repObj.getAll());
            }
            else
            {
                TempData["Access"] = "Not Allow";
                return RedirectToAction("Display");
            }
        }
        else
        {
            TempData["Session"] = "Not Login";
            return RedirectToAction("Login", "Login", "MasterUserLogin");
        }
    }


Here Is StoredProcedure Query

SQL
SELECT advertisement.*, adsType.*, client.*, masterUser.* FROM advertisement  
INNER JOIN adsType ON advertisement.adsTypeId = adsType.adsTypeId 
INNER JOIN client ON advertisement.clientId = client.clientId 
INNER JOIN masterUser ON advertisement.adsPostBy = masterUser.mUserId 
OR advertisement.adsDeactivateBy = masterUser.mUserId;



Query Returns all data Fine..... It Does not gives me any error but no data returns.....
code did not go into WHILE Loop....

What I have tried:

How to Implement Method for Display Multiple Tables Data using ADO.NET in MVC5
Posted
Comments
F-ES Sitecore 26-Jul-16 4:18am    
The code looks so the problem is elsewhere. Remember we can't run your code for you, examine your variables, access your database etc so you're going to have to learn to debug these issues. Maybe the connection string isn't pointing to the right place? Maybe different SQL is running that what you think. Maybe you just don't have the data to satisfy the query? Use SQL Profiler to see exactly what is being executed against your database and use SQL Management Studio to execute the same SQL and see if anything comes to light.

One thing I'll say though, if you select * from all your tables and your code asks for "clientid", then how is it to know which clientid field you are referring to? Re-write your SP to select only the fields you are interested in.

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