首页 > 解决方案 > 根据主键从数据库中过滤后,值变为空 - C# Winforms

问题描述

我不知道我是否能够准确地解释我的问题,但我能够提供的最佳信息如下:

我有一个电子邮件的数据网格视图列表,我想根据电子邮件(也是主键)将电子邮件一一发送到该列表,从数据库中获取一些信息并且获取的值应该在我的电子邮件正文中标记的特定位置替换。当我手动输入所有值时它工作得很好但是一旦我尝试从数据网格视图中获取值,值就会显示在文本字段中(从数据库中获取之后)但是当发送电子邮件时,不会替换所需的值使用我的电子邮件正文中的标记标签,根据我的代码调整,我得出的结论是,我在文本框中从数据库收到的值在传递到我的电子邮件正文时被视为空值。这是我的价值传递代码:

        int cellValue = 0;
        int rowCount = dgvStudents.Rows.Count;
        for (int i = 0; i <= rowCount; i++)
        {
            string id = dgvStudents.Rows[i].Cells[cellValue].Value.ToString();
            string campus = dgvStudents.Rows[i].Cells[cellValue + 1].Value.ToString();
            txtStudentId.Text = id;
            cmbStudyCenter.Text = campus;
}




 **//And this is where the values are replaced with my marked tags:** 

 if (rdPreFinal.Checked)
        {
            string txtPreFinalText = txtEmailTemplate.Text.Replace("[ENTER VIVA TYPE HERE]", rdPreFinal.Text);
            txtEmailTemplate.Text = txtPreFinalText;



        }
        else if (rdFinal.Checked)
        {
            string rdFinalText = txtEmailTemplate.Text.Replace("[ENTER VIVA TYPE HERE]", rdFinal.Text);
            txtEmailTemplate.Text = rdFinalText;

        }
        else if (rdTestPhase.Checked)
        {
            string rdTestPhaseText = txtEmailTemplate.Text.Replace("[ENTER VIVA TYPE HERE]", rdTestPhase.Text);
            txtEmailTemplate.Text = rdTestPhaseText;

        }
        string newDateTime = VivaDate.Text + " " + timeFormat.Text;
        string newId = txtEmailTemplate.Text.Replace("[ENTER STUDENT ID]", txtStudentId.Text);
        txtEmailTemplate.Text = newId;
        string groupId = txtEmailTemplate.Text.Replace("[ENTER GROUP ID]", txtGroupId.Text);
        txtEmailTemplate.Text = groupId;
        string newDate = txtEmailTemplate.Text.Replace("[VIVA DATE/TIME]", newDateTime);
        txtEmailTemplate.Text = newDate;
        string newVivaStation = txtEmailTemplate.Text.Replace("[ENTER VIVA STATION DETAIL HERE ALONFWITH COMPLETE ADDRESS]", txtVivaStation.Text);
        txtEmailTemplate.Text = newVivaStation;

        //fetching previouus date and time for the viva

        SqlConnection Connection = new SqlConnection(connectionString);
        string commandText = "Select * from Sent_Viva_Emails where Student_Id ='" + txtStudentId.Text + "'";
        SqlCommand Command = new SqlCommand(commandText, Connection);
        Connection.Open();
        SqlDataReader sdr1 = Command.ExecuteReader(); SqlConnection sql = new SqlConnection();
        if (sdr1.Read())
        {
            string PreviousDate = sdr1.GetValue(6).ToString();
            string PreviousTime = sdr1.GetValue(7).ToString();
            PreviousDate = txtEmailTemplate.Text.Replace("[ENTER PREVIOUS VIVA DATE]", PreviousDate);
            txtEmailTemplate.Text = PreviousDate;

            PreviousTime = txtEmailTemplate.Text.Replace("[ENTER PREVIOUS VIVA TIME]", PreviousTime);
            txtEmailTemplate.Text = PreviousTime;
}

我搜索了 StackOverflow 和其他一些在线平台,但无法得到我想要的。我希望我能够解释我的问题,或者专家可以建议我对我的问题进行更多更改,以使其更易于回答。谢谢 :)

我的问题的简化版本:

string newVivaStation = txtEmailTemplate.Text.Replace("[ENTER VIVA STATION DETAIL HERE ALONFWITH COMPLETE ADDRESS]", txtVivaStation.Text);
        txtEmailTemplate.Text = newVivaStation;

在放置断点时,我看到值正在通过 txtVivaStation.txt 但它永远不会替换

[ENTER VIVA STATION DETAIL HERE ALONFWITH COMPLETE ADDRESS]

我的电子邮件正文。txtVivaStation.txt 中的值应替换为我的电子邮件正文中的 [ENTER VIVA STATION DETAIL HERE ALONFWITH COMPLETE ADDRESS]。 这是替换值后我的结果图像的 SS

正如您所看到的,只有日期被我标记的标签正确替换,但所有其他值都是空白的。

One Liner:从数据网格视图动态传递到我的文本字段的值不起作用,但如果我手动选择输入值,它工作正常。

标签: c#winformsdatagridview

解决方案


幸运的是,在更多地使用我的代码之后,我发现了我的问题的确切问题。主要问题是因为电子邮件正文甚至在动态值来自数据库之前就已加载到表单中,这就是我的值无法替换标记标签的原因。现在我只是在从数据库中检索到所有值并且工作完美之后加载我的电子邮件模板,感谢您的评论和宝贵的时间!


推荐阅读