Connect to database with custom code (error:System.NullReference)

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
2/8/2010 4:34:02 PM
Gravatar
Total Posts 76

Connect to database with custom code (error:System.NullReference)

Hello,

Im trying to connect to my new custom table in the mojo-database.

I have the following piece of code:

protected void Button1_Click(object sender, System.EventArgs e)
{

//Instantiate a connection.
string conn = string.Empty;
conn = ConfigurationManager.ConnectionStrings["MSSQLConnectionString"].ConnectionString;

System.Data.SqlClient.SqlConnection cnn1 = new System.Data.SqlClient.SqlConnection(conn);
cnn1.Open();

//Declare command to validate MemberID and password.
System.Data.SqlClient.SqlCommand cmd1 = new System.Data.SqlClient.SqlCommand();
cmd1.CommandType = System.Data.CommandType.StoredProcedure;
cmd1.CommandText = "GetUser";
cmd1.Connection = cnn1;

I get an error in the line: conn = ConfigurationManager.ConnectionStrings["MSSQLConnectionString"].ConnectionString;

It says: System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="App_Web_jnxwiwtm"
StackTrace:
at HelloMojoModule.ascx.Button1_Click(Object sender, EventArgs e) in c:\Projects\Mojoportal\Web\MyCustomModules\HelloMojoUserControl.ascx:line 50
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:

 

Has it something to do with the connectionstring "MSSQLConnectionString" ??

This string stands in the user.config:

<add key="MSSQLConnectionString" value="server=****.*****.com;UID=*****;PWD=******;database=databasemojo" />

Please help! Thnx....
 

2/9/2010 12:18:50 PM
Gravatar
Total Posts 76

Re: Connect to database with custom code (error:System.NullReference)

:) Solved it!


Used appsettings instead of connectionstrings in web.config

 

You must sign in to post in the forums. This thread is closed to new posts.