不从数据库检索数据到文本框. [英] Not retriving data into textboxes from database.

查看:97
本文介绍了不从数据库检索数据到文本框.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生您好,
我在Windows应用程序中创建了一个表单,其中创建了三个标签(第一个,中间,最后一个)&首先,我在底部使用搜索按钮创建了文本框.现在,我在数据库中创建了三个字段,分别称为first,middle,last,并为这些字段提供了值.我需要的是输入第一个"文本框中的内容,然后点击搜索"按钮,查找其余文本框,例如中间&最后,必须将数据检索到其中.现在,其给出的错误提示无法将varchar转换为int.我已将数据库中所有字段的数据类型指定为"varchar(50)".这是错误将varchar值"f"转换为数据类型int时转换失败..

这是代码:


Hello sir,
I created in windows application,one form in that i had created three labels (first,middle,last) & infront of that i created textboxes with Search button in bottom .And now i created in database three fields called first,middle,last also i gave values for those fields . what i need is wen i enter in "first" textbox and hit the button search,for the remaining textboxs like middle & last the data has to be retrived into it.Now its giving error saying cannot convert varchar to int.I had given datatype as for all the fields in database as "varchar(50)". This is the error Conversion failed when converting the varchar value ''f'' to data type int..

And this the code:


namespace Config_admin
{
    public partial class Form2 : Form
    {
        SqlCommand cmd = new SqlCommand();
        SqlDataReader rdr;
        DataSet ds;
        SqlDataAdapter da;
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=CBP\\SQLEXPRESS;Initial Catalog=ECG;Integrated Security=True");
            con.Open();
            cmd.CommandText = "select * from  Table2 where first=" + textBox1.Text.Trim();
            cmd.Connection = con;
            rdr = cmd.ExecuteReader();
            bool temp = false;
            while (rdr.Read())
            {
                //textBox1.Text = rdr.GetString(0);
                textBox2.Text = rdr.GetString(1);
                textBox3.Text = rdr.GetString(2);
                //textBox4.Text = rdr.GetString(3);
                //textBox5.Text = rdr.GetString(4);
                //textBox6.Text = rdr.GetString(5);
                //textBox7.Text = rdr.GetString(6);
                //textBox8.Text = rdr.GetString(7);
                //textBox9.Text = rdr.GetString(8);
                //textBox10.Text = rdr.GetString(9);
                temp = true;
            }
            if (temp == false)
                MessageBox.Show("not found");
            con.Close();

            con.Open();
            ds = new DataSet();
            da = new SqlDataAdapter("select * from Table2", con);
            da.Fill(ds, "Table2");
            con.Close();



        }
    }
}




请任何人帮我.

谢谢&问候
Pradeep CBZ




Please any one help me.

Thanks & Regards
Pradeep CBZ

推荐答案

1)不要连接字符串以构建SQL命令.它使您对意外或蓄意的SQL注入攻击敞开大门,这可能会破坏整个数据库.请改用参数化查询.
2)如果您要做的就是覆盖每个文本框中的最后一个,则遍历条目是没有意义的.
3)请丢弃您的物品!
4)你为什么要最后一刻?只会浪费时间...
5)绝对不要使用"SELECT * ...",然后通过数字索引引用返回的数据.您不知道Sql Server将以什么顺序返回列-如果表发生更改,则程序将失败.也不是出于任何明显的原因.令人讨厌的发现,整装待发.始终返回每个字段是一种不好的做法-浪费带宽,这会导致大量数据的显着减慢.
6)停止使用控件的Visual Studio默认名称.您可能还记得textBox7是今天的邮政编码,但是在数周的时间内,您将不得不去寻找与我们其他人一样的邮递区号.使用有意义的名称!
7)对SQL Command元素使用大写字母,对于字段使用小写字母也是一个好主意-在阅读时,遵循SQL命令变得更容易.
8)尝试考虑您的用户:未找到"不是有用的错误消息-它不会告诉他们什么地方出了问题,或者他们应该怎么做才能避免将来发生这种情况.
试试:
1) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
2) There is no point in looping through entries if all you are going to do is overwrite the last one in each textbox.
3) Please, dispose of your objects!
4) Why are you doing the last bit? All it will do is waste time...
5) Never, ever use "SELECT * ..." and then reference the returned data by numeric indexes. You do not know what order Sql Server will return columns in - if the table changes, your program fails. and not for any obvious reason. Nasty to find, and fiddly to fix. It is bad practice to always return every field anyway - it wastes bandwidth which can cause significant slowdown with large amounts of data.
6) Stop using the Visual Studio default names for controls. You may remember that textBox7 is the postcode today, but in a weeks time, you will have to hunt about to find out which one it is like the rest of us. Use meaningful names!
7) It is also a good idea to use UPPER CASE for Sql Command element, and lower case for fields - it makes it easier to follow the SQL command when you are reading it.
8) Try to think about your users: "not found" is not a helpfull error message - it doesn''t tell them what is wrong, or what they should do to avoid it in future.
Try:
private void button1_Click(object sender, EventArgs e)
    {
    using (SqlConnection con = new SqlConnection("Data Source=CBP\\SQLEXPRESS;Initial Catalog=ECG;Integrated Security=True"))
        {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("SELECT middle, last FROM Table2 WHERE first=@FIRST", con))
            {
            cmd.Parameters.AddWithValue("@FIRST", tbFirstName.Text.Trim());
            using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                if (rdr.Read())
                    {
                    tbMiddleName.Text = rdr.GetString("middle");
                    tbLastName.Text = rdr.GetString("last");
                    }
                else
                    {
                    MessageBox.Show(string.Format("No data was found for anyone with the first name of \"{0}\"",tbFirstName.Text.Trim()) );
                    }
                }
            }
        }
    }


它可能无法解决您眼前的问题,但是可以为您解决这一问题提供一种公平的方法.

完成此操作后,哪一行会出错?


It may not cure your immediate problem, but it goes a fair way toward getting rid of it.

When you have done this, which line is it erroring on?


这篇关于不从数据库检索数据到文本框.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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