Filling in DataTable using OleDb

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maylortaylor
    New Member
    • Nov 2012
    • 72

    Filling in DataTable using OleDb

    For some reason I am having problems doing this simple task. I just want to run a query in Access to select a
    a field from the table that is "like" the string being inputted.

    However, the dataset comes back empty...well not empty, I get a column name but no data. So when I type in
    Code:
    ds.Tables[0].Columns[0].ColumnName
    , I get the column name coming from the database ("ParcelNumber" ).

    When I run the query inside Access I receive the desired information. But when I try to check for the info in code
    Code:
    ds.Rows[0]
    , I get "There is no row at position 0".



    Code:
    public DataTable FindFullPNusingPartialPN(string _parcelNumber)
                {
                     OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ConfigurationManager.AppSettings["TaxCert_Database"]);                DataTable dt = new DataTable();
                    try
                    {
                    string query = "SELECT ParcelNumber FROM Orders WHERE ParcelNumber LIKE '*" + _parcelNumber + "*';";
                   
                    OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);             
                                     
                    DataSet ds = new DataSet();
                    adapter.Fill(ds);
                    
    
                    if (ds.Tables[0].Rows.Count >= 0)
                    {
                        dt = ds.Tables[0];
                    }
                        
                    }
    
                    catch (Exception ex)
                    {
                      throw;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open)
                        {
                            conn.Close();
                        }
                    }
                    return dt;
                }
  • maylortaylor
    New Member
    • Nov 2012
    • 72

    #2
    Fixed it. Went with .Parameters and changed my "*" to "%".
    Code:
     public DataTable FindFullPNusingPartialPN(string _parcelNumber)
                {
    
                    string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ConfigurationManager.AppSettings["TaxCert_Database"];
                    DataTable dt = new DataTable();
                    
                    try
                    {
                        OleDbConnection conn = new OleDbConnection(connString);
                        string query = "SELECT ParcelNumber FROM Orders WHERE ParcelNumber LIKE @_parcelNumber;";
    
                        OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
                        adapter.SelectCommand.Parameters.Add("@_parcelNumber", OleDbType.VarChar).Value = "%" + _parcelNumber + "%";
    
                        DataSet ds = new DataSet();
                        adapter.Fill(ds);
    
    
                        if (ds.Tables[0].Rows.Count >= 1)
                        {
                            dt = ds.Tables[0];
                        }
    
                    } 
    
    
                    catch (Exception ex)
                    {
                        //myLogger.Log(ex.Message, ex.TargetSite.ToString(), ex.StackTrace);
                        throw;
                    }
                    finally
                    {
                        //if (conn.State == ConnectionState.Open)
                        //{
                        //    conn.Close();
                        //}
                    }
                    return dt;
                }

    Comment

    Working...