-3

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. 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. 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. 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

[ENTER VIVA STATION DETAIL HERE ALONFWITH COMPLETE ADDRESS]

my email body. value from txtVivaStation.txt should be replaced with [ENTER VIVA STATION DETAIL HERE ALONFWITH COMPLETE ADDRESS] in my email body. Here is SS of my result image after values are replaced

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.

  • `string commandText = "Select * from Sent_Viva_Emails where Student_Id ='" + txtStudentId.Text + "'";` I can't help with your actual question, since I don't understand it. But don't do that. https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection – mjwills Jun 11 '20 at 00:52
  • Please update your question to show the `CREATE TABLE` for `Sent_Viva_Emails`. – mjwills Jun 11 '20 at 00:56
  • I am simplifying my questions: string newVivaStation = txtEmailTemplate.Text.Replace("[ENTER VIVA STATION DETAIL HERE ALONFWITH COMPLETE ADDRESS]", txtVivaStation.Text); txtEmailTemplate.Text = newVivaStation; on break points, I see value is pass to txtVivaStation.Text but it is not replaced with [ENTER VIVA STATION DETAIL HERE ALONFWITH COMPLETE ADDRESS], what could be the issue? – shahzad waheed Jun 11 '20 at 00:58
  • I have edited my ending part of my question and have not deleted the old questions because it carries detailed explanation of what I want – shahzad waheed Jun 11 '20 at 01:03
  • Instead of guessing why don't you debug your code and check what's happening? – Gusman Jun 11 '20 at 01:08
  • What is the **exact** (don't guess) value of `newVivaStation`? Please check in the `Immediate Window`. Also check `newVivaStation.Length`. – mjwills Jun 11 '20 at 01:13
  • I think you misunderstood me or maybe I was not able to explain, I am not guessing, on breakpoint, I see value is there but when it is passed to a string variable it vanishes or disappears. I have attached a ss at the end of my question, if it helps to understand my question :) – shahzad waheed Jun 11 '20 at 01:18
  • That's not possible, if the value is there when you pass it to `Replace` it can't be arbitrarily changed to a null or ignored. Put a breakpoint at the beginning of your code and step line by line to check what's really happening. – Gusman Jun 11 '20 at 01:25
  • Already did, I see newVivaStation.txt carries my value but it never passes it to replace on its tag but in case of date (a ss has been attached at the end of my question), value is replaced perfectly. can it be because I am not using a global string variable and I am declaring my new variable within the method? I have tried declaring a global variable to but still not luck :( – shahzad waheed Jun 11 '20 at 01:28
  • 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. – shahzad waheed Jun 11 '20 at 01:44

1 Answers1

0

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. 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!