2012-05-12 4 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에서입니다. 문자열 일 수 있습니다. 이것이 문제의 원인이라고 생각합니다. Flux는 DB의 숫자입니다. 나는 sqlite DB입니다. 이 오류는 소수로 변환 할 수 없다는 것을 이해합니다. 하지만이 문제를 해결하는 가장 좋은 방법은 무엇일까요?

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으로 확인 있다면하는

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

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

널 빈 값이 0

당신이 경우 싶어으로 변환됩니다 시도 유에게 일

+0

당신은 어떤 가치를 기대합니까? 제로? –

+0

예 0으로 추측됩니다. 그래서 upCmd.Parameters.AddWithValue ("@ Flux", Convert.ToDecimal (row [ "Flux"])); 오류 : 개체를 DBNull에서 다른 유형으로 캐스팅 할 수 없습니다. – user575219

답변

0

감사 null 값 가져 오기 :

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()이 부동으로 해석 될 수 있는지 확인합니다.

+0

Althaus : upCmd.Parameters.AddWithValue ("@ Flux", Convert.ToDecimal (row [ "Flux"])); 오류 : DBNull에서 다른 유형으로 객체를 형변환 할 수 없습니다. – user575219

+0

첫 번째 답변은 완전히 범위를 벗어났습니다. 죄송합니다 ... 편집했습니다. –

+0

감사합니다. @ Raphaël Althaus – user575219