call method from class to form

titojd

Well-known member
Joined
Oct 12, 2018
Messages
57
Programming Experience
1-3
Hello guys, I'm having problems calling a search method, when I put the method on the form and call it on the button,
the method works perfectly,
I want to put this method in the Controller class and call it to the form, and I am not able to call this method from the Controller class,
I tried to look at some tutorials on the net, but all I found are with DataGrid,
I need to search the array by the contest number and fill the array at the click of the button
an example image:
Contest.png


My Code
So I use it on the form and it works, but I prefer the class's char, along with the other methods.
C#:
public void searchResult()
        {
            SqlConnection con = new SqlConnection(DadosDaConexao.StringDaConexao);
            con.Open();
            string query = "Select _01,_02,_03,_04,_05,_06,_07,_08,_09,_10,_11,_12,_13,_14,_15 from TBNumber where Contest =" + txtContest.Text;
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ResultLabel[0].Text = dr[0].ToString();
                ResultLabel[1].Text = dr[1].ToString();
                ResultLabel[2].Text = dr[2].ToString();
                ResultLabel[3].Text = dr[3].ToString();
                ResultLabel[4].Text = dr[4].ToString();
                ResultLabel[5].Text = dr[5].ToString();
                ResultLabel[6].Text = dr[6].ToString();
                ResultLabel[7].Text = dr[7].ToString();
                ResultLabel[8].Text = dr[8].ToString();
                ResultLabel[9].Text = dr[9].ToString();
                ResultLabel[10].Text = dr[10].ToString();
                ResultLabel[11].Text = dr[11].ToString();
                ResultLabel[12].Text = dr[12].ToString();
                ResultLabel[13].Text = dr[13].ToString();
                ResultLabel[14].Text = dr[14].ToString();
            }
        }

In this way I tried in Class controller but it doesn't work

C#:
  public static string path = Directory.GetCurrentDirectory() + "\\ResultDB.sqlite";
        private static SQLiteConnection sqliteConnection;

        private static SQLiteConnection DbConnection()
        {
            sqliteConnection = new SQLiteConnection("Data Source=" + path);
            sqliteConnection.Open();
            return sqliteConnection;
        }

        public static void CriarBancoSQLite()
        {
            try
            {
                if (File.Exists(path) == false)
                {
                    SQLiteConnection.CreateFile(path);
                }
            }
            catch
            {
                throw;
            }
        }
 public static void CriarTabelaSQLite()
        {
            try
            {
                using (var cmd = DbConnection().CreateCommand())
                {
                    cmd.CommandText = "CREATE TABLE IF NOT EXISTS TBNumber(Contest Varchar(5),_01 Varchar(2),_02 Varchar(2),_03 Varchar(2),_04 Varchar(2),_05 Varchar(2), _06 Varchar(2)," +
                        "_07 Varchar(2),_08 Varchar(2),_09 Varchar(2),_10 Varchar(2),_11 Varchar(2), _12 Varchar(2),_13 Varchar(2),_14 Varchar(2), _15 Varchar(2))";
                     cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

public static int SearchResult(int Contest)
        {
            string Result = "";
            SQLiteCommand cmd = new SQLiteCommand();
            using (cmd = DbConnection().CreateCommand())
            {
                cmd.CommandText = "Select _01,_02,_03,_04,_05,_06,_07,_08,_09,_10,_11,_12,_13,_14,_15 from TBNumber where Contest =" + Contest;
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception exc)
                {
                    Result = string.Format("An error occurred while fetching the result {0}", exc.Message);
                }
                return 0;
            }
        }
C#:
 public class Result
    {
        public int Contest { get; set; }
        public int _01 { get; set; }
        public int _02 { get; set; }
        public int _03 { get; set; }
        public int _04 { get; set; }
        public int _05 { get; set; }
        public int _06 { get; set; }
        public int _07 { get; set; }
        public int _08 { get; set; }
        public int _09 { get; set; }
        public int _10 { get; set; }
        public int _11 { get; set; }
        public int _12 { get; set; }
        public int _13 { get; set; }
        public int _14 { get; set; }
        public int _15 { get; set; }
       
    }
my Button
C#:
private void btnBuscar_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtContest.Text))
            {
                SearchResult();//Here I call the form method (It works but it doesn't work for me)
                //Logica.SearchResult(txtContest.Text);//Here will be what I need, call from the class
                return;
               
            }
            else
            {
                MessageBox.Show("Enter a Contest to search!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
 
Solution
I would recommend doing something like this pseudo code:
C#:
struct Result
{
    public int Contest = 0;
    public int Number[] = new int[15];
    public bool IsValid => Contest != 0;
}
:
public Result SearchResult(int contest)
{
    var result = new Result();
    using (var connection = new SqlConnection(...))
    using (var command = new SqlCommand("SELECT _01,_02,_03,_04,_05,_06,_07,_08,_09,_10,_11,_12,_13,_14,_15 from TBNumber WHERE Contest = @contest", connection))
    {
        cmd.Parameters.AddWithValue("@contest", contest);
        using(var reader = command.ExecuteReader())
        {
            if (reader.Read())
            {
                result.Contest = contest;
                for(int i = 0; i < 15; i++)...
In this way I tried in Class controller but it doesn't work

What do you mean by "it doesn't work"?

Are you getting a compilation error? If so, tell us what error you are getting?

If you are net getting a compilation error, are you getting a runtime error or exception? If so what is that error or exception?

If you are not getting a compilation error or a runtime error, what behavior are you seeing? What behavior were you expecting to see?
 
Anyway, due to the lack of detail you provided, I'm assuming that you are getting a compilation error trying to use this:
C#:
Logica.SearchResult(txtContest.Text);
and that is because you are trying to pass a string, but you declared your method to take an int:
C#:
public static int SearchResult(int Contest)
 
hello @Skydiver, thanks for resonding, actually I am not able to implement the call on the button, I tried several ways but I didn't get any result, honestly speaking, I don't know how to make the call, that's why I come to ask for some help to implement the method call on button,

I tried like this, but I get an error cocode
C#:
  for (int i = 0; i < ResultLabel.Length; i++)
            {
                ResultLabel[i] = Logica.SearchResult(txtContest.Text);
            }
 
Last edited:
even changing my method to string I don't get anything on the button

C#:
public static string SearchResult(string Contest)
        {
            string Result = "";
            SQLiteCommand cmd = new SQLiteCommand();
            using (cmd = DbConnection().CreateCommand())
            {
                cmd.CommandText = "Select _01,_02,_03,_04,_05,_06,_07,_08,_09,_10,_11,_12,_13,_14,_15 from TBNumber where Contest =" + Contest;
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception exc)
                {
                    Result = string.Format("An error occurred while fetching the result {0}", exc.Message);
                }
                return Result;
            }
        }
no error but no return
C#:
for (int i = 0; i < ResultLabel.Length; i++)
            {
                ResultLabel[i].Text = Logica.SearchResult(txtContest.Text);
            }
 
The other part of the issue is that your "working code" does this:
C#:
SqlDataReader dr = cmd.ExecuteReader();
but your non-working code does this:
C#:
cmd.ExecuteNonQuery();

A non-query is not expected to return results, but you are trying to use it as if it returned results.

Further more, you are always returning an empty string:
C#:
string Result = "";
:
return Result;
 
I would recommend doing something like this pseudo code:
C#:
struct Result
{
    public int Contest = 0;
    public int Number[] = new int[15];
    public bool IsValid => Contest != 0;
}
:
public Result SearchResult(int contest)
{
    var result = new Result();
    using (var connection = new SqlConnection(...))
    using (var command = new SqlCommand("SELECT _01,_02,_03,_04,_05,_06,_07,_08,_09,_10,_11,_12,_13,_14,_15 from TBNumber WHERE Contest = @contest", connection))
    {
        cmd.Parameters.AddWithValue("@contest", contest);
        using(var reader = command.ExecuteReader())
        {
            if (reader.Read())
            {
                result.Contest = contest;
                for(int i = 0; i < 15; i++)
                    result.Number[i] = (int) reader[0].Value;
            }
        }
    }
    return result;
}
:
void btnBuscar_Click(object sender, EventArgs e)
{
    var contest = txtContest.Text;
    var result = new Result();
    if (!string.IsNullOrWhiteSpace(contest))
        result = Logica.SearchResult(contest);
    if (result.IsValid)
    {
        for(int i = 0; i <= 15; i++)
            Result[i].Text = result.Number[i].ToString();
    }
    else
    {
        // handle invalid contest number
    }
}
 
Solution
@Skydiver
using the struct it starts to give error in the ADD and Update methods

C#:
public static string Update(Resultado result)
        {

without the struct the error is in

C#:
result.Number[i] = (int)reader[0].value;
this using the method in the class
 
Last edited:
As before, tell us what error are you getting? If you are not getting an error, what behavior are you seeing and what were you expecting?

Recall that we are not sitting right beside you as are writing your code. We have no idea how you end up implementing our suggestions. We are not psychic or clairvoyant.
 
@Skydiver I managed to fix it here, I changed a few lines in your code and it worked, your help was of fundamental importance, thank you very much again.
 

Latest posts

Back
Top Bottom