c# - Connection string without error page? -


i have gridview , sqldatasource this:

<asp:sqldatasource id="sqldatasource1" runat="server"      connectionstring="<%$ connectionstrings:dbconnectionstring %>"      providername="<%$ connectionstrings:dbconnectionstring.providername %>"      selectcommand="select * [string]"> </asp:sqldatasource>  <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false"      datakeynames="id" datasourceid="sqldatasource1">     <columns>         <asp:boundfield datafield="id" headertext="id" insertvisible="false"              readonly="true" sortexpression="id" />         <asp:boundfield datafield="hostname" headertext="hostname"              sortexpression="hostname" />         <asp:boundfield datafield="date" headertext="date" sortexpression="date" />         <asp:boundfield datafield="int" headertext="int" sortexpression="int" />     </columns> </asp:gridview> 

but when connection getting error, page getting error too.

i want use connection without error page. if connection have problem(not connect, table not exist etc.), page must open without error.

i can try/catch oledbconnection @ code behind. how can sqldatasource?

connectionstring:

<add name="dbconnectionstring" connectionstring="data source=databaseip:1521/orcl;persist security info=true;user id=username;password=pass;unicode=true" providername="system.data.oracleclient" /> 

following comment here basic example can expand required.

using (sqlconnection connection = new sqlconnection("your connection string")) {   string sqlquery = "your query";      using (sqlcommand cmd = new sqlcommand(sqlquery, connection))     {       using (sqldataadapter sda = new sqldataadapter())       {           connection.open();           sda.selectcommand = cmd;           datatable dt = new datatable();           sda.fill(dt);           gridview1.datasource = dt;           gridview1.databind();        }     } } 

the code makes use of using block object disposed correctly. recommend have separate function returns datatable , in page_loaddo

gridview1.datasource = functionname; gridview1.databind(); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -