【问题标题】:Label not changing after extracting data from SQL Table从 SQL 表中提取数据后标签未更改
【发布时间】:2013-12-30 19:39:26
【问题描述】:

Dear User 后面的 User 标签会根据lblcName.Text = detail;的代码对应当前登录的用户而变化

并且我已经为 iCount 、 Viewer 和 Time 创建了一个代码,以根据登录的用户进行更改,获取用户数据并在此处显示。但是标签没有改变..这段代码中的任何地方我可能做错了吗?

通过将breakpoint 放在DataRow firstRow = dtC.Rows[0]; 上进行调试显示我的label for iCount 值为1,但没有更改该值,并且viewertime label 值也是查看器和时间

public partial class Notification : Form
    {
        public Notification(string detail)
        {
            InitializeComponent();
            lblcName.Text = detail;
        }

        private void Notification_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
            conn.Open();
            DataTable dtC = new DataTable();

            SqlDataAdapter daCount = new SqlDataAdapter("select iCount,LastView,Viewtime from ComDet where cName = @cName", conn);
            daCount.SelectCommand.Parameters.Add("@cName", SqlDbType.VarChar).Value = lblcName.Text;

            daCount.Fill(dtC);
            DataRow firstRow = dtC.Rows[0];

            lblCount.Text = firstRow["iCount"].ToString();
            lblLastView.Text = firstRow["LastView"].ToString();
            lblView.Text = firstRow["ViewTime"].ToString();
conn.Close();


        }
    }

【问题讨论】:

  • 您确定您实际显示的是完成加载的表单吗?
  • 是的..这是在单击按钮以显示表单之后,但标签没有改变..
  • 可能根本不相关,但是你的数据库master真的有这张表吗?如果是,则不是一个好主意。
  • 听起来你应该向我们展示更多代码......你在这里没有真正提供足够的上下文。 (顺便说一句,您应该使用using 语句进行连接。)
  • 如果您将标签更改为某个字面值会发生什么,例如“这是不同的”?

标签: c# sql winforms forms


【解决方案1】:

首先,确保数据库不为空。其次,使用刷新表单控件的表单的Validate()方法。

【讨论】:

  • 好的,调用 Validate() 方法怎么样?尝试调用方法 Update() 以及(验证后)。
  • 我的表引用错误。我需要的数据确实在 dbo.ComDet 中,但我使用的是另一个表中的 cName,即 dbo.cID。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-21
  • 2020-06-18
  • 1970-01-01
  • 2018-11-10
相关资源
最近更新 更多