2012-05-12 28 views
0

我收到此錯誤:fomat例外,datagridview的,SQLite的

Error: Format Exception: Input string was not in a correct format. 

foreach (DataRow row in dtChanges.Rows) 
      { 
       using (SQLiteConnection conn = new SQLiteConnection(connString)) 
       { 
        conn.Open(); 
        SQLiteCommand upCmd = new SQLiteCommand(@"update.......",conn); 
      upCmd.Parameters.AddWithValue("@Flux",float.Parse(row["Flux"].ToString())); 

該行[ 「流量」]從datagridview的。它可能是一個「」字符串。我想這是導致問題的原因。流量是數據庫中的數字。我是sqlite數據庫。 我明白這個錯誤,「」不能轉換爲小數。但是,解決這個問題的最好方法是什麼?

Stack Trace: 
     at System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt) 
     at System.Single.Parse(String s) 
     at RVEST.frmVesselData.SaveData(DataGridView dgv) in C:\D_Drive_Stuff\RVESTV2\RVEST\frmVesselData.cs:line 249 
     at RVEST.frmVesselData.btnSave_Click(Object sender, EventArgs e) in C:\D_Drive_Stuff\RVESTV2\RVEST\frmVesselData.cs:line 190 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.Run(Form mainForm) 
     at RVEST.Program.Main() in C:\RVEST\Program.cs:line 26 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 

謝謝宇 孫

+0

你希望等於什麼值?零? –

+0

是的0我會確定。所以嘗試upCmd.Parameters.AddWithValue(「@ Flux」,Convert.ToDecimal(row [「Flux」]));錯誤:對象不能從DBNULL轉換爲其他類型。 – user575219

回答

0

如果你確定了零,嘗試

float result; 
result = row["flux"] != null && float.TryParse(row["flux"].ToString(), out result) ? result :0; 

upCmd.Parameters.AddWithValue("@Flux", result); 

空和空值將被轉換爲0。

,如果你想得到一個空值:

float result; 
    var res= row["flux"] != null && float.TryParse(row["flux"].ToString(), out result) ? (float?)result :null; 

upCmd.Parameters.AddWithValue("@Flux", res); 

無論如何,

您必須檢查: - 如果在調用「ToString()」之前,行[「flux」]爲null。 - 然後看看value.ToString()是否可以解析爲float。

+0

Althaus:我試着upCmd.Parameters.AddWithValue(「@ Flux」,Convert.ToDecimal(row [「Flux」]));錯誤:對象不能從DBNULL轉換爲其他類型。 – user575219

+0

第一個答案完全超出範圍,對不起...我編輯 –

+0

謝謝你@RaphaëlAlthaus – user575219