通过在另一个文本框中输入ID在文本框中显示表数据(c#) [英] Displaying Table Data in Text Box by typing ID in another Text Box (c#)

查看:68
本文介绍了通过在另一个文本框中输入ID在文本框中显示表数据(c#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从数据库中检索单个项目,例如输入相应的CategoryID,然后在单独的文本框中显示CategoryName.

到目前为止,这是我尝试过的方法,但似乎没有任何效果.

I need to retrieve single items from a database e.g. CategoryName by entering the corresponding CategoryID and then display the CategoryName in a separate text box.

Here is what I''ve tried so far, but nothing seems to be working.

<pre lang="c#"><br />
<br />
 private void button1_Click(object sender, EventArgs e)<br />
        {<br />
            DataSet returnedTable = new DataSet();<br />
<br />
            int id = int.Parse(idTextBox.Text);<br />
<br />
            SqlParameter param = new SqlParameter();<br />
            param.ParameterName = "@Id";<br />
            param.Value = id;<br />
<br />
            SqlDataAdapter adapter = new SqlDataAdapter("Select CategoryName from Categories WHERE CategoryID=@Id", con);<br />
<br />
            adapter.Fill(returnedTable);<br />
<br />
            foreach (DataRow row in returnedTable.Tables[0].Rows)<br />
            {<br />
                for (int i = 0; i < returnedTable.Tables[0].Columns.Count; i++)<br />
                {<br />
                    textBox1.Text = row[i].ToString();<br />
                }<br />
            }<br />
        }<br />
<br />
</pre>



正如上面所见,我已经使用了一个参数来存储用户在ID文本框中输入的值,并在我的SQL Select查询中使用了此参数,如上面所见.我遇到一些问题,将不胜感激.

谢谢,
Jamie



I have used a parameter as you can see above to store the value that the user enters in the Id text box and have used this parameter in my SQL Select Query as you can see above. I''m having a few problems and would greatly appreciate any help.

Thanks,
Jamie

推荐答案

这是一场灾难.您在哪里将参数添加到适配器?只是随机创建它是没有帮助的.总体而言,在我看来,您似乎是在随机编码,应该读一本书.
This is a disaster. where do you add your parameter to your adapter ? Just creating it at random does not help. Overall, it looks to me like you''re coding at random and should read a book.


这篇关于通过在另一个文本框中输入ID在文本框中显示表数据(c#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆