0

I'm trying to run the following code, basically using an SQL query that's on my access database, adding parameters to it and getting a datatable from it:

    string spSQL = "spFindFullScheduleEvents";
    ArrayList prmList;
    OleDbParameter prm;
    int userID = (int)Session["UserID"];
    if (userID < 0 || Session["UserID"] == null)
        userID = 0;
    int i = 1;
    prmList = new ArrayList();

    prm = new OleDbParameter("IDUser", OleDbType.Integer);
    prm.Value = userID;
    prmList.Add(prm);

    DataTable dt = DoQueries.ExecuteSPDataTable(spSQL, prmList);

DoQueries Function:

 public static DataTable ExecuteSPDataTable(string strSQL, ArrayList prmList)
{
    DataTable dt = new DataTable();
    OleDbDataAdapter adp = new OleDbDataAdapter(strSQL, strConnection);
    foreach (OleDbParameter prm in prmList)
    {
        adp.InsertCommand.Parameters.Add(prm);
    }
    adp.Fill(dt);
    return dt;
}

but, for whatever reason, I get an "Object reference not set to an instance of an object", when trying to add the parameters, thought it was Session["UserID"] which was causing the problem, but even when I tried putting a regular int value (30), the problem still occures on this line:

  adp.InsertCommand.Parameters.Add(prm);

The SQL command goes as follows:

   SELECT *
   FROM tblFullSchedule
   WHERE IDUser = [@IDUser];

I should also mention that I'm using this for a school project and stuff here are pretty outdated unfortunately, the DoQueries class was made by my teacher, which is why I don't know if it's an issue with her code or I'm doing something wrong. Previously I was executing a similar function called ExecuteDataTable because I didn't need any parameters, which worked perfectly fine.

After finishing writing this down I have a suspicion that ExecuteSPDataTable accepts an SQL command and not a stored procedure, which is why the error occurs. if my suspicion is correct, then I would love help writing a DoQueries function that does accept a store procedure, because I'm an SQL/ OleDB beginner, and I have no idea how to do it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Taabkl
  • 103
  • 1
  • 9
  • 2
    As written, `ExecuteSPDataTable` can never work because it is not setting the `InsertCommand` on the `OleDbDataAdapter`, hence the NRE. I would suggest you throw your teacher's class in the bin and write something of your own that not only works but uses C# features newer than .NET 1.1 (`ArrayList` in this day and age, seriously?). – Ian Kemp Feb 04 '19 at 07:13
  • 1
    Microsoft Access does not have stored procedures. As already commented, there isn't much that is salvageable here. If you want to do any troubleshooting the best thing is to step through the code and identify which statement does not return an object when it should. – Nick.Mc Feb 04 '19 at 07:36
  • @IanKemp It's pretty horrendous and you're completely right. It's been really discouraging taking my project seriously with my teacher not bothering to update anything from the day she made it. I don't want to exaggerate but this code is probably 15 years old... I've really hesitated to change anything about it, was always in the mindset of "It works so what the hell, I wouldn't wanna break it any further", my point being, that I have no idea how I should update / rewrite it properly, do you have somewhere I could start learning? Maybe an example? – Taabkl Feb 04 '19 at 07:38
  • @Nick.McDermaid Intent is queries saved on my database, might have used the wrong term – Taabkl Feb 04 '19 at 07:40
  • 1
    I can't believe this code has ever worked. Giving non functioning, confusing code to students to learn from is basically just lazy. I've never seen a student question on here that gave me any confidence that any lecturer has the slightest clue – Nick.Mc Feb 04 '19 at 07:40
  • In that case.... queries do not have or take parameters. – Nick.Mc Feb 04 '19 at 07:41
  • Sorry... MS Access queries do not have parameters. The SQL statement you posted does have a parameter, but you probably need to remove the `@`. Sorry it's a while since I've done MS Access programming - are you absolutely sure this is MS Access? Can you post your connection string – Nick.Mc Feb 04 '19 at 07:43
  • This SO question shows no less than three different ways of using parameters in C# / OLEDB / MS Access. Sorry I don't know which one actually works - please try them all! https://stackoverflow.com/questions/5893837/using-parameters-inserting-data-into-access-database – Nick.Mc Feb 04 '19 at 07:47
  • @Nick.McDermaid haha it's fine, connection string is so: (sorry for the formatting) private static string strConnection = "Provider =Microsoft.ACE.OLEDB.12.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(@"../App_Data/ProjectDB.accdb"); – Taabkl Feb 04 '19 at 07:47
  • @Nick.McDermaid thank you, I'll give it a look – Taabkl Feb 04 '19 at 07:50
  • The accepted answer in that link says that you should remove the square brackets from your parameters - i.e. use this in your SQL query : `WHERE IDUser = @IDUser;` – Nick.Mc Feb 04 '19 at 07:59
  • So just to clarify some terminology: stored procedures are stored database code that may take parameters and return one or more datasets or scalars.... MS Access doesn't have these. Stored queries are called "queries" in MS Access but these do not take parameters, although you _can_ apply a where clause. The select statement you are using is referring to `tblFullSchedule` so I guess it is actually referring to a table, not a query, so in fact there is no "stored query" here at all. I hope this clarifies some things - I don't mean to be condescending at all. – Nick.Mc Feb 04 '19 at 08:08
  • @Nick.McDermaid Access does have SPs since version 2010. – Ian Kemp Feb 04 '19 at 08:19
  • Sorry my mistake. And doing some googling it appears that you can create queries in MS Access that take parameters. I really stuffed this one up. – Nick.Mc Feb 04 '19 at 08:22

1 Answers1

1

So, I found a way to fix it - with this outdated code, if someone finds themselves in the same situation.
I can't believe I haven't figured this out - but the solution was to change InsertCommand to SelectCommand..

public static DataTable ExecuteSPDataTable(string strSQL, ArrayList prmList)
{
    DataTable dt = new DataTable();
    OleDbDataAdapter adp = new OleDbDataAdapter(strSQL, strConnection);

    foreach (OleDbParameter prm in prmList)
    {
        adp.SelectCommand.Parameters.Add(prm);
    }
    adp.Fill(dt);
    return dt;
}

and change the SQL string to:

 string strSQL = "SELECT * FROM tblFullSchedule WHERE IDUser = @IDUser";

Instead of the query name.... Now, I seriously gotta update this code..

Taabkl
  • 103
  • 1
  • 9
  • Good on you for working it out and posting it. You should be able to accept your answer – Nick.Mc Feb 04 '19 at 09:33
  • @Nick.McDermaid Thank you ! I wanted to accept but it says I can accept my own answer in 2 days, so I'll come back in 2 days and accept it :) – Taabkl Feb 04 '19 at 09:50