简体   繁体   中英

C# asp.NET displaying output of a query to screen. Error: 'System.ArgumentException' in Oracle.DataAccess.dll

First post here. I'm trying to create a website that fetches data from an Oracle database and returns some tables. I was able to connect my database fine and made a DataConnector that returns a list of CodeDesc objects. My main problem right now is simply displaying that data to the screen, preferably in the form of a drop down list but I'm using a GridView for now.

Here's my front end:

protected void Button1_Click(object sender, EventArgs e)
{
    DataConnector dc = new DataConnector();
    GridView2.DataSource = dc.getCodeTypes();
    GridView2.DataBind();
}

When I click the button, nothing is generated and the debugger only says "Exception thrown: 'System.ArgumentException' in Oracle.DataAccess.dll" Any help would be appreciated. This is my first time doing web development and it's been a struggle to get even this far. I'm using Visual Studio 2015

Back End:

//Create datatable to get info from db & return results
public List<CodeDesc> getCodeTypes()
{
    try
    {
        OracleConnection con = new OracleConnection(connString);
        con.Open();

       string query = "select id, descr from code_desc where code_type_id = 0";

        // Create the OracleCommand
        OracleCommand cmd = new OracleCommand();

        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;

        // Execute command, create OracleDataReader object
        OracleDataReader reader = cmd.ExecuteReader();

        List<CodeDesc> L = new List<CodeDesc>(); 

        while (reader.Read())
        {
            CodeDesc c = new CodeDesc();
            c.id = reader.GetInt32(0);
            c.description = reader.GetString(1);
            L.Add(c);
        }
        // Clean up
        reader.Dispose();
        cmd.Dispose();
        con.Dispose();

        System.Diagnostics.Debug.WriteLine(L);
        return L;
    }
    catch (Exception ex)
    {
      // catch clause here...
    }
}

CodeDesc:

public class CodeDesc
{
    public int id { get; set; }
    public string description { get; set; }
}

Any help would be great.

You never set the query string to the CommandText property of the OracleCommand. Of course this can only result in an exception when you try to execute your command.

Said that, remember that every disposable object should be enclosed in a using statement. This is very important in case of exceptions because the correct closing and disposing is executed automatically exiting from the using block

public List<CodeDesc> getCodeTypes()
{
    try
    {
        List<CodeDesc> L = new List<CodeDesc>(); 
        string query = "select id, descr from code_desc where code_type_id = 0";
        using(OracleConnection con = new OracleConnection(connString))
        using(OracleCommand cmd = new OracleCommand(query, con))
        {
            con.Open();
            // Execute command, create OracleDataReader object
            using(OracleDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                   CodeDesc c = new CodeDesc();
                   c.id = reader.GetInt32(0);
                   c.description = reader.GetString(1);
                   L.Add(c);
                }
            }
       }
       System.Diagnostics.Debug.WriteLine(L);
       return L;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM