Click here to Skip to main content
15,885,947 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
HTML
public ActionResult list(MvcApplication1.Models.LoginDetails model)
        {


             using (var con = new SqlConnection
           (ConfigurationManager.ConnectionStrings["con"].ConnectionString))

           {
            var cmd = new SqlCommand("sp_select_data", con); // here use Select Procedure
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar)).Value = model.UserName.Trim();//Pass the parameter
            cmd.Parameters.Add(new SqlParameter("@LoginId", SqlDbType.VarChar)).Value = model.LoginID.Trim();//Pass the parameter
            cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar)).Value = model.Password; //Pass the parameter
            con.Open();
            DataSet logininfo = new DataSet();
            SqlDataAdapter dataAdptr = new SqlDataAdapter(cmd);
            dataAdptr.Fill(logininfo);


            dataAdptr.Dispose();
            DataTable Loginfinfotable = new DataTable();
            Loginfinfotable = logininfo.Tables[0];
            List<DataRow> obj = new List<DataRow>();
            //List<DataRow> obj = Loginfinfotable.AsEnumerable().ToList();


            foreach (DataRow dr in Loginfinfotable.Rows)
            {
                obj.Add(dr);
                   

            }






            return View("LoginDetails", obj); 

            //return View(Loginfinfotable);

          

             }


            //return View();
        }



The following error generated

XML
Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 90:
Line 91:         <table>
Line 92:        @foreach (var item in Model) {
Line 93:     <tr>
Line 94:         <td>
Posted

1 solution

From your error message it seems that the Model object sent to the current View is null, so you have to activate the view from your controller with an Model object created before.

So you have to put a breakpoint in your controller action "list" before to return the view to make shore that your action was invoked and that your Model was created and populated with data!
 
Share this answer
 
Comments
Dhruvin Sukhadiya 10-Jan-14 5:33am    
how to activate view from controller?
Rahul VB 11-Jan-14 11:19am    
Absolutely correct, if the object is null it wont come to life. So as Ron said you need to add a breakpoint at that particular line where the exception was thrown. Now at that line of code if you check then that object will be obviously null, because we broke at that line and it dint execute. So now hover your mouse to the recent places where you have declared that object, you will definitely find it null somewhere. Then you will be exactly be able to trace your code especially the place where the object wasn't initialized.

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