Posted: 6/29/2011
Greetings,
I am trying to make the grid display no data even when no database exists (it's in the process of renaming). I get Either the user, [], does not have access to the Financials database, or the database does not exist instead. How would I force the script just ignore the exception and populate the grid with blanks? Thanks
MyDBConn.ConnectionString =
"Provider=MSOLAP.4;Data Source=core-ssas2;Integrated Security=SSPI;Initial Catalog=Financials;Connect Timeout=6000"
Try
MyDBConn.Open()
MyDBConn.CreateCommand.CommandTimeout = 200
Catch exole As
System.Data.OleDb.OleDbException
Catch ex As
SystemException
Catch exe As
Exception
GridView2.Style(HtmlTextWriterStyle.Display) =
"none"
End
MyDBConn.Close()
Hi,
You sould add null to datasource and bind the grid, and you can set ShowHeaderWhenEmpty="true" property of the gridview (this is posible just in ASP .NET 4.0):
GridView2.DataSource = null; or = string.Empty; GridView2.DataBind();
Or, you can add empty row and then hide it after bidning.
Best Regards,
Gjorgji
You can fill the grid with list of objects made at front end side only. In that case you have to avoid using your source from database.
Thanks guys, I am pretty new to this and the issue kept bothering me for a while. Could you please elaborate on where I should put the codes you suggest. Below is the scripted gridview. Thanks a lot!
<asp:GridView ID="GridView2" Title='FinancialStats Cube' runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="Financials" GridLines="Vertical" EmptyDataText = "The Cube is being Renamed. Please Refresh the Page" Height="156px" PageSize="3" Width="288px"> <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
Also, I believe I need to use asp.net 1.0 for some compatibility purposes
If you use datatable add empty new row, and bind the grid.If you use list add empty object to your list, and bind the grid.
Dear Gjorgji,
I have added the new row to the data grid as suggested. To test, I have renamed the source database to simulate the connection failure. Still getting Either the user does not have access to the FinancialStats database, or the database does not exist
Thanks for your assistance
Its inevitably that you will get this exception, but as i can see you are using try{}catch{} block, if your logic for fetching the data is in try{} block, you can use finall{} block and here bind the grid, in catch block just log the message if you have logger.
Just add 'Nothing' to your GridView2.DataSource and call DataBind() method again.
In your ASPX code of your GridView, add <EmptyDataTemplate>
Example
VB.NET Code-behind:
Try MyDBConn.Open() MyDBConn.CreateCommand.CommandTimeout = 200 Catch GridView2.DataSource = Nothing GridView2.DataBind() End Try
<asp:GridView ID="GridView2" runat="server"> ... <EmptyDataTemplate> No Data </EmptyDataTemplate> </asp:GridView>
Be careful, you need to add <EmptyDataTemplate> No Data </EmptyDataTemplate> as part of the other aspx markup inside your GridView.
Hope this helps.
Posted: 6/30/2011
Thanks Hajan,
It turns out that the actual grid definition I had handles the connection exception well. The failure to connect happens in the MDX query definition below. How would I handle it? Thanks!
<asp:SqlDataSource ID="Financials" runat="server" ConnectionString="<%$ ConnectionStrings:Financials %>" SelectCommand="with set s as Tail(nonempty([Date].[year - month - date].[date desc].members), 3)select {[Measures].[Final Total GP - Actual], [GP Var]} on 0,s on 1from [Financials]" ProviderName="<%$ ConnectionStrings:Financials.ProviderName %>"> </asp:SqlDataSource>