Data(Category) cant be inserted

I have a DAL folder which has a Category.cs and i used the insert like this
  public int CategoryInsert()
        {
            StringBuilder sql;
            SqlCommand sqlCmd;
            int result;

            result = 0;
            sql = new StringBuilder();
            sql.AppendLine("INSERT INTO tblCategories (CatID, CatName)");
            sql.AppendLine(" ");
            sql.AppendLine("values (@CatName)");
            SqlConnection conn = dbConn.GetConnection();
            try
            {
                sqlCmd = new SqlCommand(sql.ToString(), conn);
                sqlCmd.Parameters.AddWithValue("@CatName", this.CatName);
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            finally
            {
                conn.Close();
            }

            return result;
        }

Open in new window

The code behind for my addCategory.aspx page is this
   protected void btnAdd_Click(object sender, EventArgs e)
        {
            Category eCategory = new Category(txtCatName.Text);

            if (eCategory.CategoryInsert() == 0)
            {
                Response.Write("<script>alert('Insert successful');</script>");
            }
            else
            {
                Response.Write("<script>alert('ID exists.Insert NOT successful.');</script>");
            }
        }

Open in new window

when i click add when i run, it says Insert successful but when i check in the database nothing is being inserted!!! I set the CatID to identity(1,1)
JayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
what if try add:

sqlCmd.ExecuteNonQuery();

Open in new window


after:

sqlCmd.Parameters.AddWithValue("@CatName", this.CatName);

Open in new window


?
0
JayAuthor Commented:
I've tried that but it still doesnt work!! thanks for your help though
0
Ryan ChongCommented:
>> sql.AppendLine("INSERT INTO tblCategories (CatID, CatName)");
            sql.AppendLine(" ");
            sql.AppendLine("values (@CatName)");

you're specify two columns (CatID, CatName) in your insert statement , while you only assigned one value into it.

what's the value of CatID to be assigned with?

if CatID is an autonumber field, just use this instead:

sql.AppendLine("INSERT INTO tblCategories (CatName)");
            sql.AppendLine("values (@CatName)");
...
sqlCmd.ExecuteNonQuery();

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

JayAuthor Commented:
public int CategoryInsert()
        {
            StringBuilder sql;
            SqlCommand sqlCmd;
            int result;

            result = 0;
            sql = new StringBuilder();
            sql.AppendLine("INSERT INTO tblCategories (CatName)");
            sql.AppendLine(" ");
            sql.AppendLine("values (@CatName)");
            SqlConnection conn = dbConn.GetConnection();
            conn.Open();
            try
            {
                sqlCmd = new SqlCommand(sql.ToString(), conn);
                sqlCmd.Parameters.AddWithValue("@CatName", this.CatName);
                sqlCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            finally
            {
                conn.Close();
            }

            return result;
        }

solved by ryan!!!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongCommented:
yes, as per Live session, we do need to open the Connection as well.

 conn.Open();

Open in new window

0
JayAuthor Commented:
Solved by ryan :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.