DataReader错误和日期时间选择器 [英] DataReader error & datetime picker

查看:73
本文介绍了DataReader错误和日期时间选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的项目的代码.
在这里,我想在页面上显示数据,单击按钮.,所以我使用了数据读取器,但是仍然出现错误.
错误...

附加信息:已经有与此命令关联的打开的DataReader,必须先关闭它.

还有一个问题是,我无法将日期值分配给datetime picker控件.我的数据库字段类型是"datetime".

请帮助我&给我发送正确的代码.

Following is the code of my project.
Here I want to show data on page, on button click.,so I used data reader,but still error is coming like.
ERROR...

Additional information: There is already an open DataReader associated with this Command which must be closed first.

And also one more problem is that, I am fail to assign date value to datetime picker control.my database field type is " datetime".

Please help me & send me correct code.

private void button7_Click(object sender, EventArgs e)
{

    SqlCommand cmd = new SqlCommand();

//if (cn.State ==ConnectionState .Closed )
{
    //cn.Open();

//SqlDataReader dr;
    //dr  = new SqlDataReader();

cmd.CommandText = "select * from clubAC where accountno = " + txtacno .Text + "";
    cmd .Connection =cn ;

SqlDataReader  dr = cmd.ExecuteReader();
if (dr .Read ())
    {

        txtname.Text = dr["name"].ToString();
        txtnomadd.Text = dr["address"].ToString();
        txtage.Text = dr["age"].ToString();
        txtsex.Text = dr["sex"].ToString();
        dateTimePicker1.Value = Convert.ToDateTime.dr["dateofcommencement"].ToShortDateString;
        //dateTimePicker2.Value = dr["duedate"].ToString();
        txtterm.Text = dr["term"].ToString();
        //dateTimePicker3.Value = dr["lastpaymentdate"].ToString();
        txtbkamt.Text = dr["bookingamount"].ToString();
        txtamtpay .Text  = dr["fullpayableamount"].ToString();
        //dateTimePicker4.Value = dr["dateoptrfdpay"].ToString();
        txtassocode.Text = dr["associatecode"].ToString();
        txtmodepay.Text = dr["modeofpaymengt"].ToString();

        txtoptrfdpay.Text = dr["optionalrefundpay"].ToString();
        txtassoname.Text = dr["associatename"].ToString();
        txtnomage.Text = dr["nomineeage"].ToString();
        txtnomadd.Text = dr["nomineeaddress"].ToString();
        txtnomname.Text = dr["nomineename"].ToString();
        txtnomrel.Text = dr["relation"].ToString();
        cmd.Dispose();
        dr.Close();
     }

推荐答案

要解决您的第一个问题,请颠倒这两行的顺序:
To fix your first problem, reverse the order of these two lines:
cmd.Dispose();
dr.Close();


成为:


Becomes:

dr.Close();
cmd.Dispose();



其次,不要通过串联来创建字符串:这会使您容易受到意外或故意的SQL注入攻击.改用参数化查询:



Secondly, don''t create your strings by concatenation: it leaves you wide open to an accidental or deliberate SQL Injection attack. Use Parametrized queries instead:

cmd.CommandText = "select * from clubAC where accountno = @AN";
cmd.Parameters.AddWithValue("@AN", txtacno.Text);
cmd.Connection =cn ;


您报告的最后一个问题需要更多信息:将日期存储在数据库中的字段类型是什么?


The final problem you report needs more information: what is the field type that you are storing the date in the database?


这篇关于DataReader错误和日期时间选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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