Link to home
Start Free TrialLog in
Avatar of Tapan Pattanaik
Tapan PattanaikFlag for India

asked on

Error in web.config file and dataaccess

hi Experts,

                I am using asp.net, C# , SQL SERVER. In my web.config file I am using ProviderName= "System.Data.SqlClient", but in my data access i am using dbProvider, when i execute i got the error. I have attached screen shot for your review. I guess the error is in between web.config and my dataAccess. Please let me know.

============////////////////////===============////////////////////
<connectionStrings>

    <add name="BalloonShopConnection" connectionString="server=TAPAN-PC;Database=BallonShop;User=ballonshop;Password=ecommerce" ProviderName="System.Data.SqlClient"/>

</connectionStrings>

========================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;

/// <summary>
/// Class contains generic data access functionality to be accessed from the businesstier
/// </summary>
public static class GenericDataAccess
{
    //static constructor
    static GenericDataAccess()
      {        
      }

    //Execute a command and returns the results as a dataTable object
    public static DataTable ExecuteSelectCommand(DbCommand command )
    {
        //The DataTable to be returned

        DataTable table;

        // Execute the command,making sure the connection gets closed in the end
        try
        {
            //Open the data connection
            command.Connection.Open();
            //Execute the command and save the results in a DataTable
            DbDataReader reader = command.ExecuteReader();

            table = new DataTable();
            table.Load(reader);

            //Close the connection
            reader.Close();
        }
        catch (Exception ex)
        {
            Utilities.LogError(ex);
            throw;
        }
        finally
        {
            //close the connection
            command.Connection.Close();
        }
        return table;
    }

    //Create and prepares a new Dbcommand object on a new connection

    public static DbCommand CreateCommand()
    {
        //obtain the database provider name;

        string dataProviderName = BalloonShopConfiguration.DbProviderName;

        //Obtain the database connection string
        string connectionString = BalloonShopConfiguration.DbConnectionString;
         
        //Create a new data Provider Factroy
        DbProviderFactory factory= DbProviderFactories.GetFactory(dataProviderName);

        // Obtain a database-specific connection object
        DbConnection conn = factory.CreateConnection();

        //set the connection string
        conn.ConnectionString = connectionString;

        //create a database specific command object
        DbCommand comm = conn.CreateCommand();

        // Set the command type to stored procecures
        comm.CommandType = CommandType.StoredProcedure;

        //Return the initialized command object
        return comm;

    }
}

GenericDataAccess1.JPG
ASKER CERTIFIED SOLUTION
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tapan Pattanaik

ASKER

When i build the solution (F6). it tells  build succeed, but when i press F5 ,it redirects to the  "App_Code\GenericDataAccess.cs" file. Screen shot of  which i was attached previously and i was also posted full code of that page  in my question.

In the web browser i have seen this error. which i have attached screen shot below for your review.

<if you want to check my ConnectionString , you can check in my question which i have posted first.>

Thanks,
tapan Pattanaik.
Error-in-webpage.JPG
Please check the exact error message.


ErrrorMesage.JPG
From StackTrace i found  the error was in connection string. I was writing wrong " ProviderName" instead of "providerName".