C#格式异常

时间:2015-01-13 07:16:17

标签: c# asp.net

我有两个表格ConturiPersoana我想展示Cont的每一个项目(我不能使用ListView)这样做我遇到了一点问题

表格如下:

[Cont]:
        - id (cheie primara)
        - id_persoana (cheie externa in [Persoana])
        - Banca (sir de caractere maxim 64)
        - Sold (decimal)
        - Unitate (moneda, sir de caracatere de lungime 3)
        - Data_deschiderii (data)

我无法确定格式异常的位置,这就是代码:

namespace TarnaLucianFlorinGrupa333
{
    public partial class Conturi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string query = GetQuery();
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            conn.Open();
            SqlCommand cmd = new SqlCommand(query, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            FillTable(reader);
            conn.Close();
        }


        private string GetQuery()
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(TextBox1.Text))
            {
                query = string.Format("select c.id,p.Nume, c.Banca,c.Sold,c.Unitate,c.Data_Deschiderii from Cont c,Persoana p where c.Sold>={0} and p.Id = c.id_persoana", Convert.ToInt32(TextBox1.Text));
            }

            else
            {
                query = "select c.id,p.Nume, c.Banca,c.Sold,c.Unitate,c.Data_Deschiderii from Cont c,Persoana p where p.Id = c.id_persoana";
            }

            return query;
        }

        private void FillTable(SqlDataReader reader)
        {
            TableRow th = Table1.Rows[0];
            Table1.Rows.Clear();
            Table1.Rows.Add(th);

            while (reader.Read())
            {
                TableRow row = new TableRow();

                TableCell nume = new TableCell();
                nume.Text = reader.GetValue(1).ToString();
                row.Cells.Add(nume);

                TableCell Banca = new TableCell();
                Banca.Text = reader.GetValue(2).ToString();
                row.Cells.Add(Banca);

                TableCell Sold = new TableCell();
                Sold.Text = reader.GetValue(3).ToString();
                row.Cells.Add(Sold);

                TableCell Unitate = new TableCell();
                Unitate.Text = reader.GetValue(4).ToString();
                row.Cells.Add(Unitate);

                TableCell Data_Deschiderii = new TableCell();
                Data_Deschiderii.Text = reader.GetValue(5).ToString();
                row.Cells.Add(Data_Deschiderii);

                TableCell Adauga = new TableCell();
                Adauga.Text = string.Format("<a href=\"Adauga.aspx?id={0}\"> Adauga </a>", reader.GetValue(0).ToString());
                row.Cells.Add(Adauga);

                Table1.Rows.Add(row);
            }
        }
        protected void Button2_Click(object sender, EventArgs e) { }


    }
}

这就是观点:

   <table>

    <tr>
        <td> Soldul </td>
        <td> 
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </td>
        <td> 
            &nbsp;</td>
    </tr>
    <tr>

          <td>

              <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Cauta" />

          </td>
    </tr>
    <tr>
        <td colspan="3">
            <asp:Table ID="Table1" runat="server">
                <asp:TableHeaderRow BackColor=Aqua>
                    <asp:TableHeaderCell> nume </asp:TableHeaderCell> 
                    <asp:TableHeaderCell> Banca </asp:TableHeaderCell> 
                    <asp:TableHeaderCell> Sold </asp:TableHeaderCell> 
                    <asp:TableHeaderCell> Unitate </asp:TableHeaderCell>
                    <asp:TableHeaderCell> Data_Deschidere</asp:TableHeaderCell>
                    <asp:TableHeaderCell> Adauga</asp:TableHeaderCell>  
                </asp:TableHeaderRow>
            </asp:Table>
        </td>


    </tr>

 </table>

它引发我的错误是:

  

mscorlib.dll中发生了'System.FormatException'类型的异常,但未在用户代码中处理

Additional information: Input string was not in a correct format.

有人能指出我这个错误的来源吗?

4 个答案:

答案 0 :(得分:1)

可能是你必须

 if (!string.IsNullOrEmpty(TextBox1.Text))
            {
                query = string.Format("select c.id,p.Nume, c.Banca,c.Sold,c.Unitate,c.Data_Deschiderii from Cont c,Persoana p where c.Sold>={0} and p.Id = c.id_persoana", Convert.ToInt32(TextBox1.Text));
            }

似乎你试图在文本框为空时转换为int

答案 1 :(得分:1)

string.IsNullOrEmtpy()切换为int.TryParse(string, out int),如果已解析该值,则返回true。 (或者只是坚持使用string.IsNullOrEmpty(),如果你不想要/需要解析)

    private string GetQuery()
    {
        string query = string.Empty;
        int value = -1;
        if (int.TryParse(TextBox1.Text, out value))
        {
            query = string.Format("select c.id,p.Nume, c.Banca,c.Sold,c.Unitate,c.Data_Deschiderii from Cont c,Persoana p where c.Sold>={0} and p.Id = c.id_persoana", value);
        }

        else
        {
            query = "select c.id,p.Nume, c.Banca,c.Sold,c.Unitate,c.Data_Deschiderii from Cont c,Persoana p where p.Id = c.id_persoana";
        }

        return query;
    }

作为旁注,您应该查看参数化查询,而不是使用string.Format()甚至手动字符串连接。

参数化版本:

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        SqlCommand cmd = GetQuery();
        cmd.Connection = conn;
        cmd.Connection.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        FillTable(reader);
        cmd.Connection.Close();
    }

    private SqlCommand GetQuery()
    {
        SqlCommand cmd = new SqlCommand();

        int value = -1;
        if (int.TryParse(TextBox1.Text, out value))
        {
            cmd.CommandText = "select c.id,p.Nume, c.Banca,c.Sold,c.Unitate,c.Data_Deschiderii from Cont c,Persoana p where c.Sold>=@Sold and p.Id = c.id_persoana";
            cmd.Parameters.Add(new SqlParameter("@Sold", value));
        }

        else
        {
            cmd.CommandText = "select c.id,p.Nume, c.Banca,c.Sold,c.Unitate,c.Data_Deschiderii from Cont c,Persoana p where p.Id = c.id_persoana";
        }

        return cmd;
    }

答案 2 :(得分:0)

使用TryParse代替此行。如果文本框中的文字不是数字,您将获得FormatException

Convert.ToInt32(TextBox1.Text)

答案 3 :(得分:0)

  

您对文本框进行了错误的检查。你正在检查是否   TextBox1的值为NULL或空而不是将其转换为int32所以如何   null或空值可以转换为int32。

还要确保在此文本框中写入整数值。像这样改变

if (!string.IsNullOrEmpty(TextBox1.Text))
{
query = string.Format("select c.id,p.Nume, c.Banca,c.Sold,c.Unitate,c.Data_Deschiderii from Cont c,Persoana p where c.Sold>={0} and p.Id = c.id_persoana", Convert.ToInt32(TextBox1.Text));

}
else
 {
query = "select c.id,p.Nume, c.Banca,c.Sold,c.Unitate,c.Data_Deschiderii from Cont c,Persoana p where p.Id = c.id_persoana";                            
}