简体   繁体   English

在我根据主键从数据库中过滤后,值变为 Null - C# Winforms

[英]Values Becomes Null after I filter from database on the basis of a primary key - C# Winforms

I don't know if I am able to exactly explain my question but best of information that I am able to provide is following:我不知道我是否能够准确地解释我的问题,但我能够提供的最佳信息如下:

I have a datagrid view list of emails in it, I want to send emails to this list one by one and on the basis of the email (which is also a primary key), some information is fetched from the database and that fetched values should be replaced in my email body on specific places which are marked.我有一个电子邮件的数据网格视图列表,我想根据 email (这也是一个主键)将电子邮件一一发送到该列表,从数据库中获取一些信息并且获取的值应该可以在我的 email 车身上标明的特定地方更换。 It works perfectly when I enter all value manually but once I try to get values from the data grid view the values are displayed in the text fields (after getting fetched from the database) but when the email is sent, the required values are not replaced with the marked tags in my email body, as per my code tweaks, I have come to a conclusion that the value that I receive in the textbox from the database is dealt as a null value when passed to my email body.当我手动输入所有值时它工作得很好但是一旦我尝试从数据网格视图中获取值,值就会显示在文本字段中(从数据库中获取之后)但是当 email 被发送时,所需的值不会被替换使用我的 email 正文中的标记标签,根据我的代码调整,我得出的结论是,当我从数据库中收到的文本框中的值被传递给我的 Z0C83F57C786A0B4A39EFAB7 正文时,它被处理为 null 值。 Here is my value passing code:这是我的价值传递代码:

        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;
}

I have searched StackOverflow and some other online platforms but could not get what I want.我搜索了 StackOverflow 和其他一些在线平台,但无法得到我想要的。 I hope I was able to explain my problem or experts can suggest me more changes to my questions to make it more understandable to answer.我希望我能够解释我的问题,或者专家可以建议我对我的问题进行更多更改,以使其更易于回答。 Thank you:)谢谢:)

Simplified version of my question:我的问题的简化版本:

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

on placing break points, I see value is passing through txtVivaStation.txtbut it never replaces in在放置断点时,我看到值正在通过 txtVivaStation.txt 但它永远不会替换

[ENTER VIVA STATION DETAIL HERE ALONFWITH COMPLETE ADDRESS]

my email body.我的 email 身体。 value from txtVivaStation.txt should be replaced with [ENTER VIVA STATION DETAIL HERE ALONFWITH COMPLETE ADDRESS] in my email body. txtVivaStation.txt 中的值应替换为我的 email 正文中的 [ENTER VIVA STATION DETAIL HERE ALONFWITH COMPLETE ADDRESS]。 Here is SS of my result image after values are replaced这是替换值后我的结果图像的 SS

As you can see only date was replaced correctly with my marked tag but all other values were blank.正如您所看到的,只有日期被我标记的标签正确替换,但所有其他值都是空白的。

One Liner: value that are coming dynamically from the datagrid view to my textfields are not working but if I select enter values manually, it works fine. One Liner:从数据网格视图动态传递到我的文本字段的值不起作用,但如果我 select 手动输入值,它工作正常。

Luckily I have found the exact issue with my problem after playing more with my code.幸运的是,在更多地使用我的代码之后,我发现了我的问题的确切问题。 The main issue was because the email body was loading into the form even before the dynamic values came from the database and that was the reason why my values were not able to replace the marked tags.主要问题是因为 email 正文甚至在动态值来自数据库之前就已加载到表单中,这就是我的值无法替换标记标签的原因。 Now I am simply loading my email template after all the values have been retrieved from the database and its working perfect, thank you for your comments and precious time!现在我只是在从数据库中检索到所有值并且工作完美之后加载我的 email 模板,感谢您的评论和宝贵的时间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM