值在我根据主键 - C# Winforms 从数据库筛选后变为 Null



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

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

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 应替换为 [在此处输入万岁站详细信息 ALONFWITH 完整地址] 在我的电子邮件正文中。 这是替换值后我的结果图像的SS

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

一行:从数据网格视图动态发送到我的文本字段的值不起作用,但是如果我选择手动输入值,它可以正常工作。

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

最新更新