0

This is my code:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=OCS-MXL930055N\\;Initial           Catalog=sample;Integrated Security=True";
        SqlCommand cmd = new SqlCommand

        con.Open("Select * from ShoppingList", con);

        con.Close("Select * from ShoppingList", con);
    }
}

And these are the lines I am having problems with:

con.Open("Select * from ShoppingList", con)();

con.Close("Select * from ShoppingList", con)();

Any help with what they mean? I'm not too sure what I'm doing wrong.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
user2952873
  • 9
  • 1
  • 2
  • It means that the compiler needs to know somehow you're trying to initialize a new instance (or several instances) of a type. In your case add parentheses to: `SqlCommand cmd = new SqlCommand();` Notice the brackets? – Aage Nov 04 '13 at 14:54

3 Answers3

5

Your statement:

SqlCommand cmd = new SqlCommand

should be :

SqlCommand cmd = new SqlCommand("Select * from ShoppingList");

Later when you are opening and closing the connection, just remove the parameters, these parameters are required for SqlCommand constructor.

You may have your code like:

using(SqlConnection con = new SqlConnection("Data Source=OCS-MXL930055N\\;Initial Catalog=sample;Integrated Security=True"))
using(SqlCommand cmd = new SqlCommand("Select * from ShoppingList", con))
{
    //.. code to execute command
}

Read about basic C#, constructors and ADO.Net Examples on MSDN

Habib
  • 219,104
  • 29
  • 407
  • 436
4

C# isn't ruby you need to make your intent known. There are 3 ways to instantiate an object, all using the new operator:: e.g.

var myObj = new MyObj(); // call a constructor (parameterless)
var myObjArray = new MyObj[10]; // create an array of 10 MyObjs
var myObj = new MyObj{someProperty="someValue"}; // initializer notation.

Note you can mix the array and initializer so you can do this and its legals::

var myInts = new int[]{1,2,3,4,5,6}; //create an int array with 6 values.

To fix your snippet you need to add parens like so::

   SqlCommand cmd = new SqlCommand();

If you are sending strings of sql I strongly recommend using the library Dapper-Dot-Net found on nuget.

Michael B
  • 7,512
  • 3
  • 31
  • 57
1

You haven't correctly created your SqlCommand instance:

SqlCommand cmd = new SqlCommand

turns into:

SqlCommand cmd = new SqlCommand("Select * from ShoppingList");

which in turn means:

con.Open("Select * from ShoppingList", con)();

becomes simply:

con.Open();

and similarly with con.Close();.

Arran
  • 24,648
  • 6
  • 68
  • 78