Solved

error with sqlparameter array C#  (Visual studio 2.0)

Posted on 2006-11-21
3
931 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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