0

I having an aspx.cs file that I am adding code to. I have all the right namespaces and references in my solution, but my code is referencing the wrong namespace with the following error on my Server server = new Server()

System.Web.UI is a 'property' but used as a 'type'

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.IO;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IISLoggingSolution
{
    public partial class Main : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //connection string for SQL DB
            string connectionString = @"Data Source=Computer\SQLEXPRESS;Initial Catalog=IISLogs;Integrated Security=True";

            //Location of the query to be ran
            string script = File.ReadAllText(@"C:\\Users\\User\\Desktop\\query.txt");

            SqlConnection conn = new SqlConnection(connectionString);

            //this is the error line
            Server server = new Server(new ServerConnection(conn));

            //executes query
            server.ConnectionContext.ExecuteNonQuery(script);


        }
    }
}

How can I get it to use the Microsoft.SqlServer.Management.Smo instead of trying to used the System.Web.UI?

2 Answers 2

3
using NS_Server = Microsoft.SqlServer.Management.Common;
using System.Web.UI;

...

NS_Server.Server server = new NS_Server.Server();

the line using NS_Server is an alias. You can use it as a shortcut when there are namespace collisions.

Sign up to request clarification or add additional context in comments.

2 Comments

I did not know you could give alias to namespaces, this could prove beneficial in the future. Thanks!
np! glad to help.
0

You can use an alias directive:

using Server = Microsoft.SqlServer.Management...Server;

It may not work if property takes precedence, but in that cas you can simply specify the full namespade path.

var server = new Microsoft.SqlServer.Management....Server();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.