密钥“用户实例”的值无效

时间:2013-11-25 03:11:53

标签: c# asp.net sql-server visual-studio-2010

我有这个代码的问题,其中GridView1.DataBind();为关键'用户实例'提供错误无效值。我哪里做错了?

if (Byday.Checked == true)
    {
        // ConnectionString to NorthWind Database.
        string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\shafiq\\Desktop\\cuba\\App_Data\\radiation.mdf;Integrated Security=True;User Instance=Truee";

        // Create SQLDataSource.
        SqlDataSource sqlDataSource = new SqlDataSource();
        sqlDataSource.ID = "SqlDataSource123";
        this.Page.Controls.Add(sqlDataSource);

        // Bind ConnectionString to SQLDataSource.
        sqlDataSource.ConnectionString = connectionString;
        // Retrieve records with only 5 Columns from Employees table of NorthWind Database.
        sqlDataSource.SelectCommand = "SELECT [date], [data] FROM [loc1] WHERE (([data] >= '2') AND ([date] >= '" + txtStartDate.ToString() + "') AND ([date] < '" + Label9.ToString() + "')) ORDER BY [data] DESC, [date]";

        // Bind SQLDataSource to GridView after retrieving the records.
        GridView1.DataSource = sqlDataSource;
        GridView1.DataBind();
    }

2 个答案:

答案 0 :(得分:1)

在您的连接字符串中,您说:

User Instance=Truee

应该是:

User Instance=True

答案 1 :(得分:1)

更改User Instance=Truee

User Instance=True

并使用以下参数

sqlDataSource.SelectCommand = "SELECT [date], [data] FROM [loc1] WHERE (([data] >= '2') AND ([date] >=@fromDate) AND ([date] < @toDate)) ORDER BY [data] DESC, [date]";

您需要根据类型设置参数值。如果您的[date]列是数据库中的日期时间类型,请将文本输入转换为Datetime,然后设置参数

sqlDataSource.SelectParameters.Add("fromDate", DbType.DateTime, fromDate);
sqlDataSource.SelectParameters.Add("toDate", DbType.DateTime, toDate);

您需要将txtStartDate和Label9.Text字符串转换为DateTime格式