Showing posts with label smo. Show all posts
Showing posts with label smo. Show all posts

Monday, March 19, 2012

Error when creating a table based function

I'm trying to create a table based function using SMO. The problem is that I get an error saying that I need to set the DataType property on the function before it can be created. However, the documentation clearly says that the DataType property is null for Table based functions. I can't even set this value to null either!

Here's a code snippet that shows what I'm trying to do.

if (returnType.ToLower() != "table")

{

Type clrDataType = Type.GetType(returnType);

returnSqlDataType = GetSqlDataType(clrDataType);

function.FunctionType = Smo.UserDefinedFunctionType.Scalar;

function.DataType = returnSqlDataType;

}

else

{

function.FunctionType = Smo.UserDefinedFunctionType.Table;

function.TableVariableName = xmlNode.Attributes["tableVariableName"].Value;

XmlNode returnTableDescription = GetChildOfParent(xmlNode, "ReturnTableDescription");

XmlNode columnsNode = GetChildOfParent(returnTableDescription, "Columns");

foreach (XmlNode columnNode in columnsNode.ChildNodes)

{

function.Columns.Add(CreateUdfColumn(function, columnNode));

}

}

The xmlNode that is being referenced is an XmlNode that is describing how to create the table. returnType is a variable that is set from one of the xmlNode's attributes.

When I call on function.Create() then I get the error that I posted above. Any help anyone can give is appreciated. One thing I also tried doing was to connect to an already existing Table based function and look at its SMO properties. Sure enough, its DataType property was null.

MSFT: Is this a bug?

Never mind on this post.....it was my mistake. The DataType property not being set wasn't on the function itself, but rather on the columns collection.

Problem solved.

Error when create a Server object using SMO

Please help: I got this error and can't figure out what went wrong.

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception. on the line that create Server object.

This is the code (Visual C# 2005 - SQL Server 2000 - Windows Active Directory)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Common;

namespace SSMO

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

Server oSvr = new Server("myServer");

foreach (Database oDB in oSvr.Databases)

{

cboDatabases.Items.Add(oDB.Name.ToString());

}

}

}

I guess there should be additionally some inner exceptions which should clarify th error in detail.

HTH, Jens Suessmeyer.


http://www.sqlserver2005.de

|||

This is the error:

System.TypeInitializationException was unhandled
Message="The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception."
Source="System.Data"
TypeName="System.Data.SqlClient.SqlConnection"
StackTrace:
at System.Data.SqlClient.SqlConnection..ctor()
at Microsoft.SqlServer.Management.Common.ServerConnection..ctor()
at Microsoft.SqlServer.Management.Smo.ExecutionManager..ctor(String name)
at Microsoft.SqlServer.Management.Smo.Server..ctor(String name)
at SSMO.Form1.Form1_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at SSMO.Program.Main()
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

|||

As the constructor of the SQLConnection is throwing an error, this might be just a slight misconfiguration problem. As I searched through the Net I found that this could be something based on a misconigured app.config file or the lack of a bad installation where the performance counters of the SQL Connection classes are not fully installed and registered. Never had that, but try to use a more simple command to make sure that this is based on that error:

new
System.Data.SqlClient.SqlConnection()).Dispose();


HTH, Jens Suessmeyer.


http://www.sqlserver2005.de