noeway noeway - 3 months ago 18
C# Question

Having some trouble with returning boolean value from a function

I'm trying to create a boolean return function that would check the value from the textbox,check it with the datareader from a table to find whether they found a match or not and then return a boolean value.I've been using this function quite sometime now and it works fine.

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!!"); }
}
}
}


However I'm having trouble with this simple check method for the past few hours.

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!!!"); }
}
}
}


It kept returning false even though i entered the correct id value.I've tried everything that i know off but still no go.Any help or suggestion would be great.Thanks

Answer

Try to compare as below

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