2016-04-11 4 views
2

Ich versuche, diese zu meiner Datebase zu retten, aber ich halte diesen FehlerSystemInvalidOperationException

System.InvalidOperationException 

hier mein 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."; 

    } 
+0

Wenn Sie einen Breakpoint auf 'cmd.ExecuteNonQuery()' setzen und 'cmd.CommandText' betrachten, sehen Sie etwas falsch? Können Sie diesen Wert verwenden und die Abfrage in Ihrer Datenbank ausführen? – ConnorsFan

+0

@connors cmd.CommandText? Ich habe das noch nicht ausprobiert. –

+1

Nicht verwandt mit Ihrer Frage, aber etwas, das Sie ansprechen müssen: SQL-Injektion Möglichkeit –

Antwort

2

WhoAmI ist wahrscheinlich richtig, jedoch könnte Ihr Code stark verbessert werden, um andere Probleme zu vermeiden und Ihnen auch erlauben, nicht behandelte Ausnahmen zu erlauben.

Ich habe setzen zusätzliche Kommentare direkt im 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(); 
} 

Als Randbemerkung, gehört dieser Code auf eine Datenschicht, keine Präsentationsschicht. Erwägen Sie, es in eine andere Baugruppe aufzunehmen.

+0

Danke, habe ich über die sql Injektion thingy.thanks für die Tipps Mann vergessen! –

1

Sie einfügen 6 Werte (orderName,orderFile,orderType,orderPrice,orderQuantity,orderShipped) hier, aber nur 5 Werte geliefert. DropDownList1.SelectedValue, lblFile.Text, lblPrice.Text, txtQuantity.Text, DateTime.Now.