2016-08-28 96 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

相关问题