+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / setting up external DB within web.config file

setting up external DB within web.config file

11 posts, 0 answered
  1. Viren
    Viren avatar
    41 posts
    Registered:
    03 Aug 2010
    19 Oct 2010
    Link to this post
    Hi,

    I am tryin to setup an external provider for images to the image control in my application. I am modifying the config file by making the following additions to the already existing entries. I am having problems with the Red colored content:
    <connectionStrings>
            <add name="MyData" connectionString="data source=VM-SQL2008\SQL2008;UID=myID;PWD=mypassword; initial catalog=MyData" providerName="System.Data.SqlClient"/>
    /connectionStrings>

    <dataAccess defaultConnection="DefaultConnection">
         <connections>
                 <add name="MyData" driver="Telerik.DataAccess.Providers.ExtendedSqlServer2005Provider, Telerik.DataAccess" connectionStringName="MyData"/>
         </connections>
    </dataAccess>

    <providers>
                    <clear/>                 

                             <add name="MyDataProvider" urlRewriteFormat="~/{Provider}/{LibraryName}/[Name].sflb.ashx" urlDateTimeFormat="yy- MM-dd" urlWhitespaceChar="_" visible="False" defaultMetaField="Name" applicationName="/Libraries" allowVersioning="False" allowLocalization="False" localizationProviderName="" allowWorkflow="False" securityProviderName="" versioningProviderName="" connectionStringName="MyData" type="Telerik.Libraries.Data.DefaultProvider, Telerik.Libraries.Data" />

    </providers>

    I am using MS SQL Server 2008 locally (on the same machine as sitefinity).

    For the image control to  be able to pull the images (binary data) from 1 column in 1 particular table in "MyData" database, I am guessing that urlRewriteFormat="~/{Provider}/{LibraryName}/[Name].sflb.ashx" would not work and definitely not type="Telerik.Libraries.Data.DefaultProvider, Telerik.Libraries.Data". Am I right here? I also want to set the ApplicationName correct which I think in this case woould be the sitefinity image control client. Can I get some help with the strings here as I am running into a lot of config errors.

    Thanks.



  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    19 Oct 2010
    Link to this post
    Hello Viren,

    This configuration will work only of you server IContent objects. The applicationName should be different for the newly added provider, otherwise it will share items from the default one - Libraries.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Viren
    Viren avatar
    41 posts
    Registered:
    03 Aug 2010
    19 Oct 2010
    Link to this post
    Thanks Ivan.
    I have now made the following changes:

    applicationName="/MyImages"

    And now I have created a converter class in App_Code folder wherein I intend to convert all the binary image data to IContent.
    1. Is this the correct way to do it?

    Can I just remove the provider entry from the web.config file and establish a connection with my database using:

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
            mySqlCommand.CommandText = "SELECT picture_column FROM pictures_table";
            mySqlConnection.Open();               
            SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();     
            Bitmap bmp;
            while (mySqlDataReader.Read())
            {
                MemoryStream stream = new MemoryStream((byte[])productsSqlDataReader["equ_pict"]);        
                bmp = new Bitmap(stream);
                newContent.Content = bmp;
                newContent.SetMetaData("Name", "Image1");
                newContent.SetMetaData("Author", "PS");
                newContent.SetMetaData("Description", "Imagine Some text here");
                //save the content
                imgmanager.SaveContent(newContent);
            }

    I think that if this piece of code inside a file called DataContentManager under App_Code works, then I can think of proceeding further with the image gallery control. Even if this piese of code looks correct logically, I still doubt that the config file problem would be removed persist?

    However if I keep both, the contents of my web.config file and also this c# code in my file, then can I use the "MyDataProvider" with the public property of the image control? Will it load the image(s) ?

    Can this be done or will this be a problem as well?? I am wondering why do I need to config file entries if I am going to extablish the connection in my C# code file. Or is there a way to assign a provider name inside the c# code itself?
    Also I want to make sure that this DataContentManager.cs file that I created under App_Code gets executed everytime the sitefinity website is run/loaded/accessed. I looked around a lot to see if I could add this file as part of the website startup but could not find a solution. How can I assure that it would run everytime with sitefinity? Or can I ad this code to the Default.master.cs so that it loads up for sure?

    Thanks.

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    19 Oct 2010
    Link to this post
    Hello Viren,

    You can override CreateDataSource method of ImageGallery control and there move these content that makes connection to your database. Then create a class in App_Code or class library that you call inside CreateDataSource and return the IContent objects as a List or Generic List.

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Viren
    Viren avatar
    41 posts
    Registered:
    03 Aug 2010
    20 Oct 2010
    Link to this post
    Hi,

     I am using an image gallery control on one of my templates inside sitefinity. The template is just a .master file without a code behind. So I added a code behind file manually to main template.master file and wrote a CreateDataSource method that opens a connection to my database. Then as guided, I am calling a class under App_Code that returns images from my database as IContent objects from within this overridden CreateDataSource method as IList collection.

    However when I try to build the sitefinity project in visual studio I get the following error:

    Error   119   'App_Master_templateName.CreateDataSource(object, System.EventArgs)': no suitable method found to override   C:\Program Files (x86)\Telerik\Sitefinity3.7\WebSites\projectName\App_Master\templateName.master.cs   30   30   C:\...\projectName\

    I also tried inserting the following inside the templateName.master file but this did not work either:

    <script runat="server">
    private IList CreateDataSource()
        {

            SqlConnection mySqlConnection = new SqlConnection("Data Source=VM-SQL2008\\SQL2008;Initial Catalog=PS_2010Data;UID=synergyweb;PWD=support$1;");
            SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
            mySqlCommand.CommandText = "SELECT * FROM StoredFile where StoredFilesPk = 1";
            mySqlConnection.Open();
            SqlDataReader productsSqlDataReader = mySqlCommand.ExecuteReader();

            //IContent myimage = (IContent)new MyImageConverter(productsSqlDataReader);

            Telerik.Cms.Engine.ContentManager imgmanager = new Telerik.Cms.Engine.ContentManager();
            Telerik.Cms.Engine.IContent newContent = imgmanager.CreateContent("image/bmp");
            DataTable myDataTable = productsSqlDataReader.GetSchemaTable();
            Bitmap bmp;
            while (myDataTable.Rows.Count > 0)
            {
                DataRow newrow = myDataTable.Rows[0];
                MemoryStream stream = new MemoryStream((byte[])productsSqlDataReader["FileData"]);
                bmp = new Bitmap(stream);
                newContent.Content = bmp;
                newContent.SetMetaData("Name", "Image1");
                newContent.SetMetaData("Author", "PS");
                newContent.SetMetaData("Description", "Imagine Some text here");
                //save the content
                imgmanager.SaveContent(newContent);
                //IList tmp = newContent.Content

                productsSqlDataReader.Close();
                mySqlConnection.Close();
            }

            return ((IList)newContent as IList);
        }
    </script>

    Then when I try to insert the following inside the web.config file

    <add name="MyImageDataProvider" urlRewriteFormat="~/{Provider}//[Name].sflb.ashx" urlDateTimeFormat="yy-MM-dd" urlWhitespaceChar="_" visible="False" defaultMetaField="Name" applicationName="/MyImages" allowVersioning="False" allowLocalization="False" localizationProviderName="" allowWorkflow="False" securityProviderName="" versioningProviderName="" connectionStringName="MyData" type="Telerik.Libraries.Data.DefaultProvider, Telerik.Libraries.Data"/>

    and change the Provider property of the image gallery control from "Libraries" to "MyImageDataProvider" the control throws the following error when I click on "I'm Done"

    Property accessor 'IsEmpty' on object 'Telerik.Libraries.WebControls.ImageGallery' threw the following exception:'No connection with the name of "MyData" was found.'

    Can you please guide me as to where to write the method CreateDataSource to override the base class method? I do not have code behind file for the template of the image gallery control as it is an embedded control. How do I point the image gallery control to this new CreateDataSource method? I am assuming that I can continue using the generic image gallery control and just override the CreateDataSource method. If not then do I have to inherit a new image gallery control? What are the web.config settings needed for that? I sincerely appreciate your help so far but I am not able to get past this wall. My basic need to be able to have an image gallery on my template that pulls images from my database instead of sitefinity DB and also to be able to customize the look of the singleItem when a thumbnail is clicked upon.

    Thanks
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    20 Oct 2010
    Link to this post
    Hi Viren,

    CreateDataSource is a method of ContentBasic class from which ImageGallery control inherits. You could check the API reference.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Viren
    Viren avatar
    41 posts
    Registered:
    03 Aug 2010
    22 Oct 2010
    Link to this post
    Hi,
    My web.config file contents
    In the connectionstrings section:

    <add name="MyData" connectionString="data source=VM-SQL2008\SQL2008;UID=name;PWD=password;initial catalog=MyData" providerName="System.Data.SqlClient"/>

    DataAccess sections:


    <add name="MyDataAccess" driver="Telerik.DataAccess.Providers.ExtendedSqlServer2005Provider, Telerik.DataAccess" connectionStringName="MyData"/>

    In the providers section:


    <add name="MyDataProvider" urlRewriteFormat="~/{Provider}//[Name].sflb.ashx" urlDateTimeFormat="yy-MM-dd" urlWhitespaceChar="_" visible="False" defaultMetaField="Name" applicationName="/MyImages" allowVersioning="False" allowLocalization="False" localizationProviderName="" allowWorkflow="False" securityProviderName="" versioningProviderName="" connectionStringName="MyData" type="Telerik.Libraries.Data.DefaultProvider, Telerik.Libraries.Data"/>

    I goto the image gallery control > advanced > Data  and set the provider to “MyDataProvider” and get the  following error:


    Property accessor 'IsEmpty' on object 'Telerik.Libraries.WebControls.ImageGallery' threw the following exception:'No connection with the name of "MyData" was found.'

     

    The contents of the my table inside my database are not IContents yet. I have an embedded c# code in my GalleryListView.ascx template file that converts these images to IContents. However it is clear its not working because of no database connection. Any help on this?



     

  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    22 Oct 2010
    Link to this post
    Hello Viren,

    1. Please use IContent objects
    2. You have to override the control and set the datasource to point to a source of IContent objects.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. Viren
    Viren avatar
    41 posts
    Registered:
    03 Aug 2010
    22 Oct 2010
    Link to this post
    I am using an external template for the control. Can you please clarify on "override the control" ? How do I set the datasource to IContent objects when the images in my db are in binary format.?

    Thank you again.
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    22 Oct 2010
    Link to this post
    Hello Viren,

    The driver is correct but coonnectionStringName inside the lines below should be set to MyDataAccess, since you are going to use the dataAcess connection.

    <add name="MyDataProvider" urlRewriteFormat="~/{Provider}//[Name].sflb.ashx" urlDateTimeFormat="yy-MM-dd" urlWhitespaceChar="_" visible="False" defaultMetaField="Name" applicationName="/MyImages" allowVersioning="False" allowLocalization="False" localizationProviderName="" allowWorkflow="False" securityProviderName="" versioningProviderName="" connectionStringName="MyDataAccess" type="Telerik.Libraries.Data.DefaultProvider, Telerik.Libraries.Data"/>

    Note that this will work only if you use Sitefinity's data provider  type="Telerik.Libraries.Data.DefaultProvider, Telerik.Libraries.Data"

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  11. Viren
    Viren avatar
    41 posts
    Registered:
    03 Aug 2010
    22 Oct 2010
    Link to this post
    Yes I am sorry. That is a typo while I tried to change the name because I wanted to hide the real names. I have the connectionstringName set correctly to point to my database.

    It is clear that I will not be using Sitefinity's data provider as I am going to use the images from my database. What is the "type" I use in that case? Can you provide me steps to have my image gallery control take images from my database ?

Register for webinar
11 posts, 0 answered