SQL Exception ( declaring a variable )

This is a discussion on SQL Exception ( declaring a variable ) within the C# Programming forums, part of the General Programming Boards category; hi guys , i am doing a add book form of an admin panel in library automation at ASP.NET (C#). ...

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

    Unhappy SQL Exception ( declaring a variable )

    hi guys , i am doing a add book form of an admin panel in library automation at ASP.NET (C#).
    I have a problem at declaring a variable , when i built the project there is no error or warning but when i run and clicked the add button the error comes.
    the code is below

    Code:
    protected void btnInsert_Click(object sender, EventArgs e)
        {
            SqlConnection con = dbConnection.GetConnection();
            SqlCommand command = new SqlCommand("select b_name from books where b_name=@name", con);
            command.Parameters.AddWithValue("@name", txtBook.Text);
            con.Open();
            SqlDataReader rdr = command.ExecuteReader();
            if (rdr.Read())
            {
                ltrlInf.Text = "";
                ltrlInf.Text += "This book has been added before";
            }
            else
            {
                rdr.Close();
    
                SqlCommand cmd2 = new SqlCommand("select c_id from categories where c_name=@catname", con);
                cmd2.Parameters.AddWithValue("@catname", ddlCat.SelectedValue.ToString());
                SqlDataReader rdr2 = cmd2.ExecuteReader();
                
                while (rdr2.Read())
                {
                    i = Int32.Parse(rdr2.GetValue(0).ToString());
                }
                
                SqlCommand cmd = new SqlCommand("insert into books (b_name,b_author,b_lang,b_page,c_id,b_stock,b_year,b_thumbnail,s_id) VALUES (@name,@author,@lang,@page,@cid@,@stock,@year,@thumbnail,@sid)", con);
                cmd.Parameters.AddWithValue("@name", txtBook.Text);
                cmd.Parameters.AddWithValue("@author", txtAuthor.Text);
                cmd.Parameters.AddWithValue("@lang", txtLang.Text);
                cmd.Parameters.AddWithValue("@page", txtPage.Text);
                cmd.Parameters.AddWithValue("@cid",i);
                cmd.Parameters.AddWithValue("@stock", txtStock.Text);
                cmd.Parameters.AddWithValue("@year", txtYear.Text);
                cmd.Parameters.AddWithValue("@thumbnail", ".\\" + "thumbs" + "\\" + FileUpload1.FileName);
                cmd.Parameters.AddWithValue("@sid", ddlScat.SelectedValue.ToString());
                rdr2.Close();
                cmd.ExecuteNonQuery(); //ERROR COMES HERE ( Must declare the scalar variable "@cid@". )           
                ltrlInf.Text += "The book has been added";
                con.Close();
            }
        }
    i am waiting your answers...

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Havent run your code but dont you thing that "@cid@" is different from "@cid"?

    Look at your sql

  3. #3
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71
    upsss i am sleepy and tired.
    Thanks a lot Fordy

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    10
    wow dats hard to me (((blurrr)))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. life cycle of exception object
    By George2 in forum C++ Programming
    Replies: 43
    Last Post: 02-13-2008, 07:50 AM
  2. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 01:12 PM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  4. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 07:23 PM
  5. Beginner question
    By Tride in forum C Programming
    Replies: 30
    Last Post: 05-24-2003, 09:36 AM

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