rai nalasa
rai nalasa

Reputation: 859

SystemInvalidOperationException

I am trying to save this to my datebase but I keep getting this error

System.InvalidOperationException

here is my code.

 protected void btnSend_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd = new SqlCommand(@"INSERT INTO orders2
                            (orderName,orderFile,orderType,orderPrice,orderQuantity,orderShipped)
                            VALUES
                            ('"+DropDownList1.SelectedValue+"','"+lblFile.Text+"','"+lblPrice.Text+"','"+txtQuantity.Text+"','"+DateTime.Now+"')",con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblFinished.Text = "Order has been submitted for process.";

    }

Upvotes: 2

Views: 66

Answers (2)

Alexei - check Codidact
Alexei - check Codidact

Reputation: 23078

WhoAmI is probably right, however your code could be greatly improved to avoid other problems and to also allow you not to allow unhandled exceptions.

I have put extra comments directly in the code:

try
{

    // SqlConnection is disposable, so it is recommended to dispose it (using calls Dispose() for you)
    using (var con = new SqlConnection(connStr))
    { 
        con.Open();

        // this is missing from your code and might the errors actual cause
        // SqlCommand is also disposable
        using (var cmd = con.CreateCommand())
        {
            // is is strongly recommended to construct parameterized commands
            // to avoid SQL injection (check this - https://technet.microsoft.com/en-us/library/ms161953(v=sql.105).aspx)
            cmd.Text = @"
                INSERT INTO orders2
                (orderName,orderFile,orderType,orderPrice,orderQuantity,orderShipped)
                VALUES (@orderName, @orderFile, @orderType, @orderPrice, @orderQuantity, @orderShipped)";

            // the parameters - SqlCommand infers parameter type for you
            cmd.AddWithValue("@orderName", DropDownList1.SelectedValue);
            cmd.AddWithValue("@orderFile", lblFile.Text);
            cmd.AddWithValue("@orderType", theMissingParametersForOrderType);
            // some conversion might be needed here, as I expect the price to be some number 
            // with a fixed number of decimals
            // e.g. Convert.ToDecimal(lblPrice.Text)
            cmd.AddWithValue("@orderPrice", lblPrice.Text);
            // same convertion issue as for price
            cmd.AddWithValue("@orderQuantity", txtQuantity.Text);
            cmd.AddWithValue("@orderShipped", DateTime.Now);
        }  
    }
}  
// there are several Exceptions that can be raised and treated separately
// but this at least you can do
catch (Exception exc)
{
    // log the error somewhere
    // put a breakpoint just below to inspect the full error details
}
// this is executed even if an exception has occurred
finally
{
    if (con != null && con.State != ConnectionState.Closed)
        con.Close();
}

As a side note, this code belongs to a data layer, no presentation layer. Consider including it within another assembly.

Upvotes: 2

WhoAmI
WhoAmI

Reputation: 501

You are inserting 6 values(orderName,orderFile,orderType,orderPrice,orderQuantity,orderShipped) here, but supplied only 5 values. DropDownList1.SelectedValue, lblFile.Text, lblPrice.Text, txtQuantity.Text, DateTime.Now.

Upvotes: 1

Related Questions