5

My code is view all the data in the gridview

Web.config code is

<configuration>
  <connectionStrings>
    <add name="ConStr" connectionString="DataSource=.;Integrated Security=SSPI;Initial catalog=sshopping"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
</configuration>

It is coded in external class

namespace DBAction
{
    public class ViewAction
    {
        public DataSet GetAllData()
        {
                SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
                cmd.CommandText = "Select UserName,Password,RoleName,EmailID,SecurityQuestion,SecurityAnswer,LastLogin from LoginInfo";
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();  
                da.Fill(ds);
                cmd.Dispose();
                DataConnection.CloseConnection();
                return ds;
        }
    }
}

it is giving error in line da.Fill(ds) The code to bind data source with gridview is coded on page load like this.

 DataSet ds = new ViewAction().GetAllData();
        gvLoginInfo.DataSource = ds;
        gvLoginInfo.DataBind();

And conectionstring code in data connection class is

 public static SqlConnection GetConnection()
        {

            if (con == null)
            {
                con = new SqlConnection();
                con.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
                con.Open();
            }

             return con;
        }

And one one error is

Exception Details: System.ArgumentException: Keyword not supported: 'datasource'.

Source Error:


Line 19:             {
Line 20:                 con = new SqlConnection();
Line 21:                 con.ConnectionString =ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
Line 22:                 con.Open();
Line 23:             }
4

3 回答 3

4

错误仅在 Web.Config 中。请在connectionString中的DataSource之间放一个空格为:Data Source。因此,您的连接字符串将变为:

 "Data Source=.;Integrated Security=SSPI;Initial catalog=sshopping".
于 2013-07-07T05:44:06.177 回答
4

从我在网上看到的示例中,在您的连接字符串中将“DataSource”替换为“Data Source”(两个单词之间有一个空格)。 http://msdn.microsoft.com/en-us/library/ms156450.aspx

于 2013-07-07T05:44:29.357 回答
1

即使做了上面提到的所有更改,我也遇到了同样的错误。之后,我以这种方式更改我的代码:

我的网络配置:

<connectionStrings>
    <add name="conStr" connectionString="Data Source=SomeDS;Initial Catalog=SomeCatalog;User Id=myId;Password=myPass" />
</connectionStrings>

我的旧代码:

string ConnectionString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;


        using (var sqlConnection = new SqlConnection(ConnectionString ))
        {
            var result= sqlConnection.Query<My_Class>("MY_PROC_NAME", new { count }, commandType: CommandType.StoredProcedure);
            return result;
        }

我的新代码:

var dt = new DataTable();

                using (var cnn = new SqlConnection(ConnectionString ))
                using (var cmd = new SqlCommand("MY_PROC_NAME", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cnn.Open();
                    var sqlReader = cmd.ExecuteReader();
                    if (sqlReader.HasRows)
                        dt.Load(sqlReader);

                    cnn.Close();
                }
                result = dt.DataTableToList<My_Class>();

DataTableToList 是一种转换器方法,如果您需要我也可以分享它。

于 2016-02-05T11:53:24.933 回答