-1

I have this error: "Object reference not defined for an instance of an object."

What I've tried was to create an instance of the class and, through it, try to access the gridview passing a DataSet to make the Bind.

The class is consumed via AJAX (AJAX.PRO)

Example of what I did:

public partial class Exemplo : InternalBasePage
{

[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.Read)]
public static string SalvarRegra(JavaScriptArray DataRule, string NameRule)
{
    Exemplo exemplo = new Exemplo();

    DataSet ds = new DataSet();

    exemplo.GRID.DataSource = ds;
}
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Joshua
  • 5
  • 4
  • Consult [What is a NullReferenceException](https://stackoverflow.com/questions/4660142/), but I don't understand what you think that code is supposed to do since you discard both `exemplo` and `ds` without doing anything with them. – Dour High Arch Sep 10 '22 at 17:39
  • What happens is that I'm trying to assign a dataset to a gridview through a static method. I put the EXAMPLE and the DS just so you can understand what I'm trying to do, to avoid putting the complete code. – Joshua Sep 10 '22 at 17:51

1 Answers1

0

You so called static class is NOT a web page, nor a web method.

If you want to add a web method to a existing web page, then do so, and that web method is then free to use that static class.

But, a general class, or in fact any class you build is just that - a class and hunk of code. But, as such it has really ZERO to do with a web page, or even a web method.

And it not clear why you would pass a gridView to such a static method, but you can.

I mean, in code it would be better to have say all your helper routines in a static class, and thus in code you could and would do this:

Say, on page load, fill out the grid view using that static class, like this:

Say my gv is this:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID"  cssclass="table" >
        <Columns>
            <asp:BoundField DataField="Fighter" HeaderText="Fighter"  />
            <asp:BoundField DataField="Engine" HeaderText="Engine"  />
            <asp:BoundField DataField="Thrust" HeaderText="Thrust"  />
            <asp:BoundField DataField="Description" HeaderText="Description" />
            <asp:ImageField DataImageUrlField="ImagePath" HeaderText="Profile Pic"
                ControlStyle-Width="150px" />
        </Columns>
    </asp:GridView>

So, say on page load I do this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = General.MyRst("SELECT * FROM Fighters");
            GridView1.DataBind();
        }
    }

So, we get this:

enter image description here

So, in above, I used the static class General.MyRst (and that static class is bunch of general code routines that are rather handy).

Now, I suppose I could setup that static class to take a GridView as a parameter, say like this:

General.MyRst(GridView1, "SELECT * FROM Fighters";

Gee, saved one line of code. So the above static class is now loading up (manuplaiting the GV).

so it is VERY easy to have a static method to load or do whatever to a GV.

However, you are confusing that of say the above simple static method that loads up and manipulating the GV as opposed to calling a web method to do the same thing. That is a VAST VAST different issue and VAST VAST different goal. We don't want to confuse the above simple and easy demonstration of a static method manipulating a GV with that of calling a web method - they are VAST VAST different issues and goals here.

But then again, I just built a method that is now tied to the GridView type and class. With above, MyRst returns a data table - I can use it for quite much any combo box, listview, gridView - it don't matter.

But, if you going to make a web method call, that occurs from the client side page, and how do you propose to pass the GridView object? You don't have use of the GV object client side, and you don't have a post-back, so the class instance of GV does not exist at this point in time.

And if you going to do this from the web page class, such as the above one line static class that "manipulates" the GV?

You have to pass that GridView from the web page class - code behind. And any ajax call to the current page does not have any of the controls, or even use of the current web page class (to get at and use controls).

So, any static class that uses say the GV? It will have to be called from that web page - which is not a static class. And while you CAN add static web methods to the current web page?

Keep in mind this: Any web method, INCLUDING ones on the current web page will also be static, and do NOT have use of controls on the current page.

I suppose you could have the web method call return the rendered gridview, and inject that into the page, but I fail to see the advantage here.

So, the problem here is that both web methods to you say add to the current page - they are also static, and don't have use of any control on the current page. And even a static class - it also does not have use of the web page. and you not done a post back, so you still in hot water.

It not clear what you final goal here is, but I fail to see how attempting to use a static web method going to help you fill a gridview when that gridview is a server side object, and one you do NOT have use of until such time you do a post-back.

if the goal is to avoid a post-back? Then I would return json data, and use a client side grid. On the other hand, it is VERY easy to simple drop in a update-panel, and load up the grid that way, and you not suffer a full page post-back. And in most cases, you can (and should) say turn off view state for that GV to reduce the size of the grid re-load.

You can try a working grid example - I put the grid inside of a update panel, and thus when you edit values, the response time is near instant. But MORE important I fired up the browser tools, and to pop the editor in that GV - the size is a VERY tiny 7k in size.

As a result?

I used plain jane code behind, but get no browser re-plots, and response time is absolute instant. Absolute instant response time, and no messy browser re-loads should be your goal here. And that goal in most cases can be achieved without having to resort to a ajax call to load up that grid.

Try this working grid edit example here - try clicking on edit - use save button. I did not have to wire up a bunch of ajax calls, and the only js code I have was using a jquery.UI dialog to pop the div for editing - but the div is filled with code behind.

http://www.kallal.ca/WebSite11/WebForm2

Given the absolute instant response time of above? Then you can avoid a boatload of work, avoid world poverty, and not have to resort to ajax calls when working with a gridview.

All you should care about as a developer is that you not doing huge post-backs, and your not having to write a lot of code for this goal. So, you want fast, you want instant response, and you don't want or need much of any client side code to achieve this goal.

so, "WITH" a static method, you can manipulate a GV. But you can't FROM a static method. Well, I suppose you could if you put the GV into session, since a static method can use session.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51