newbie88
newbie88

Reputation: 75

Invalid value for key 'user instance'

i have a problem with this code where the GridView1.DataBind(); give the error Invalid value for key 'user instance'. where did i do wrong?

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();
    }

Upvotes: 0

Views: 657

Answers (2)

Damith
Damith

Reputation: 63065

change User Instance=Truee

to

User Instance=True

And use parameters like below

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

you need to set parameter values based on the type. if your [date] column is datetime type in the database, convert your text inputs to Datetime and then set the parameters

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

you need to convert txtStartDate and Label9.Text strings to DateTime format

Upvotes: 1

Ryan Nigro
Ryan Nigro

Reputation: 4619

In your connection string, you say:

User Instance=Truee

That should be:

User Instance=True

Upvotes: 1

Related Questions