Loupi
Loupi

Reputation: 1112

Try/Catch method error ASP.NET

I'm trying to make this work. I want it to check if a record exist after inserting but it always return an error: Line 1: Incorrect syntax near 'nvarchar'. Can someone point out to me whats wrong in my declaration? Also if you have a better try catch method please enlighten me more. Just new to programming in ASP.NET

Thanks in advance.

protected void Page_Load(object sender, EventArgs e)
        {
            string connString_LibrarySystem = "Server=DEVSERVER;User ID=sa;Password=Sup3r-Us3r;Database=LibrarySystem";
            string strSQL = "INSERT INTO TblBooks (bookid, booktitle, lastname, firstname, description, categoryid, dateadded, statusid, quantity, isdeleted) VALUES (@bookid, @booktitle, @lastname, @firstname, @description, @categoryid, @dateadded, @statusid, @quantity, @isdeleted)";

            SqlConnection conn = new SqlConnection(connString_LibrarySystem);

            conn.Open();

            SqlCommand cmd = new SqlCommand();

            cmd = new SqlCommand(strSQL, conn);
            cmd.Parameters.AddWithValue("@bookid", Request.Form["bookid"]);
            cmd.Parameters.AddWithValue("@booktitle", Request.Form["booktitle"]);
            cmd.Parameters.AddWithValue("@lastname", Request.Form["lastname"]);
            cmd.Parameters.AddWithValue("@firstname", Request.Form["firstname"]);
            cmd.Parameters.AddWithValue("@description", Request.Form["description"]);
            cmd.Parameters.AddWithValue("@categoryid", Request.Form["categoryid"]);
            cmd.Parameters.AddWithValue("@dateadded", Request.Form["dateadded"]);
            cmd.Parameters.AddWithValue("@statusid", Request.Form["statusid"]);
            cmd.Parameters.AddWithValue("@quantity", Request.Form["quantity"]);
            cmd.Parameters.AddWithValue("@isdeleted", Request.Form["isdeleted"]);

            cmd.ExecuteNonQuery();
            {
                conn.Close();
            }
            statuslabel.Text = "Insert successful";
        }

EDIT: There just removed the datatypes.

Upvotes: 3

Views: 637

Answers (5)

Deltax76
Deltax76

Reputation: 14263

At first, remove your types in the SQL, you do not need them (As other answers suggested)

Second, you add a parameter to the query by this :

cmd.Parameters.AddWithValue("@bookid", Request.Form["bookid"]);

You do not make sure that Request.Form["bookid"] is not null, this will cause your current problem

Upvotes: 2

Avitus
Avitus

Reputation: 15958

In order to make this work you need to remove the datatypes from the sqlstring variable.

I would probably switch to using a stored procedure and then load parameters since that's basically what you're doing here with the addwithvalue command

also cmd.ExecuteNonQuery() will return an int to tell you that it's been added successfully. if it returns a 1 you know that's it's complete. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

Upvotes: 2

stephenbayer
stephenbayer

Reputation: 12431

don't put the types in your values.

string strSQL = "INSERT INTO TblBooks (bookid, booktitle, lastname, firstname, description, categoryid, dateadded, statusid, quantity, isdeleted) VALUES (@bookid , @booktitle , @lastname , @firstname , @description , @categoryid , @dateadded , @statusid , @quantity , @isdeleted )";

Upvotes: 2

Code Maverick
Code Maverick

Reputation: 20415

Get rid of the data types in the values list. You don't need them.

Upvotes: 1

Bala R
Bala R

Reputation: 108947

You don't have to include data type in insert statements. Skip them.

Try

string strSQL = "INSERT INTO TblBooks (bookid, booktitle, lastname, firstname, description, categoryid, dateadded, statusid, quantity, isdeleted) VALUES (@bookid, @booktitle , @lastname, @firstname, @description, @categoryid, @dateadded , @statusid , @quantity, @isdeleted)";

Upvotes: 5

Related Questions