ASP.NET MVC Dropdownlistfor NullException RRS feed

  • Question

  • User-491950272 posted

    I'm confronting a null re exception while submitting the form using MVC Dropdownlistfor which is

    An exception of type 'System.NullReferenceException' occurred in App_Web_mw1eavab.dll but was not handled in user code

    Additional information: Object reference not set to an instance of an object.

    My Index Post action is empty.

    My Index Get Action is

    using (SqlConnection con = new SqlConnection(cs))
                    SqlCommand cmd = new SqlCommand("[dbo].[EGB_GETACTIVEJOBPOSTINGS]", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@jobid", -1);
                    DataTable newdt = new DataTable();
                    SqlDataAdapter dataadap = new SqlDataAdapter();
                    dataadap.SelectCommand = cmd;
                    foreach (DataRow item in newdt.Rows)
                        CRVM.Position.Add(new SelectListItem() { Text = item["JOBPOST"].ToString(), Value = item["JOBID"].ToString() });
                        CRVM.JobDiscription.Add((long)item["JOBID"], (string)item["DESCRIPTION"]);
                    if (newdt.Rows.Count > 0)
                        //var desc = newdt.Rows[0]["DESCRIPTION"];
                        //jobInfo.JobDiscription = (string)desc;
                        CRVM.JobDiscription = CRVM.JobDiscription;
                        CRVM.Position = CRVM.Position;
                        return View(CRVM);

    and Markup is

    @using (Html.BeginForm("Index", "Account", FormMethod.Post, new { @class = "form-horizontal" }))
            <div id="tabs-1">
                <br />
                <div class="col-md-8">
                    <div class="box box-info">
                        <div class="box-header with-border">
                            <h3 class="box-title">Job Information</h3>
                        <div class="box-body">
                            <div class="form-group">
                                <label class="col-sm-3 control-label">Please Select Position</label>
                                <div class="col-sm-6">
                                    @Html.DropDownListFor(x => x.Position, Model.Position, new { @class = "form-control", @id = "select1" })

    //Exception comes at Model.Position (becomes null) after post back in the dropdownlistfor
    </div> </div>

    I've mentioned the problem in the C# like comments in Razor. I'm trying to first get the required data using ADO.NET through a Stored Procedure, upon getting the data successfully and after putting into the Dropdownlistfor, I'm confronting this exception. Please provide me some clue to resolve this issue. Thanks

    Friday, September 11, 2015 9:07 AM


All replies

  • User1633621018 posted

    Hi Janshair,

    You are trying to use something that is null. This means you either set it to null , or you never set it to anything at all. Just put debugger and check if you Model.Position has some value and I am pretty sure your newdt.Rows might not have any rows in it. If you still have problems or concerns post back again.

    Friday, September 11, 2015 9:21 AM
  • User603616845 posted


    I think you are passing Position value as null. I didn't get one point why are you doing this..

    CRVM.Position = CRVM.Position;

    I think you need to pass some value from outside in this..

    CRVM.Position = "Some value";

    I hope CRVM.Position is coming null.

    Please check it.

    Hope this will help you.


    Friday, September 11, 2015 9:42 AM
  • User-491950272 posted

    Thanks for replying

    Actually I've putted a break point right their, Mode.Position is returning values from newdt.Rows as

    JobDiscription: Count = 8

    Position = 8

    That means Model.Position is not returning null. As I've tested using a break point and hovering cursor.

    The main problem is on post back. and shows the same exception as the above. 

    Monday, September 14, 2015 1:07 AM
  • User-271186128 posted

    Hi Janshair Khan,

    As for this issue, I suggest you could refer to the following code to bind DropDownList.

    Code in model:

        public class SubjectModel
            public SubjectModel()
                SubjectList = new List<SelectListItem>();
            public List<SelectListItem> SubjectList

    Code in Control:

            public ActionResult Index()
                SubjectModel model = new SubjectModel(); 
                model.SubjectList.Add(new SelectListItem { Text = "Physics", Value = "1" });
                model.SubjectList.Add(new SelectListItem { Text = "Chemistry", Value = "2" });
                model.SubjectList.Add(new SelectListItem { Text = "Mathematics", Value = "3" });
                return View(model);

    Code in View:

    @model MVCTestSample.Models.SubjectModel

    For more details, please refer to the following links:





    Best regards,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 14, 2015 3:44 AM
  • User317958953 posted

    I am not sure, may it will work for you.

    You have mentioned you post actions is empty, you have written only Index Get Action code. Bind your drop down in post action too or pass the value in model in post action. If you will return same view in post without having value that bind the drop down it will throw error.

    Monday, September 14, 2015 4:26 AM