SmtpClient in c# resource is inaccessible due to its protection level

The following C# source code shows how to send an email from my email address using SmtpClient.

protected void btnFinal_Click(object sender, EventArgs e)
{
    SmtpClient smtpClient = new SmtpClient("xxxx");
    MailMessage mailMessagePlainText = new MailMessage();
    mailMessagePlainText.IsBodyHtml = true;
    mailMessagePlainText.BodyEncoding = Encoding.UTF8;

    mailMessagePlainText.Subject = DateTime.Now.ToString("dd-MM-yyyy");

    mailMessagePlainText.From = new MailAddress("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dab7a3bfb7bbb3b69ab7a3bfb7bbb3b6f4b9b5b7">[email protected]</a>, ""); 

    if (count > 1)
    {
        values = value.Split(new[] { ';' }, 2);
        first = values[0];
        rest = values[1];
        mailMessagePlainText.To.Add(new MailAddress(first.ToString().Trim().ToUpper(), ""));            
    }
    else
    {
        mailMessagePlainText.To.Add(new MailAddress(second.ToString().Trim().ToUpper(), ""));
    }

    try
    {
        smtpClient.Send(mailMessagePlainText);
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('ok';", true);
    }
    catch (Exception ex)
    {
        throw (ex);
    }
}

Now i need using the string first or second (recipient of my message) in Hyperlink in aspx markup
<asp:TemplateField HeaderText="Number row"
    ItemStyle-HorizontalAlign="Center"
    ItemStyle-CssClass="ddl_Class_new"
    ItemStyle-Font-Size="Smaller"
    ItemStyle-Font-Names="Arial">
    <ItemTemplate>
        <asp:HyperLink
            ID="HyperLink1"
            runat="server"
            CssClass="ddl_Class_new"
            NavigateUrl='<%# String.Format("Default.aspx?ID={0}&TOUSER={1}", 
                             Eval("Number row").ToString(), ... ) %>'>

                            <%# Eval("Number_row") %>
        </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

I have tried this without success:

NavigateUrl='<%# String.Format("Default.aspx?ID={0}&TOUSER={1}", 
                 Eval("Number row").ToString(), 
                 first.ToString().Trim().ToUpper()) %>'>

or
NavigateUrl='<%# String.Format("Default.aspx?ID={0}&TOUSER={1}", 
                 Eval("Number row").ToString(), 
                 second.ToString().Trim().ToUpper()) %>'>

The error is:

CS0122 first ( or second ) is inaccessible due to its protection level

Any suggestion please?

Thanks in advance for any help

Update #1

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink HyperLink1 = (HyperLink)e.Row.FindControl("HyperLink1");
        HyperLink1.NavigateUrl = "Default.aspx?ID=" + DataBinder.Eval(e.Row.DataItem, "Number row").ToString() + "&TOUSER=" + first.ToString();
        HyperLink1.Text = DataBinder.Eval(e.Row.DataItem, "Number row").ToString();
    }
}

Error
Object reference not set to an instance of an object

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Why don’t you set it all from the code behind on the data bound event of the Grid?

 HyperLink1.NavigateURL = "default.aspx?id="+first;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x