I am trying to dynamically set the connection string of an ObjectDataSource in a VS 3.5 Web project. I have found some code in other posts that explains how to do it, but it is not working for me. The code is below:
However, for me, e.ObjectInstance.GetType().GetProperty("Connection") is null, so the above code throws an exception. This occurs on multiple ObjectDataSources.
When I run e.ObjectInstance.GetType().GetProperties(), the following 3 properties come up:
{System.Reflection.PropertyInfo[3]}
[0]: {Boolean ClearBeforeFill}
[1]: {System.ComponentModel.ISite Site}
[2]: {System.ComponentModel.IContainer Container}
Thus, the connection property doesn't seem to be showing, even though I see it in when I expand under the Locals tab. Any idea why I cannot access this property? I have set the ConnectionModifier propery on the tableadapter to True. Is there any other
way to change the connection string for the object data source at runtime?
How do you create the ObjectDataSource? Did you drag it from the control panel? I didn't see any problem from your current code. You can delete the ObjectDataSource and redrag it again if possible.
Please let me know if there's still any problem.
Thanks.
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
I seemed to have fixed the issue, but not sure if a bug in Visual Studio was causing the issue, or if it is designed that way. Previously, I was creating my connection string in the settings.settings file as an application variable. When I had that connection
string, then ran the above code, it was giving me an error. I removed my connection string from the settings.settings file and changed my dataset to use a connectionstring from the web.config file. All of a sudden the above code worked properly.
Thus, the property e.ObjectInstance.GetType().GetProperty("Connection") does not get set when using an application variable as a connection string, but it does when using a web.config connection string. Is this a bug in VS, or is
that by design?
Either way, the code is working for me now that I am using a connectionstring from my web.config file. Thanks for the reply.
I've been struggling with the same problem. I've got rid of the connectionstring in settings.settings and replaced it by an explicit string, but I still get the same null reference.
The TableAdapter Conneciton proerty Modifer is public.
I can changes the tableadapter connectionstring in code without an issue.
But trying to set the connection property for a datasource object instance just will not pick up the connection property.
I'm using VS2008 & c#
Lost 3 or 4 hours on this so far, so any help much appreciated.
You can leave the connection string in settings but you must override it to read from the web config. While in settings click on view code at the top. This takes you to the code behind of settings. Import
Imports System.Configuration
Select mysettings events from the left dropdown, and select settings loaded from the right dropdown Add this code
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
soccerwiz13
Member
4 Points
5 Posts
Dynamically set connection string for ObjectDataSource
Dec 01, 2008 04:50 PM|LINK
I am trying to dynamically set the connection string of an ObjectDataSource in a VS 3.5 Web project. I have found some code in other posts that explains how to do it, but it is not working for me. The code is below:
protected void ObjectDataSource1_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
{
if (e.ObjectInstance != null)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = MyConnectionManager.ConnectionString;
e.ObjectInstance.GetType().GetProperty("Connection").SetValue(e.ObjectInstance, conn, null);
}
}
However, for me, e.ObjectInstance.GetType().GetProperty("Connection") is null, so the above code throws an exception. This occurs on multiple ObjectDataSources.
When I run e.ObjectInstance.GetType().GetProperties(), the following 3 properties come up:
{System.Reflection.PropertyInfo[3]}
[0]: {Boolean ClearBeforeFill}
[1]: {System.ComponentModel.ISite Site}
[2]: {System.ComponentModel.IContainer Container}
Thus, the connection property doesn't seem to be showing, even though I see it in when I expand under the Locals tab. Any idea why I cannot access this property? I have set the ConnectionModifier propery on the tableadapter to True. Is there any other way to change the connection string for the object data source at runtime?
Thanks
objectdatasource connection string
Wencui Qian ...
All-Star
56784 Points
5796 Posts
Microsoft
Re: Dynamically set connection string for ObjectDataSource
Dec 04, 2008 05:27 AM|LINK
Hi soccerwiz13,
How do you create the ObjectDataSource? Did you drag it from the control panel? I didn't see any problem from your current code. You can delete the ObjectDataSource and redrag it again if possible.
Please let me know if there's still any problem.
Thanks.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
soccerwiz13
Member
4 Points
5 Posts
Re: Dynamically set connection string for ObjectDataSource
Dec 04, 2008 03:44 PM|LINK
I seemed to have fixed the issue, but not sure if a bug in Visual Studio was causing the issue, or if it is designed that way. Previously, I was creating my connection string in the settings.settings file as an application variable. When I had that connection string, then ran the above code, it was giving me an error. I removed my connection string from the settings.settings file and changed my dataset to use a connectionstring from the web.config file. All of a sudden the above code worked properly.
Thus, the property e.ObjectInstance.GetType().GetProperty("Connection") does not get set when using an application variable as a connection string, but it does when using a web.config connection string. Is this a bug in VS, or is that by design?
Either way, the code is working for me now that I am using a connectionstring from my web.config file. Thanks for the reply.
objectdatasource connection string
DEJHDante
Member
12 Points
3 Posts
Re: Dynamically set connection string for ObjectDataSource
Jul 31, 2009 09:29 AM|LINK
Hi
I've been struggling with the same problem. I've got rid of the connectionstring in settings.settings and replaced it by an explicit string, but I still get the same null reference.
The TableAdapter Conneciton proerty Modifer is public.
I can changes the tableadapter connectionstring in code without an issue.
But trying to set the connection property for a datasource object instance just will not pick up the connection property.
I'm using VS2008 & c#
Lost 3 or 4 hours on this so far, so any help much appreciated.
David
objectdatasource
Hiobx
Member
2 Points
1 Post
Re: Dynamically set connection string for ObjectDataSource
Sep 29, 2009 12:43 PM|LINK
You can leave the connection string in settings but you must override it to read from the web config. While in settings click on view code at the top. This takes you to the code behind of settings. Import
Imports System.Configuration
Select mysettings events from the left dropdown, and select settings loaded from the right dropdown Add this code
Me.Item("SettingsConnectionString Name") = ConfigurationManager.ConnectionStrings("Web.config ConnectionString Name").ConnectionString
This will override the settings connection and replace it with the one in the web.config
A.Aziem
Member
4 Points
2 Posts
Re: Dynamically set connection string for ObjectDataSource
Dec 21, 2010 10:44 AM|LINK
Hi Hiobx,
It seems to be in web application. I have same problem in a web site and I put all my controls in a separate dll
public class Ods : ObjectDataSource { public Ods() { this.ObjectCreated += new ObjectDataSourceObjectEventHandler(Ods_ObjectCreated); } void Ods_ObjectCreated(object sender, ObjectDataSourceEventArgs e) { if (e.ObjectInstance != null) { PropertyInfo propInfo = e.ObjectInstance.GetType().GetProperty("Connection"); if (propInfo != null) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = System.Web.HttpContext.Current.Session["ConnString"].ToString(); propInfo.SetValue(e.ObjectInstance, conn, null); } } } }TigerWoods
Member
22 Points
25 Posts
Re: Dynamically set connection string for ObjectDataSource
Feb 25, 2011 07:59 AM|LINK
i use the code below:
protected void ObjectDataSourceWebsitesInfo_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
{
if (e.ObjectInstance != null)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionStringNameInWeb.Config"].ConnectionString;
e.ObjectInstance.GetType().GetProperty("Connection").SetValue(e.ObjectInstance, conn, null);
}
}
and i got this error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I need your help plz.