C# fomat异常,datagridview,sqlite

C# fomat异常,datagridview,sqlite,c#,C#,我得到这个错误: Error: Format Exception: Input string was not in a correct format. 在 此行[“通量”]来自datagridview。它可能是一个“”字符串。我想这就是造成问题的原因。通量是数据库中的一个数字。我是sqlite DB。 我理解此错误,即“”不能转换为十进制。但解决这一问题的最佳方法是什么 Stack Trace: at System.Number.ParseSingle(String value

我得到这个错误:

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

此行[“通量”]来自datagridview。它可能是一个“”字符串。我想这就是造成问题的原因。通量是数据库中的一个数字。我是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()
谢谢你
Sun

如果你对零表示满意,请尝试

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。

您希望“”等于什么值?零?是的,我想零是可以的。所以尝试了upCmd.Parameters.AddWithValue(“@Flux”,Convert.ToDecimal(第[“Flux”]);错误:无法将对象从DBNull强制转换为其他类型。Althaus:我尝试了upCmd.Parameters.AddWithValue(“@Flux”,Convert.ToDecimal(行[“Flux”]);错误:无法将对象从DBNull强制转换为其他类型。
float result;
    var res= row["flux"] != null && float.TryParse(row["flux"].ToString(), out result) ? (float?)result :null;

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