2016-08-28 145 views
0

我試圖創建一個布爾返回函數,它將檢查文本框中的值,使用表中的數據讀取器檢查它以查找它們是否找到匹配項以及然後返回一個布爾值。我一直在使用這個函數,現在它已經很好用了。在函數返回布爾值時遇到了一些麻煩

public Boolean testname() 
    { 
     Boolean test=false; 
     SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\noewayout\Documents\Project.mdf;Integrated Security=True;Connect Timeout=30"); 
     SqlCommand cmd = new SqlCommand("select CNAME from CUSTOMER", conn); 
     conn.Open(); 
     SqlDataReader dr = cmd.ExecuteReader(); 
     string cname = ""; 
     while (dr.Read()) 
     { 

      cname = Convert.ToString(dr["CNAME"]); 
      if (cname == textBox1.Text.ToString()) 
      { 
       test = true; 
       return test; 
      } 


     } 
     conn.Close(); 
     return test; 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
     if (textBox1.Text == "") 
     { MessageBox.Show("Please type in Customer Name"); } 
     else 
     { 
      Boolean boo = testname(); 
      if (boo == false) 
      { 
       MessageBox.Show("Invalid Customer Name"); 
      } 
      else if (boo == true) 
      { 
       try 
       { 
        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\noewayout\Documents\Project.mdf;Integrated Security=True;Connect Timeout=30"); 
        SqlCommand cmd = new SqlCommand("select ONAME,PRICE,QTY from OIL_TYPE,SALEOIL,CUSTOMER where OIL_TYPE.OID=SALEOIL.OID and CUSTOMER.CID=SALEOIL.CID and CNAME='" + textBox1.Text.ToString() + "'", conn); 
        conn.Open(); 
        SqlDataReader dr = cmd.ExecuteReader(); 
        int qty = 0; 
        int price = 0; 
        string oname = ""; 
        while (dr.Read()) 
        { 
         oname = Convert.ToString(dr["ONAME"]); 
         qty = Convert.ToInt32(dr["QTY"]); 
         price = Convert.ToInt32(dr["PRICE"]); 
         int tcost = qty * price; 
         MessageBox.Show("Oil Name\t\tQuantity\tPrice\tTotal Cost\n" + oname + " \t" + qty + "\t" + price + "\t" + tcost); 
        } 
        conn.Close(); 
       } 
       catch (SqlException) { MessageBox.Show("Error!!"); } 
      } 
     } 
    } 

但是我在過去的幾個小時裏遇到了這個簡單的檢查方法有問題。

public Boolean testname() 
    { 
     Boolean test = false; 
     SqlConnection conn = new SqlConnection(dbsource); 
     SqlCommand cmd = new SqlCommand("select Fid from Firearm", conn); 
     conn.Open(); 
     SqlDataReader dr = cmd.ExecuteReader(); 
     string tfid = ""; 
     while (dr.Read()) 
     { 

      tfid = Convert.ToString(dr["Fid"]); 
      if (tfid == textBox6.Text.ToString()) 
      { 
       test = true; 
       return test; 
      } 


     } 
     conn.Close(); 
     return test; 
    } 
    private void searchbtn_Click(object sender, EventArgs e) 
    { 

      string mysql = "select * from Firearm where Fid= '" + textBox6.Text + "'"; 

      if (textBox6.Text == "") 
      { MessageBox.Show("Please fill the text field"); } 
      else 
      { 
       Boolean boo = testname(); 
       if (boo == false) 
       { 
        MessageBox.Show("Invalid Fid"); 
       } 
       else if (boo == true) 
       { 
        try 
        { 
         SqlConnection conn = new SqlConnection(dbsource); 
         SqlCommand cmd = new SqlCommand(mysql, conn); 
         SqlDataAdapter sda = new SqlDataAdapter(cmd); 
         DataTable dt = new DataTable(); 
         sda.Fill(dt); 
         dataGridView1.AutoGenerateColumns = true; 
         dataGridView1.DataSource = dt; 

         dataGridView1.Show(); 
        } 
        catch (SqlException) { MessageBox.Show("Error!!!"); } 
       } 
      } 
    } 

它不停地返回false,即使我輸入了正確的ID value.I've嘗試,我知道關閉,但仍然沒有go.Any幫助或建議將是great.Thanks

+0

是您Fid的列數據類型'char'或'nchar',而不是'varchar'? –

+0

你不需要在txtBox.Text屬性上調用.ToString(),因爲Text屬性已經是字符串類型了 –

+0

@Manish Dalal是的,Fid是varchar(20),我也嘗試了開槽.ToString(),但它是仍然返回假。 – noeway

回答

2

嘗試比較一切如下

if (tfid.Trim() == textBox6.Text.Trim())

+0

謝謝你,它工作。 – noeway

+0

。謝謝你的幫助。你剛剛結束了我一小時的頭痛。但是爲什麼它可以與修剪配合使用,而不是與我的正常比較呢?是的,這兩個表使用不同的數據庫,CNAME使用「nvarchar」,Fid使用「varchar」。您認爲這與它有關係嗎? – noeway

相關問題