Loading ...

Please Help Ignore Exceptions on asp.net GridView

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  web forms / data controls   » Please Help Ignore Exceptions on asp.net GridView

Please Help Ignore Exceptions on asp.net GridView

Posts under the topic: Please Help Ignore Exceptions on asp.net GridView

Posted: 6/29/2011

Lurker 20  points  Lurker
  • Joined on: 6/29/2011
  • Posts: 4

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

Try

MyDBConn.Close()


Posted: 6/29/2011

Starter 727  points  Starter
  • Joined on: 6/6/2011
  • Posts: 74

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


Posted: 6/29/2011

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490

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.


Posted: 6/29/2011

Lurker 20  points  Lurker
  • Joined on: 6/29/2011
  • Posts: 4

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

 


Posted: 6/29/2011

Starter 727  points  Starter
  • Joined on: 6/6/2011
  • Posts: 74

Hi,

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.

Best Regards,

Gjorgji


Posted: 6/29/2011

Lurker 20  points  Lurker
  • Joined on: 6/29/2011
  • Posts: 4

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


 


Posted: 6/29/2011

Starter 727  points  Starter
  • Joined on: 6/6/2011
  • Posts: 74

Hi, 

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.

As you say you want to foce binding or hidding the grid if this exception occurs.
Best Regards,
Gjorgji


Posted: 6/29/2011

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391

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

ASPX GridView Empty Data Template:
    <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

Lurker 20  points  Lurker
  • Joined on: 6/29/2011
  • Posts: 4

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 1
from [Financials]"
        ProviderName="<%$ ConnectionStrings:Financials.ProviderName %>">
    </asp:SqlDataSource>
   

 


Page 1 of 1 (9 items)