I use an entity structure to make 2 requests, one after another, the first always works fine, but the second always returns me: The ConnectionString property has not been initialized.
If I change the order of the methods, the same thing happens, the first works fine, the second throws this exception.
The code I use on the page is as follows:
> var count = RequestBaseBL.GetGenericResultsCount(query.QuerySql); > > var datatable = RequestBaseBL.GetGenericResults(query.QuerySql, 0);
and in my dal
public int GetGenericResultsCount(string strsql) { using (var connection = (SqlConnection)_context.Database.Connection) { var adapter = new SqlDataAdapter(strsql, connection); var results = new DataSet(); adapter.Fill(results, "Results"); return results.Tables["Results"].Rows.Count; } } public DataTable GetGenericResults(string strsql, int pageIndex) { StringBuilder sb = new StringBuilder(); sb.Append("WITH MyPagedData as ( "); int indexFrom = strsql.IndexOf("from"); sb.Append(strsql.Substring(0, indexFrom)); sb.Append(", "); sb.Append("ROW_NUMBER() OVER(ORDER BY RequestBaseId DESC) as RowNum "); sb.Append(strsql.Substring(indexFrom)); sb.Append(") "); sb.Append("SELECT * from MyPagedData where RowNum between @StartIndex and @StartIndex + 10"); using(var connection = (SqlConnection)_context.Database.Connection) { var adapter = new SqlDataAdapter(sb.ToString(), connection); adapter.SelectCommand.Parameters.Add("@StartIndex", SqlDbType.Int).Value = pageIndex; var results = new DataSet(); adapter.Fill(results, "Results"); return results.Tables["Results"]; } }
source share