Variable names must be unique within a query batch or stored procedure ERROR

This is a discussion on Variable names must be unique within a query batch or stored procedure ERROR within the C# Programming forums, part of the General Programming Boards category; hi guys i am working on a project with c#-asp.net. I trying to send the same message more than one ...

  1. #1
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71

    Exclamation Variable names must be unique within a query batch or stored procedure ERROR

    hi guys
    i am working on a project with c#-asp.net. I trying to send the same message more than one person. I have a message table on my db and messages are sending and bringing on username. Every user has a username.

    I taking the query and fill the datatable with the student table's columns. Cause i want to use students' username, so the messages will be sent to the students. Then i give the parameters to the sql command.

    Code:
    DataTable dtUn = DBCodes.DataGetir("SELECT * FROM student WHERE proj_taken=0");
            for (int b = 0; b < dtUn.Rows.Count; b++)
            {
                uname = dtUn.Rows[b]["stu_username"].ToString();
                cmdSend.Parameters.Clear(); //if make it comment the error comes but message will be send to the last person on the datatable again
                cmdSend.Parameters.AddWithValue("@receiver", uname);//If write the row here message will send to the last person on the datatable
            }
    
            cmdSend.Parameters.AddWithValue("@sender", Session["adusername"].ToString());
            
            cmdSend.Parameters.AddWithValue("@unopened", 1);
            cmdSend.Parameters.AddWithValue("@opened", 0);
            cmdSend.Parameters.AddWithValue("@context", context);
            cmdSend.Parameters.AddWithValue("@title", title);
            cmdSend.Parameters.AddWithValue("@senderName", Session["adname"].ToString() + " " + Session["adsname"].ToString());
       
            conSend.Open();
            cmdZero.ExecuteNonQuery();
            cmdSend.ExecuteNonQuery();
            conSend.Close();
    the error is The variable name '@receiver' has already been declared. Variable names must be unique within a query batch or stored procedure.
    Must declare the scalar variable "@sender".

    how can i fix the problem

    thanks..

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    You need to run the loop around the whole send cmd.
    Code:
    conSend.Open();
    for (int b = 0; b < dtUn.Rows.Count; b++)
    {
        uname = dtUn.Rows[b]["stu_username"].ToString();
        cmdSend.Parameters.Clear();
        cmdSend.Parameters.AddWithValue("@receiver", uname);
        cmdSend.Parameters.AddWithValue("@sender", Session["adusername"].ToString());
        cmdSend.Parameters.AddWithValue("@unopened", 1);
        cmdSend.Parameters.AddWithValue("@opened", 0);
        cmdSend.Parameters.AddWithValue("@context", context);
        cmdSend.Parameters.AddWithValue("@title", title);
        cmdSend.Parameters.AddWithValue("@senderName", Session["adname"].ToString() + " " + Session["adsname"].ToString());
        cmdSend.ExecuteNonQuery();
    }//for
    
    cmdZero.ExecuteNonQuery();
    conSend.Close();
    Woop?

  3. #3
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71
    prog-bman thanks a lot it worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  5. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21