输入字符串格式不正确,已尝试但无法解析

时间:2015-10-01 07:14:16

标签: c# sql-server

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;
    }

}

输入字符串格式不正确,尝试了所有格式但无法解决问题,因此请提出解决问题的建议。

4 个答案:

答案 0 :(得分:0)

试试这个,索引是零相对

counter = Convert.ToInt32(fbrc_getfabricID.Substring(index + 3));

答案 1 :(得分:0)

然后很简单,你有这个代码:

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
}

答案 2 :(得分:0)

这可能会为你做到这一点

index = fbrc_getfabricID.IndexOf("FAB");
counter = Convert.ToInt32(fbrc_getfabricID.Substring(index, index + 4))

答案 3 :(得分:0)

  

“输入字符串格式不正确”只有在将类型从源类型转换为目标类型时才会抛出,此处它是从字符串到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
}