Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a web application that should send quizzes to the users automatically. Now, I am working in writing simple console application that will read the Quiz table in the database which its schema as following:
***QuizID, Title, Description, IsSent***

IsSent is a flat with Bit data type which specified the status of message sent or not. I am trying now to retrieve the QuizID and IsSent from the database and doing the following logic:
If the quiz was not sent, sends it
Else
don't do anything

I am writing the small amount of code and I struggled now with retrieving the QuizID and IsSent flag and apply the above logic on both of them. So HOW TO DO THAT?

My code-behind:

C#
protected void Page_Load(object sender, EventArgs e)
    {
        SmtpClient sc = new SmtpClient("MAIL ADDRESS");
        MailMessage msg = null;


        try
        {

            msg = new MailMessage("yyyyyyy@gmail.com",
                "xxxxxxxxxx@gmail.com", "Message from PSSP System",
                "This email sent by the system");
            msg.IsBodyHtml = true;
            sc.Send(msg);
        }

        catch (Exception ex)
        {
            throw ex;
        }

        finally
        {
            if (msg != null)
            {
                msg.Dispose();
            }
        }


        //For requesting the Quiz_ID and check the IsSent flag for whether sending it or not
        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
        string cmdText = "SELECT QuizID, IsSent FROM dbo.QUIZ";

        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            // Open DB connection.
            using (SqlCommand cmd = new SqlCommand(cmdText, conn))
            {
                SqlDataReader reader = cmd.ExecuteReader();
                While (reader != null){
                    if (reader.Read())
                        if (reader["IsSent"].Equals(0))

                }
            }
        }


    }
Posted
Comments
[no name] 30-Dec-11 23:29pm    
What is the problem?
matrix388 30-Dec-11 23:47pm    
The above code does not work with me and I don't know how to fix it or complete it. I want to retrieve the QuizID and put it as link in the email that will be sent to all users. So the user can go to the quiz directly by clicking on that link.

1 solution

C#
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
string cmdText = "SELECT QuizID, IsSent FROM dbo.QUIZ where isnull(IsSent,0)=0";
//  then agove query filters only not sent records
//  this is the condition : "where isnull(IsSent,0)=0"
string sLinkId = "";
using (SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();
    // Open DB connection.
    using (SqlCommand cmd = new SqlCommand(cmdText, conn))
    {
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader != null)
        {
            if (reader.Read())
            {
                string sQid = reader["QuizID"].ToString();
                sLinkId += "<a href="http://websitename.com/quizepage.aspx?quizeid=" + sQid + "">Quize ID :" + sQid + "</a>";
            }

        }
    }
}


SmtpClient sc = new SmtpClient("MAIL ADDRESS");
MailMessage msg = null;


try
{
    msg.To.Add("mailid1@gmial.com");
    msg.To.Add("mailid2@gmial.com");
    msg.To.Add("mailid3@gmial.com");
    msg.From = new MailAddress("yourmailid@gmail.com");
    msg.Subject = "Message from PSSP System";
    msg.Body = "This Mail Send by System :" + sLinkId;
    msg.IsBodyHtml = true;
    sc.Send(msg);
}

catch (Exception ex)
{
    throw ex;
}

finally
{
    if (msg != null)
    {
        msg.Dispose();
    }
}



Try This...
 
Share this answer
 
Comments
matrix388 31-Dec-11 2:47am    
Thanks. I really appreciate your help.
matrix388 31-Dec-11 7:05am    
Ok and what about updating the value of {isSent} from false to true in the database, to eliminate the possibility of sending it again.
Technoses 2-Jan-12 11:57am    
add :
cmdText = "Update dbo.quiz set IsSent=1 where QuizID"+sQid;
cmd.ExecuteNonQuery();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900