Custom Feature Different Database Call and display

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.
10/30/2012 3:21:19 PM
Gravatar
Total Posts 199

Custom Feature Different Database Call and display

I have a custom feature built and I want to call information stored in a different sql database.

I have a new key setup in user.config with all the correct access info called "MSSQLConnectionStringHighland"

in my feature code-behind I have a connection string established: string connectionString = ConfigurationManager.AppSettings["MSSQLConnectionStringHighland"];

now all I want to do is grab one record from that db and display it, but could not find a good example.

the below works fine in all my stand alone projects, but I am guessing it is not correct/best way for mojo.  Any guidance would be greatly appreciated.

 

  private void PopulateControls()
{

 

string connectionString1 = ConfigurationManager.AppSettings["MSSQLConnectionStringHighland"];
SqlConnection con = new SqlConnection(connectionString1);
string sql1 = "SELECT TOP 1 * FROM News WHERE NewsCatID = 6 ORDER BY NewsDate Desc";
SqlCommand cmd = new SqlCommand(sql1, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
lbl_Description.Text = reader["Description"];
}

}

11/5/2012 1:26:53 PM
Gravatar
Total Posts 18439

Re: Custom Feature Different Database Call and display

In mojoPortal we don't like to talk directly to the database from UI code (nor have any references to it). UI code talks to business class objects which in turn call the data access code.

Of course in your own custom features you are free to do whatever you want. There is ample data access code in mojoPortal that can be studied for examples though the data layer for MS SQL uses stored procedures whereas most of the other data layers build up sql statements using StringBuilder. 

Be careful about using DataReaders to make sure you close the connection its best to use using blocks like change your code like this:

string sql1 = "SELECT TOP 1 * FROM News WHERE NewsCatID = 6 ORDER BY NewsDate Desc";

using(SqlConnection con = new SqlConnection(connectionString1))
{
SqlCommand cmd = new SqlCommand(sql1, con)
con.Open();
using(SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
lbl_Description.Text = reader["Description"];
}

}
}
}

the using block makes sure to dispose the object and close the underlying connection, otherwise you could be leaking connections from the conneciton pool and this will hurt performance and can cause the app to hang if it exhausts the pool of available connections. If your sql returns only a single row its probably cleaner to use if instead of while

Hope that helps,

Joe

11/8/2012 12:58:40 PM
Gravatar
Total Posts 199

Re: Custom Feature Different Database Call and display

thank you for this detailed response, I will be improving on this feature as you suggested.

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