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:
My Code
So I use it on the form and it works, but I prefer the class's char, along with the other methods.
In this way I tried in Class controller but it doesn't work
my 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:
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; }
}
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);
}
}