首页 > 解决方案 > C# Tuple - How to search for questions based on input from text box

问题描述

I am creating a test where the radio buttons populate based on the test that the user needs to take.

The test is created by an admin in a table. We currently have a Favorite Color table already set in place but I am looking to future-proof our code to allow another test to be selectable. Below is my code but it was done quickly and thus has the Favorite Color table hardcoded in the SQL statement.

I am also using TUPLE to set the questions in a Binary pair. This allows the user to choose between 2 options. See below.

//Loads the Test data from the DB and organizes the data into Tuple Binary Pairs
    private void TestF_Load(object sender, EventArgs e)
    {
        list = new List<Tuple<string, string>>();
        tempTable = new DataTable();
        tempTable.Columns.Add("Item1");
        tempTable.Columns.Add("Item2");
        tempTable.Columns.Add("Value");
        var tkey = new DataColumn[] { tempTable.Columns[0], tempTable.Columns[1] };
        tempTable.PrimaryKey = tkey;
        SqlCommand cmd = new SqlCommand("SELECT item_name FROM TEST_ITEMS I JOIN TESTS T ON I.test_id = T.test_id WHERE test_name = 'Favorite Colors'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        total = dt.Rows.Count;
        for (int i = 0; i < total - 1; i++)
        {
            for (int j = total; j > i + 1; j--)
            {
                list.Add(new Tuple<string, string>(dt.Rows[i].ItemArray[0].ToString(), dt.Rows[j - 1].ItemArray[0].ToString()));
            }
        }

        // Get the items from the DB for test X (id = 1)
        // Randomize pairings
        // Present list[0].Item1
        // Author Nils Updated 5/8
        radioButton1.Text = list[0].Item1;
        radioButton2.Text = list[0].Item2;
        radioButton3.Text = "Undecided";
        location++;
        total = list.Count;
    }

Is there a way that I can have the user input the name of their test in a text box which can then update the SQL statement based on their input?

I have the textBox1 as my pre-established text box with a button to select once the input has been entered, however, all of my iterations result in radioButton1.Text = list[0].Item1; throwing an exception error image of error

Any help would be appreciated.

标签: c#sqltuples

解决方案


输入测试名称的文本框

string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["key"].ConnectionString;
using (SqlConnection sqlConn = new SqlConnection(connStr))
{
    sqlConn.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT item_name FROM TEST_ITEMS I JOIN TESTS T ON I.test_id = T.test_id WHERE test_name = @testName", sqlConn))
    {
        cmd.Parameters.AddWithValue("testName", TextBox1.Text);

        DataTable dt = new DataTable();
        using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
        {
            adapter.Fill(dt);
        }

        // use dt as you did
    }
}

对于异常,我猜它SELECT没有检索任何内容,请使用调试模式检查是否dt按预期填充了数据。

顺便说一句,在哪里con定义?


推荐阅读