Click here to Skip to main content
13,048,846 members (69,031 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
I got this EROR:Must declare the scalar variable "@".
my program is 3 layer.
plz help me
this is dataAccess layer
 
        public int InsertObject(int objectID, string name, string typeObject,int number,string typeUnit)
        {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=anbraProject;Integrated Security=True");
        SqlCommand com = new SqlCommand();
 
        com.CommandType = CommandType.Text;
        com.CommandText = "INSERT INTO Objecttbl(objID,name,typeObj,num,typeUnit)VALUES(@objID,@name,(SELECT typeID FROM typeObjtbl WHERE nameType = @ nametype),@num,(SELECT typeUnitID FROM typeUnit WHERE nameUnit = @ nameUnit))";
        com.Connection = con;
        com.Parameters.AddWithValue("@objID", objectID);
        com.Parameters.AddWithValue("@name", name);
        com.Parameters.AddWithValue("@typeObj", typeObject);
        com.Parameters.AddWithValue("@num", number);
        com.Parameters.AddWithValue("@typeUnit", typeUnit);
 

     
 
        con.Open();
        int Result = com.ExecuteNonQuery();
        con.Close();
 
          return Result;
        }

this is Middle layer:
public int InsertObject(int objectID, string name, string typeObject, int number, string typeUnit)
        {
            DataAccess.DataManager dm = new DataAccess.DataManager();
            return dm.InsertObject(objectID, name, typeObject, number, typeUnit);
        }

and This is Presentation layer:
private void button1_Click(object sender, EventArgs e)
        {
            Middle.Middle m = new Middle.Middle();
            if (m.InsertObject(Int32.Parse(txtIDObj.Text), txtNameObj.Text, cboTypeObj.SelectedValue.ToString(), Int32.Parse(txtNumObj.Text), cbotypeUnit.SelectedValue.ToString()) == 1)
            {
                dataGridView1.DataSource = m.GetObjByName();
                dataGridView1.Refresh();
            }
            
 
        }
Posted 11-Jan-13 4:19am
Comments
richcb 11-Jan-13 10:22am
   
You need to take the spaces out between the "@" and the variable name in your query. It should read: WHERE nameUnit = @nameUnit not WHERE nameUnit = @ nameUnit.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

In overall the parameter names in the SQL statement do not match the ones you use in your program. As already said by others, you cannot have spaces between the name and the @.

So have a try with something like:
public int InsertObject(int objectID, string name, string typeObject,int number,string typeUnit)
        {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=anbraProject;Integrated Security=True");
        SqlCommand com = new SqlCommand();
 
        com.CommandType = CommandType.Text;
        com.CommandText = @"
INSERT INTO Objecttbl(objID, name, typeObj, num, typeUnit) 
VALUES (@objID, 
        @name, 
       (   SELECT typeID 
           FROM typeObjtbl 
           WHERE nameType = @nametype),
       @num,
       (   SELECT typeUnitID 
           FROM typeUnit 
           WHERE nameUnit = @nameUnit)
       )";
        com.Connection = con;
        com.Parameters.AddWithValue("@objID", objectID);
        com.Parameters.AddWithValue("@name", name);
        com.Parameters.AddWithValue("@nametype", ???);
        com.Parameters.AddWithValue("@nameUnit", ???);
 
        con.Open();
        int Result = com.ExecuteNonQuery();
        con.Close();
 
        return Result;
  }

I marked the two parameter values with question marks since I'm not able to say what values you want to use for the parameters. The point is you have to define a value that you want to use in the querying part for tables typeObjtbl and typeUnit
  Permalink  
Comments
jibesh 11-Jan-13 11:41am
   
Yes.. and You got it right. Good Catch Mika. +5
Mika Wendelius 11-Jan-13 11:42am
   
Thanks :)
Maciej Los 11-Jan-13 13:00pm
   
Good answer, my 5!
Mika Wendelius 11-Jan-13 13:20pm
   
Thanks Maciej :)
f,amiri882 11-Jan-13 13:17pm
   
thanks alot!
Mika Wendelius 11-Jan-13 13:19pm
   
You're welcome :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

"INSERT INTO Objecttbl(objID,name,typeObj,num,typeUnit)VALUES(@objID,@name,(SELECT typeID FROM typeObjtbl WHERE nameType = @ nametype),@num,(SELECT typeUnitID FROM typeUnit WHERE nameUnit = @ nameUnit))";


Looks like SPACE is present in between @ & parameter name. Try:
@nametype<br />
@nameUnit
  Permalink  
Comments
Maciej Los 11-Jan-13 13:01pm
   
Good answer, my 5!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

@ nametype @ nameUnit
Maybe remove the SPACEs separating the @ from the name?
  Permalink  
Comments
f,amiri882 11-Jan-13 11:05am
   
I cleared space,but it still has this EROR
Plyswthsqurles 11-Jan-13 11:07am
   
It would help if you specify your error that you are getting, the crystal ball only gets us so far.
f,amiri882 11-Jan-13 11:34am
   
after clear space,I got this eror:
Must declare the scalar variable "@nametype".
Must declare the scalar variable "@nameUnit".
Plyswthsqurles 11-Jan-13 11:39am
   
wow

"You've also got @nametype and @nameUnit which you are not assigning any value to as well."

You need to add

com.Parameters.AddWithValue("@nameType", number);
com.Parameters.AddWithValue("@nameUnit", typeUnit);
Maciej Los 11-Jan-13 13:00pm
   
Good answer, +5!
f,amiri882 11-Jan-13 13:19pm
   
thanks alot,
may problem solved.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Change your query code from this

INSERT INTO Objecttbl(objID,name,typeObj,num,typeUnit)VALUES(@objID,@name,(SELECT typeID FROM typeObjtbl WHERE nameType = @ nametype),@num,(SELECT typeUnitID FROM typeUnit WHERE nameUnit = @ nameUnit))


To this

INSERT INTO Objecttbl(objID,name,typeObj,num,typeUnit)VALUES(@objID,@name,(SELECT typeID FROM typeObjtbl WHERE nameType = @nametype),@num,(SELECT typeUnitID FROM typeUnit WHERE nameUnit = @nameUnit))


You've also got @nametype and @nameUnit which you are not assigning any value to as well.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100