0

I'm making some book management system using WinForm in C#.

i think there is no error insert query(i cant find what's problem).. but i keep doing process, there is no data in DB. :(

i maked insertForm.cs and database.cs. please help me..

this is database.cs below:

public void GetBookInsert(BookEntity be)
        {   
            ConnectDB(); 
            try
            {
                cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "INSERT INTO dbo.book (bookName,author,company,price,bookImage,classId,bookPlace)"
                                  + " VALUES (@bookName,@author,@company,@price,@bookImage,@classId,@bookPlace)";
                cmd.Parameters.Add(new SqlParameter("@bookName", SqlDbType.NVarChar, -1,be.BookName));
                cmd.Parameters.Add("@author", SqlDbType.NVarChar, -1,be.Author);
                cmd.Parameters.Add("@company", SqlDbType.NVarChar, -1,be.Company);
                cmd.Parameters.Add("@price", SqlDbType.Int,be.Price);
                cmd.Parameters.Add("@bookImage", SqlDbType.NVarChar, -1,be.bookImage);
                cmd.Parameters.Add("@classId", SqlDbType.NVarChar, -1,be.ClassId);
               // cmd.Parameters.Add(new SqlParameter("@bookDate", System.Data.SqlDbType.Date));
                //cmd.Parameters.Add(new SqlParameter("@buyDate", System.Data.SqlDbType.Date));
                cmd.Parameters.Add("@bookPlace", SqlDbType.NVarChar, -1,be.bookPlace);
                cmd.ExecuteNonQuery();
            }
            catch (SqlException e) {
                string msg = "Insert error";
                msg += e.Message;
            }
            finally
            {
                CloseDB();
            }

and insertForm.cs part code is:

private void buConfirm_Click(object sender, EventArgs e)
        {
                Con_Database bookDB = new Con_Database();

                BookEntity books = new BookEntity();
                books.BookName = textName.Text;
                books.Author = textAuthor.Text;
                books.Company = textCompany.Text;
                books.Price = Int32.Parse(textPrice.Text);
                books.bookImage = textImage.Text; 
                books.ClassId = comboClass.Text; 
               // books.BookDate = bookDate.Text; 
               // books.BuyDate = buyDate.Text; 
                bookDB.GetBookInsert(books);
             }
        }
3
  • you should print out the full query and test it manually in your database Commented Dec 4, 2014 at 10:42
  • And why do you supposedly know it does not insert? Hint: you likely check on the wrong database. Commented Dec 4, 2014 at 10:42
  • i tested using mssql server2012... it dosen't work my code.. Commented Dec 4, 2014 at 11:18

1 Answer 1

1

I think you are using SqlParameterCollection.Add Method (String, SqlDbType, Int32, String) overload in a wrong way.

The last parameter doesn't take the value of your parameter, it takes the source column (SourceColumn) if this SqlParameter is used in a call to Update. Because of that, your parameters doesn't have any values. And using -1 as a column length is meaningless.

Just use them like;

cmd.Parameters.Add("@author", SqlDbType.NVarChar).Value = be.Author;

Also use using statement to dispose your database connections and objects.

Sign up to request clarification or add additional context in comments.

1 Comment

@Hyeon What do you mean by doesn't work? You get any exception or error message? Are you sure your parameter values are valid for column types? Are you sure your connection is right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.