c# - 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.
解决方案
输入测试名称的文本框
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
定义?
推荐阅读
- c - 用户输入数字的斐波那契函数
- javascript - 如何在 CSS 中模糊正文直到页面完全加载
- ios - 为 2017 年 5 月 23 日 12:30 创建日期格式化程序的最有效方法
- ios - Objective-C 方法的无法识别的选择器实例错误
- python - 我可以通过减去其中两个值的结果对字典列表进行排序吗?
- reactjs - 生成新的 ServiceStack React 项目缺少 SharpPagesFeature
- javascript - 如何在容器的同一页面上显示多个输入
- xna - 如何使用定向光理解 HLSL 中的指数阴影映射?
- javascript - 如何在 Cloud Functions 上恢复 Google Cloud Speech API (longRunningRecognize) 超时
- python - 网页抓取 url 中的嵌入链接内容