5 Replies - 2862 Views - Last Post: 24 September 2010 - 04:27 AM Rate Topic: -----

#1 pwtc222  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 72
  • Joined: 02-July 10

No definition for parameter error

Posted 16 September 2010 - 08:40 PM

Hi.
I have a code where it will store values from textbox to gridview temporarily without sending to database.
Now i have created a button,trying to send the values in temporary gridview to database.
Here's the cs code :
 protected void BtnSendToDatabase_Click(object sender, EventArgs e)
        {
            foreach (DataRow row in dt.Rows)
            {
                Console.WriteLine(row["name"].ToString());
            }

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "INSERT INTO Table1 (Company_Name) VALUES (@compName)";
            cmd.Parameters.AddWithValue("@compName", TxtName.Text);
            
            foreach (DataRow row in dt.Rows)
            {
                cmd.Parameter.AddWithValue("@compName", row["name"].ToString());
                cmd.ExecuteNonQuery();
            }

            cmd.Dispose();
            conn.Close();
            conn.Dispose();
        }



But i'm getting this error:
CS1061: 'System.Data.SqlClient.SqlCommand' does not contain a definition for 'Parameter' and no extension method 'Parameter' accepting a first argument of type 'System.Data.SqlClient.SqlCommand' could be found (are you missing a using directive or an assembly reference?)
Source error:
cmd.Parameter.AddWithValue("@compName", row["name"].ToString());
 


I tried to use command instead of cmd,but it still gives error.

Any ideas guys?

This post has been edited by eclipsed4utoo: 17 September 2010 - 04:40 AM
Reason for edit:: please use the code tags... [code] your code here [/code]


Is This A Good Question/Topic? 0
  • +

Replies To: No definition for parameter error

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: No definition for parameter error

Posted 17 September 2010 - 04:42 AM

This is a basic part of debugging. Here are two lines of code. The first one works...

cmd.Parameters.AddWithValue("@compName", TxtName.Text);



the second one does not..

cmd.Parameter.AddWithValue("@compName", row["name"].ToString());



Tell me why you are being told that "Parameter" is not a property/method of the SqlCommand class.
Was This Post Helpful? 1
  • +
  • -

#3 pwtc222  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 72
  • Joined: 02-July 10

Re: No definition for parameter error

Posted 17 September 2010 - 05:53 PM

Oh,i got it now.
I missed the 's' in Parameters
which is suppose to be like this :

cmd.Parameters.AddWithValue("@compName", row["name"].ToString());



NOT like this
cmd.Parameter.AddWithValue("@compName", row["name"].ToString());



But now,i'm getting error on the sql connection :
Error-ExecuteNonQuery: Connection property has not been initialized.
On line
cmd.ExecuteNonQuery();


I think the code is just ok,do you find anything wrong in the code?

This post has been edited by pwtc222: 17 September 2010 - 05:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: No definition for parameter error

Posted 18 September 2010 - 06:01 AM

Yes, there is something wrong. Where is your SqlConnection object? How does the SqlCommand know where to go to connect to the database? It uses a SqlConnection object...which you don't have.
Was This Post Helpful? 0
  • +
  • -

#5 pwtc222  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 72
  • Joined: 02-July 10

Re: No definition for parameter error

Posted 23 September 2010 - 05:58 PM

what do you mean by SqlConnection object?..I dont get you
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: No definition for parameter error

Posted 24 September 2010 - 04:27 AM

View Postpwtc222, on 23 September 2010 - 08:58 PM, said:

what do you mean by SqlConnection object?..I dont get you


you need to have an object of the SqlConnection type when doing database operations.

Try this..

protected void BtnSendToDatabase_Click(object sender, EventArgs e)
       {
           foreach (DataRow row in dt.Rows)
           {
               Console.WriteLine(row["name"].ToString());
           }

           // I changed this line.
           SqlCommand cmd = conn.CreateCommand();
           cmd.CommandText = "INSERT INTO Table1 (Company_Name) VALUES (@compName)";
           cmd.Parameters.AddWithValue("@compName", TxtName.Text);
           
           foreach (DataRow row in dt.Rows)
           {
               cmd.Parameter.AddWithValue("@compName", row["name"].ToString());
               cmd.ExecuteNonQuery();
           }

           cmd.Dispose();
           conn.Close();
           conn.Dispose();
       }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1