Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0problems with SqlDataSource.selectcommand when connecting to another database.problems with SqlDataSource.selectcommand when connecting to another database.
Previous
 
Next
New Post
8/28/2006 12:14 AM
 
I'm a complete noob at ASP.net 2.0, so I'm sure I'm missing something obvious.

I'm creating a module that connects to a remote SQL Server database to pull in some data. In my module .ascx file, I created a SqlDataSource ("mySqlDataSource") and a GridView ("myGridView") and everything worked fine (the query was "Select Top 5 From myTable WHERE Category=1"). Then I decided to get a little fancier, and added two settings to the module: "Number" and "Category". I would like to be able to change the number of retrieved records and the category, depending on the page that the module is on. I am able to  set and  update those two setting, but I cannot send them to the SqlDataSource.

As far as I can tell, I cannot get the syntax right for the selectcommand. I thought that simply adding:
<%
   Dim myNumber = CType(ModuleSettings("myNumber"), Integer)
   Dim myCategory = CType(ModuleSettings("myCategory"), Integer)
   mySqlDataSource.selectcommand = "Select Top "&myNumber&" From myTable WHERE myCategory="&myCategory
%>

on the line above the "<asp:SqlDataSource ID="mySqlDataSource"..." tag would do it, but the SqlDataSource control doesn't see the selectcommand string. I've tried also using SqlDataSource.SelectParameters (as per some of the examples on MSDN), but to no avail.  I'm using Visual Web Developer 2005 Express, on XP Home and Pro.

TIA, Marius



email: MY USER ID at gmail dot com
 
New Post
8/28/2006 3:11 PM
 
What you need are a couple of additions to web.config as shown here and then change a couple of lines in SqlDataProvider.vb and DataProvider.vb of your module.

I have tried to colour code the matching entries for you:

<configSections>
<sectionGroup name="dotnetnuke">
      <!-- the requirePermission attribute will cause a syntax warning - please ignore - it is required for Medium Trust support-->
      <section name="data" requirePermission="false" type="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke"/>

You add your setting here:
      <section name="MyData" requirePermission="false" type="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke"/>

<connectionStrings>
      <!--
Connection String for SQL Server 2000/2005   -->
      <add name="SiteSqlServer" connectionString="Server=(local);Database=DotNetNuke_2dev;uid=XX_sa;pwd=XX;" providerName="System.Data.SqlClient"/>

You add your setting here:

      <add name=" MyDataSiteSqlServer" connectionString="Server=(local);Database=MyDatabase;uid=My_sa;pwd=MyPassword;" providerName="System.Data.SqlClient"/>      </connectionStrings>
<appSettings>
<!-- Connection String for SQL Server 2000/2005 - kept for backwards compatability - legacy modules -->
    <add key="SiteSqlServer" value="Server=(local);Database=DotNetNuke_2dev;uid=Mysa;pwd=XX;"/>

You add your setting here:

      <add key=" MyDataSiteSqlServer " value="Server=(local);Database=MyDatabase;uid=My_sa;pwd= MyPassword;"/> 

Add the following just below the default

<data defaultProvider ---
</data>

<MyData defaultProvider="SqlDataProvider">
  <providers>
   <clear/>
    <add name="SqlDataProvider" type="DotNetNuke.Data.SqlDataProvider, DotNetNuke.SqlDataProvider"
      connectionStringName=" MyDataSiteSqlServer"
      upgradeConnectionString=""
      providerPath="~\Providers\DataProviders\SqlDataProvider\"
      objectQualifier=""
      databaseOwner="dbo"/>
  </providers>
</ MyData>

 Now to your module:
Change the line below in SqlDataProvider.vb to point to your MyData 

It follows:

    Inherits DataProvider
#Region "Private Members" 
    Private Const ProviderType As String = "MyData"

In DataProvider.vb change the objProvider line below to MyData:

Private Shared Sub CreateProvider()
        objProvider = CType(Framework.Reflection.CreateObject("MyData", "Stored Proc name here", ""), DataProvider)
 

Declan


 
New Post
8/28/2006 11:54 PM
 

Off the top of my head,

I not sure you have to change the web.config if you don't mind hard coding the connectionstring in your module code.  If it worked for you fine with just a simple select and you want to just make a dynamic sql string the you might try using string concatenation

 Dim myNumber as string = CType(ModuleSettings("myNumber"), string)
   Dim myCategory as string =  CType(ModuleSettings("myCategory"), string)
   mySqlDataSource.selectcommand = "Select Top " & myNumber & " From myTable WHERE myCategory=" & myCategory

This assumes the myCategory column in the database is a int type


Michael Jackson
Brillnat.com
Custom module development
Database access tokenized HTML modules
 
New Post
8/29/2006 12:20 PM
 
Hi Liffey, Thanks for showing me the "proper" way of doing it. However, I was just looking for a quick hack, like the one mhj outlined. The problem was that I'm so used to writing ASP spaghetti code, that I was placing the code in the wrong place. I had it in the .ascx file. I moved it to the ascx.vb file and everything worked.

I'm in the process of convinicnig the boss to move all the data into DNN and ditching the old database.

Again, thanks guys for helping me get a clue here.

Marius

email: MY USER ID at gmail dot com
 
New Post
8/29/2006 11:36 PM
 

I have a similar problem.  Although I am going thru and creating my own Sqldataprovider.

I have varified what liffey has put up there, but I am getting the following error:

DotNetNuke.Services.Exceptions.ModuleLoadException: Object reference not set to an instance of an object. ---> System.NullReferenceException: Object reference not set to an instance of an object. at DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(PortalModuleBase ctrlModule, Exception exc) at Settings.LoadSettings() in c:\Inetpub\wwwroot\DNN\DesktopModules\ServiceLocator\Settings.ascx.cs:line 64 at DotNetNuke.Modules.Admin.Modules.ModuleSettingsPage.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\DNN\Admin\Modules\ModuleSettings.ascx.vb:line 264 --- End of inner exception stack trace

When I stepped thru the code, I notice that it is getting that error when I tried to return the objProvider static variable.

objProvider was null.  This I found to be odd, so I did a check and verified that the objProvider was created.  Sure enough, I still get this error, just now at the line where I am trying to set the value of objProvider.

Any idea what I am doing wrong?

Thanks,

Keith

 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0problems with SqlDataSource.selectcommand when connecting to another database.problems with SqlDataSource.selectcommand when connecting to another database.


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out