Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
is it possible to query a db using the value in a column of the selected row?

What I have tried:

What i have tried already is query the db but it takes along time because some values are huge so i have created another table with the huge value.
Posted
Updated 1-Jul-16 3:56am
v2

1 solution

Try:
C#
if (myDataGridView.SelectedRows.Count > 0)
    {
    DataGridViewRow row = myDataGridView.SelectedRows[0];
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("SELECT Id, description FROM myTable WHERE myColumn=@VALUE", con))
            {
            cmd.Parameters.AddWithValue("@VALUE", row.Cells[0].Value);
            using (SqlDataReader reader = cmd.ExecuteReader())
                {
                while (reader.Read())
                    {
                    int id = (int)reader["Id"];
                    string desc = (string)reader["description"];
                    Console.WriteLine("ID: {0}\n    {1}", id, desc);
                    }
                }
            }
        }
    }
 
Share this answer
 
Comments
BEBE2011 1-Jul-16 10:24am    
Thanks.
OriginalGriff 1-Jul-16 10:37am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900