Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 855
Loupi Avatar asked Mar 20 '26 16:03

Loupi


2 Answers

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)";
like image 133
Bala R Avatar answered Mar 23 '26 06:03

Bala R


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 )";
like image 40
stephenbayer Avatar answered Mar 23 '26 06:03

stephenbayer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!