-2

I want to check if my randomly generated number matches [id] from my datatable, this is necessary so that I can take random values ​​from the table by id. what i came up to:

            Random rnd = new Random();
            int card = rnd.Next(1,21);
            label8.Text = Convert.ToString(card);

            try
            {
                while (await sqlReader.ReadAsync())
                {
                    string find = "item_manuf_id = 'some value'";
                    DataRow[] foundRows = table.Select(find);
                }
            }

thanks to rufus L, now i came up with such a code:

string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\project vstudio\ShefPowarSln\ShefPowar\Database.mdf;Integrated Security=True";

            Random rnd = new Random();
            int card = rnd.Next(1, 21);

            bool isValidId = false;
            string sqlQuery = "SELECT [Id] from [Recipes] where [Id] = @id";

            using (SqlConnection connection = new SqlConnection(connectionString))
            using (SqlCommand command = new SqlCommand(sqlQuery, connection))
            {
                connection.Open();
                var idParam = new SqlParameter("id", SqlDbType.Int);
                idParam.Value = card;
                command.Parameters.Add(idParam);
                var result = command.ExecuteReader();
                if (result.HasRows) isValidId = true;
                if (isValidId == true) label9.Text = "Yass";
            }
Draconov
  • 1
  • 3
  • 1
    There's not a lot of context in your question. If you could update it to first describe what you're trying to achieve. Then describe an attempted solution. For example, are you trying to find a single recipe by a unique id? Provide an example of your data - the recipes for instance. – Jevon Kendon Jun 03 '20 at 22:07
  • What's `table`? Does that code fragment run? Your idea to generate a random number looks sort of right, but reading from the database, that part looks confused. Have you checked out Dapper? It has a simple SQL interface for reading data: https://dapper-tutorial.net/ – Jevon Kendon Jun 03 '20 at 22:23
  • no, it is not working and i can't use any additional libraries or extensions – Draconov Jun 03 '20 at 22:27

1 Answers1

1

It sounds like you just need to query the database for any record from some table where the item_manuf_id field matches the card value:

bool isValidId = false;
string query = "SELECT [item_manuf_id] from [tableName] where [item_manuf_id] = @id";

using (SqlConnection con = new SqlConnection(/* connection info */))
using (SqlCommand cmd = new SqlCommand(query, con))
{
    var idParam = new SqlParameter("@id", SqlDbType.Int);
    idParam.Value = card;
    cmd.Parameters.Add(idParam);
    var result = cmd.ExecuteReader();
    if (result.HasRows) isValidId = true;
}

// isValidId  will be 'true' if any records for that id were found
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • it bugs here ===> var result = command.ExecuteReader(); (it says it need open connection, but why if you `using (SqlConnection con = new SqlConnection(/* connection info */))`) – Draconov Jun 03 '20 at 22:38