Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The code displays such an error:
System.Data.SqlClient.SqlException: „Incorrect syntax near '-'.”

The purpose of the code is to insert a numerical data entered by the user into the textbox (into column "ilosc") if there is no such column "odmiany" in the database, or to add the numerical value in the textbox to the number already in the database in the "ilosc" column.

my code:

sql = "select count(*) from zapasy_2020-21  where login='" + Context.User.Identity.Name + "' and odmiana='" + odmiany.SelectedValue + "'";
                  SqlCommand cmd = new SqlCommand(sql, con);

                  con.Open();
                  ros = (int)cmd.ExecuteScalar();

                  con.Close();

                  if (ros > 0)
                  {
                      sql = "select ilosc from zapasy_2020-21  where login='" + Context.User.Identity.Name + "' and odmiana='" + odmiany.SelectedValue + "'";
                      SqlCommand cmq = new SqlCommand(sql, con);
                      con.Open();
                      int ilo = (int)cmq.ExecuteScalar();
                      con.Close();

                      sql = "insert into zapasy_2020-21 values (@ilosc) where login='" + Context.User.Identity.Name + "' and odmiana='" + odmiany.SelectedValue + "'";
                      con.Open();
                      SqlCommand cma = new SqlCommand(sql, con);
                      string y = TextBox1.Text;
                      int z;
                      z = int.Parse(y);

                      SqlParameter paramName = new SqlParameter();
                      paramName.ParameterName = "@ilosc";
                      paramName.Value = ilo + z;

                      cma.Parameters.AddWithValue("ilosc", paramName);


                      int i = cma.ExecuteNonQuery();
                      con.Close(); // connection close

                  }
                  else
                  {




                      sql = "insert into zapasy_2020-21 values (@login,@odmiana,@ilosc)";
                      con.Open();
                      SqlCommand cmk = new SqlCommand(sql, con);
                      string y = TextBox1.Text;
                      int z;
                      z = int.Parse(y);
                      cmk.Parameters.AddWithValue("login", Context.User.Identity.Name);
                      cmk.Parameters.AddWithValue("odmiana", odmiany.SelectedValue);
                      cmk.Parameters.AddWithValue("ilosc", z);

                      int i = cmk.ExecuteNonQuery();
                      con.Close(); // connection close


                  }


What I have tried:

i tried to change sql queries, but i don't know which member is invalid
Posted
Updated 4-Dec-20 10:32am
Comments
CHill60 7-Dec-20 8:37am    
Did you get this sorted out?

1 solution

You need to switch to parameterized queries for several reasons. Most importantly to avoid sql injection issues. Right now your code could easily be hacked and your database compromised. Secondly, you'll avoid the syntax issues.

Something like:
C#
using (SqlConnection sqlCon = new SqlConnection("connection string"){
  sqlCon.Open();
  String sql = "select count(*) from zapasy_2020-21  where login= @username and odmiana= @odm";
  using (SqlCommand cmd = new SqlCommand(sql, sqlCon){
      cmd.Parameters.AddWithValue("@username", Context.User.Identity.Name);
      cmd.Parameters.AddWithValue("@odm", odmiany.SelectedValue);
      ...
   }
 }
 
Share this answer
 
Comments
Łukasz Szeszycki 4-Dec-20 16:10pm    
when I use this code, shows me an error "an unassigned variable was used" (cmd)
BabyYoda 4-Dec-20 16:23pm    
I'd have to see the rest of your code. But basically, you just need to fix something. What I gave is not complete, you need to make it work in your code.
Łukasz Szeszycki 4-Dec-20 16:29pm    
Yes, i know, but when i write SqlCommand with using , visual shows me a problem with cmd that "an unassigned variable was used". I could send you all my code :)
BabyYoda 4-Dec-20 16:46pm    
No, just post the relevant part.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900