2012-05-12 11 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() 

あなたがゼロでOKなら、あなたたい場合

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

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

ヌルと空の値が0

に変換されますしてみてください 日

+0

どのような価値を同等にしたいですか?ゼロ? –

+0

はい0は私がokと思うでしょう。そのため、upCmd.Parameters.AddWithValue( "@ Flux"、Convert.ToDecimal(row ["Flux"]));エラー:オブジェクトをDBNullから他の型にキャストできませんでした。 – user575219

答えて

0

湯をありがとうヌル値を取得:

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

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

いずれの場合も、

チェックする必要があります。 - "ToString()"を呼び出す前にrow ["flux"]がnullの場合。 - 次に、value.ToString()を解析して浮動小数点にすることができるかどうかを確認します。

+0

Althaus:upCmd.Parameters.AddWithValue( "@ Flux"、Convert.ToDecimal(row ["Flux"]));エラー:オブジェクトをDBNullから他の型にキャストできません。 – user575219

+0

最初の答えは全く範囲外でした、申し訳ありません...編集しました –

+0

ありがとう@RaphaëlAlthaus – user575219

関連する問題