Solved

error with sqlparameter array C#  (Visual studio 2.0)

Posted on 2006-11-21
3
928 Views
Last Modified: 2010-08-05
I have this function and on the line

SqlParameter[3] smyparams;

I keep getting ";expected" error. I am not sure if this is the right
way to declare a sqlparameter array.

Pls assist.



public  int myInsert(Customer c)
    {
        string sql;
        sql = "insert into customers(customerid,companyname,contactname,country) values(@custid,@company,@contact,@country)";
        SqlParameter[3] smyparams;
       
        smyparams[0] = new SqlParameter("@custid", c.CustomerID);
        smyparams[1] = new SqlParameter("@company", c.CompanyName);
        smyparams[2] = new SqlParameter("@contact", c.ContactName);
        smyparams[3] = new SqlParameter("@country", c.Country);

        SqlHelper sh = new SqlHelper();
        return sh.ExecuteNonQuery(sql, myparams);
        //return SqlHelper.ExecuteNonQuery(sql, myparams);
       
    }
0
Comment
Question by:Sirdots
3 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 17989884
Try changing

SqlParameter[3] smyparams;
to
SqlParameter[] smyparams = new SqlParameter[4];
0
 
LVL 28

Expert Comment

by:strickdd
ID: 17989943
You could try using a command object, something like this:

string SQL = "insert into customers(customerid,companyname,contactname,country) values(@custid,@company,@contact,@country)";
SqlConnection Connection = new SqlConnection(ConnectionStrings["ConString"]));
            SqlCommand Command = new SqlCommand(SQL, Connection);
            Command.CommandType = CommandType.Text;

            Command.Parameters.AddWithValue("@custid", c.CustomerID);
            Command.Parameters.AddWithValue("@company", c.CompanyName);
            Command.Parameters.AddWithValue("@contact", c.ContactName);
            Command.Parameters.AddWithValue("@country", c.Country);

            Connection.Open();

            Command.ExecuteNonQuery();

            Connection.Close();
0
 

Author Comment

by:Sirdots
ID: 17989949

Thanks Nightman. This works.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question