输入字符串格式不正确,已尝试但无法解析
本文关键字:格式 字符串 不正确 输入 | 更新日期: 2023-09-27 18:15:39
string fbcTypeNameDate = "", fbcTypeNameID = "",Billfor="";
int TotalQuantity=0,BillId=0;
DateTime BillDate;
string fbrc_FabricID="",fbrc_FabricQuantity="",fbrc_FabricDetail="";
string SupplierName = "", InvoiceNo = "", BillDescription = "";
decimal TotalRate = 0,fbrc_FabricRate=0;
string fbrc_getfabricID = "";
int count = 0, index = 0, counter = 0;
public void fillcomboboxinDataGridView()
{
dt = new DataTable();
cmd = con.CreateCommand();
cmd.CommandText = "Select FabricTypeName,FabricID From [tbl_FabricTypeName] ";
ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
dgv_FabricTypeName.DataSource = dt;
dgv_FabricTypeName.ValueMember = "FabricID";
dgv_FabricTypeName.DisplayMember = "FabricTypeName";
}
public void fbcTypeName_fillcomboboxBySupplierName()
{
dt = new DataTable();
cmd = con.CreateCommand();
cmd.CommandText = "SELECT SupplierName,SupplierID FROM tbl_SupplierDetails";
ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
cmbox_PurchaseFabric_SupplierName.DataSource = dt;
cmbox_PurchaseFabric_SupplierName.ValueMember = "SupplierID";
cmbox_PurchaseFabric_SupplierName.DisplayMember = "SupplierName";
}
public void getFabricId()
{
cmd = con.CreateCommand();
cmd.CommandText = "select COUNT(*)as countid from tbl_FabricBuy ";
count= Convert.ToInt32( cmd.ExecuteScalar());
if (count==0)
{
fbcTypeNameDate = dtp_PurchaseFabric_Date.Text;
fbcTypeNameID = fbcTypeNameDate + "/FAB/" + 1;
dataGridView_PurchaseFabric_EnterItemsDetails.Rows[0].Cells[0].Value = fbcTypeNameID;
}
else
{
cmd.CommandText = "select top 1 (FabricID) from tbl_FabricBuy order by FabricNo desc";
dr = cmd.ExecuteReader();
while (dr.Read())
{
fbrc_getfabricID = dr["FabricID"].ToString();
}
dr.Close();
fbcTypeNameDate = dtp_PurchaseFabric_Date.Text;
index = fbrc_getfabricID.IndexOf("FAB");
counter = Convert.ToInt32(fbrc_getfabricID.Substring(index + 4));
counter = counter + 1;
fbcTypeNameID = fbcTypeNameDate + "/FAB/" + counter;
dataGridView_PurchaseFabric_EnterItemsDetails.Rows[0].Cells[0].Value = fbcTypeNameID;
}
}
输入字符串格式不正确,尝试了所有格式都无法解决问题,请给出解决问题的建议
试试这个,索引相对为零
counter = Convert.ToInt32(fbrc_getfabricID.Substring(index + 3));
很简单,你有这样的代码:
counter = Convert.ToInt32(fbrc_getfabricID.Substring(index + 4));
抛出和Incorrect format error
,这意味着fbrc_getfabricID.Substring(index + 4)
不能转换为Int32
。你的索引句柄可能有错误,可能是+3而不是+4。
我建议这样做:
int output;
if(!Int32.TryParse(fbrc_getfabricID.Substring(index + 4), out output))
{
// Handle your error here by applaying default value for example
}
这可能对您有用
index = fbrc_getfabricID.IndexOf("FAB");
counter = Convert.ToInt32(fbrc_getfabricID.Substring(index, index + 4))
"Input string was not ina correct format"只会在从源类型转换为目的类型失败时抛出,这里是从string转换为int。这意味着源文件不包含数字(或包含一些未转换为int的字符)
这里我建议你使用Int32.TryParse
int counter;
if(!Int32.TryParse(fbrc_getfabricID.Substring(index + 4), out counter))
{
//Do your code here, you will get the output to counter if the conversion is successfull
}