user3561881
user3561881

Reputation: 23

SQL error with update

I have problem with updating. What's wrong?

Code compiles, but when i click button, the line --> cmd.ExecuteNonQuery(); didn't work.

HERE ERROR

Additional information: The parameterized query '(@nmo int,@rezerwo​ int)UPDATE oferty SET oferty.miejsca = @nmo ' expects the parameter '@rezerwo', which was not supplied.

public partial class oferty1 : System.Web.UI.Page
    SqlConnection cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\ddd\Documents\Visual Studio 2013\Projects\sV1\stopowiczeV1\App_Data\ofety-sV1.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");
    SqlCommand cmd = new SqlCommand();

    protected void Page_Load(object sender, EventArgs e)
cmd.Connection = cn;

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        var g = GridView1.SelectedRow;
        string rezerw = GridView1.DataKeys[g.DataItemIndex]["Idoferty"].ToString();

        m = GridView1.SelectedRow.Cells[5].Text;
        int nmo = Convert.ToInt32(m);
        int rezerwo = Convert.ToInt32(rezerw);
        if (GridView1.SelectedRow.Cells[5].Text != "" & GridView1.SelectedRow.Cells[0].Text != "")
        {
            cn.Open();
            nmo--;
            cmd = cn.CreateCommand();
            cmd.CommandText = ("UPDATE oferty SET oferty.miejsca = @nmo WHERE oferty.idoferty = @rezerwo");
            cmd.Parameters.Add(new SqlParameter("@nmo", nmo));
            cmd.Parameters.Add(new SqlParameter("@rezerwo​", rezerwo));
            string snmo = Convert.ToString(nmo);
            td.miejsca = snmo;
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        db.rezerwacjes.InsertOnSubmit(tc);
        db.SubmitChanges();
    }

Upvotes: 1

Views: 82

Answers (1)

suff trek
suff trek

Reputation: 39777

Constructor of SqlParamter may have interpreted second parameter incorrectly (SqlDbType instead of actual value).

Instead of

cmd.Parameters.Add(new SqlParameter("@nmo", nmo));
cmd.Parameters.Add(new SqlParameter("@rezerwo​", rezerwo));

Try

cmd.Parameters.AddWithValue("@nmo", nmo);
cmd.Parameters.AddWithValue("@rezerwo​", rezerwo);

Another point. By default command type could be stored procedure, try adding this to your code:

cmd.CommandType = CommandType.Text;

Upvotes: 2

Related Questions