Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I got this EROR:Must declare the scalar variable "@".
my program is 3 layer.
plz help me
this is dataAccess layer
C#
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:
C#
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:
C#
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
Comments
Richard C Bishop 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.

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:
C#
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
 
Share this answer
 
Comments
Jibesh 11-Jan-13 11:41am    
Yes.. and You got it right. Good Catch Mika. +5
Wendelius 11-Jan-13 11:42am    
Thanks :)
Maciej Los 11-Jan-13 13:00pm    
Good answer, my 5!
Wendelius 11-Jan-13 13:20pm    
Thanks Maciej :)
f,amiri882 11-Jan-13 13:17pm    
thanks alot!
"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
 
Share this answer
 
Comments
Maciej Los 11-Jan-13 13:01pm    
Good answer, my 5!
@ nametype @ nameUnit
Maybe remove the SPACEs separating the @ from the name?
 
Share this answer
 
Comments
f,amiri882 11-Jan-13 11:05am    
I cleared space,but it still has this EROR
David_Wimbley 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".
David_Wimbley 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!
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900