0

I am trying to use Ninject 3 in my asp.net webforms application. It works fine except some pages that contains ObjectDataSource, the Select method of the ObjectDataSource throws a NullReferenceException. My code is as follows :

Web.Admin.Grades.aspx:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
                    EnablePaging="true" TypeName="Web.Admin.Grades"
                    SelectMethod="GetData" SelectCountMethod="GetDataCount"
                    StartRowIndexParameterName="StartRowIndex" MaximumRowsParameterName="MaximumRows">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="SearchTxtBox" Type="String" Name="SearchKeyWord" PropertyName="Text" />
                    </SelectParameters>
                </asp:ObjectDataSource>

Web.Admin.Grades.cs:

[Inject]
public IGradesRepository _Grades { get; set; }

public IList GetData(string SearchKeyWord, int StartRowIndex, int MaximumRows)
{
    return _Grades.GetGrades(SearchKeyWord, StartRowIndex, MaximumRows);
}

public int GetDataCount(string SearchKeyWord)
{
    return _Grades.GetGradesCount(SearchKeyWord);
}
6
  • 1
    Stacktrace and some more info as to how its all wired up and what you've tried would help (not a lot of people use ODS and if you can meet people half way they can help) Commented Mar 26, 2013 at 1:43
  • I start debugging the page and the injection happens but GetData method invoked _Grades is null, What happened? Commented Mar 26, 2013 at 8:31
  • I dunno - I'm not there :) Why do you think Ninject has been asked to inject into Web.Admin.Grades ? Ninject needs to have a reason to be be touching your instance - how are you configuring Ninject, what modules are you using. Show some of the wiring etc. Commented Mar 26, 2013 at 8:54
  • Web.Admin.Grades.cs is the codebehind file for the page Grades.aspx as in my previous comment when the page is called I start debugging and the property injection was called till now everything is ok but then the Select Method of ObjectDataSource is called which inside I use the injected property to get the data the property seems to be null, Am I supposed not to put the select method in the codebehind file or what I am doing wrong ? Commented Mar 26, 2013 at 9:02
  • Its what happens before your code that's obviously going wrong. How are you referencing Ninject. Can you link to the approach you are using to inject Ninject with ASP.NET ? What ASP.NET Version, What Ninject version. Wild guess: You have not told your page class to derive from the correct base. I'm not going to do any more guessing until you expand on how you're using Ninject Commented Mar 26, 2013 at 9:58

1 Answer 1

1

Sorry for misunderstanding I solve the problem as follows

 <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
                EnablePaging="true" TypeName="Domain.Abstract.IGradesRepository"
                SelectMethod="GetGrades" SelectCountMethod="GetGradesCount"   OnObjectCreating="ObjectDataSource1_ObjectCreating"
                StartRowIndexParameterName="StartRowIndex" MaximumRowsParameterName="MaximumRows">
                <SelectParameters>
                    <asp:ControlParameter ControlID="SearchTxtBox" Type="String" Name="SearchKeyWord"  PropertyName="Text" />
                </SelectParameters>
            </asp:ObjectDataSource>

and Web.Admin.Grades.cs:

[Inject]
    public IGradesRepository _Grades { get; set; }
protected void ObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
    {
        e.ObjectInstance = _Grades;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.