2015-01-13 90 views
0

我有2個表ConturiPersoana,我想要顯示每個單一的項目,我有(我不能使用ListView),這樣做時遇到一個小問題。C#格式異常

表看起來是這樣的:

[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> 

它給我的錯誤是:

型「System.FormatException」的異常出現在mscorlib.dll,但在用戶代碼中沒有處理

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

有人能指出我在哪裏,這是錯誤的?

+0

在哪條線路到底如何?在Convert.ToInt32(TextBox1.Text)'? –

+0

這是它告訴我的一切,我知道我......然後我可能會修復它... :(在排除故障時有一個與Datetime的鏈接,但我不明白爲什麼會這樣 –

+0

這肯定不是它告訴你的全部內容 –

回答

0

使用TryParse而不是此行。如果文本框中的文本不是數字,您將獲得FormatException

Convert.ToInt32(TextBox1.Text) 
+0

實際上這工作:))這是正確的答案,問題得到解決 –

+0

@LucianTarna究竟是什麼工作?你已經在你的代碼中編寫了'Convert.ToInt32(TextBox1.Text)'。 o.O'TryParse'返回'bool',但'Convert.ToInt32'返回'int',看起來你的查詢期望它是'int'。 –

+0

int id; Int32.TryParse(TextBox1.Text,out id);而瞧,而不是轉換:D –

0

你已經把錯誤檢查文本框。您正在檢查如果TextBox1的值 是NULL或空值,則將其轉換爲int32,以便 空值或空值如何轉換爲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";        
} 
+2

您需要反轉測試**或**切換if/else語句 - 如果您同時擁有*,那麼您已經移動了該錯誤。 –

+0

'SQL注入'! –

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當文本框爲空

+0

打開「SQL注入」攻擊。 –

+0

是的:我在我的應用程序中寫過!但是當我複製粘貼它,似乎我失去了!不知何故 –

1

開關的string.IsNullOrEmtpy()int.TryParse(string, out int)返回如果真值被解析。 (或只是堅持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>[email protected] 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; 
    }