C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-20-2009, 02:43 AM   #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...
Aga^^ is offline   Reply With Quote
Old 07-20-2009, 02:48 AM   #2
&TH of undefined behavior
 
Fordy's Avatar
 
Join Date: Aug 2001
Posts: 5,261
Havent run your code but dont you thing that "@cid@" is different from "@cid"?

Look at your sql
__________________
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut."
Albert Einstein (1879 - 1955)


Board Rules
Fordy is offline   Reply With Quote
Old 07-20-2009, 02:58 AM   #3
Registered User
 
Aga^^'s Avatar
 
Join Date: Aug 2008
Posts: 71
upsss i am sleepy and tired.
Thanks a lot Fordy
Aga^^ is offline   Reply With Quote
Old 07-21-2009, 03:49 AM   #4
Registered User
 
Join Date: Jul 2009
Posts: 10
wow dats hard to me (((blurrr)))
zeetaQ53 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
life cycle of exception object George2 C++ Programming 43 02-13-2008 07:50 AM
Problem with the exception class in MINGW indigo0086 C++ Programming 6 01-20-2007 01:12 PM
Declaring an variable number of variables Decrypt C++ Programming 8 02-27-2005 04:46 PM
float/double variable storage and precision cjschw C++ Programming 4 07-28-2003 06:23 PM
Beginner question Tride C Programming 30 05-24-2003 08:36 AM


All times are GMT -6. The time now is 12:04 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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