AlexDeveloper12
AlexDeveloper12

Reputation: 63

Why is my C# 'System.data.sqlclient' not working correctly?

I am creating a Windows 10 Universal App and require the 'data table' functionality, however when I create a DataTable object

DataTable test = new DataTable();

an error occurs

DataTable does not contain a constructor that takes 0 arguments

Also, I have added the namespace using clauses:

using System.Data;
using System.Data.SqlClient;

I was wondering if anybody knew how to solve this error?

In addition, when viewing intellisense I cannot see the SqlDataAdapter functionality, however, I can see the SqlDataReader.

I was wondering if anybody has any insight into these problems?

I am running Microsoft Visual Studio 2015 Enterprise and have Microsoft SQL Server Management Studio installed.

EDIT

I don't have a class named DataTable and I tried what John Wu said and created the 'var' test variable and I still receive the same error.

Here is the full code for my page

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SQLite.Net;
using System.Data.SqlClient;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using System.Data;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace PeriodicTableWin10
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        string ConnectionString = "Connection string goes here";

        private void ShowElement(string ElementName)
        {
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                try
                {
                    SqlCommand GetElement = new SqlCommand("Select ElementName from tblElement WHERE ElementName=@Element");
                    GetElement.Parameters.Add(new SqlParameter("@Element", ElementName));

                    DataTable test = new DataTable();
                }
                catch(Exception e)
                {
                    e.ToString();
                }
            }
        }
    }
}

Upvotes: 0

Views: 1087

Answers (1)

John Wu
John Wu

Reputation: 52270

Change

DataTable test = new DataTable()

to

var test = new System.Data.DataTable()

to ensure you are instantiating the right class.

Upvotes: 2

Related Questions