You are here:

C#/C# How To View Contents of String Variable...Need Example...

Advertisement


Question
QUESTION: I was told that I could view the contents of a string variable using an ASP.net control statement as a label. I haven't been able to find an example online that will spell it out to me clearly since I am new to C#. Trying to view the contents of the string variable "extension" in the code listed below:

else if (FileToUpload.HasFile)
         {
         string extension = System.IO.Path.GetExtension(FileToUpload.FileName).ToLower();
         if (extension != "doc" || extension != "docx" || extension != "pdf" || extension != "rtf")
         {
         lit_Status.Text = "<center><font color='0000ff'>Error - Your resume format is not .doc, .docx, .pdf or .rtf. Please resubmit the form to attempt your upload again.<br /><br /></font></center>";
         }

ANSWER: Hi,

Sorry for the delay in reply.

Can you please let me know what exactly you want?  I am bit confused exactly what you want to achieve?

Please let me know so that I'll be able to reply soon.

---------- FOLLOW-UP ----------

QUESTION: I am trying to determine what the contents are for the string "extension" in the code below:

else if (FileToUpload.HasFile)
         {
string extension = System.IO.Path.GetExtension(FileToUpload.FileName).ToLower();

so I can determine if the value of "extension" actually can be checked with the below if-then statement:

         if (extension != "doc" || extension != "docx" || extension != "pdf" || extension != "rtf")

I am suspecting that the value of the string "extension" is not one of the items in the if-then statement condition check listed above, but I am not sure how to check for that in the code. I was told that I could use an ASP.net control as a label and then possibly somehow have the variable "extension" in the control to print the value of the string "extension," but having trouble finding an example.

Answer
Hi,

Here is the script which does the upload and do the validation.  I try to show how you should implement the code.  This code is not tested, i believe it should work.



<%@ Page Language="C#" %>

<script runat="server">
   protected void Button1_Click(object sender, EventArgs e)
   {
       if (FileUpload1.HasFile)
         try
         {
         string extension = System.IO.Path.GetExtension(FileToUpload.FileName).ToLower();
         if (extension != "doc" || extension != "docx" || extension != "pdf" || extension != "rtf")
{
Label1.Text = "<center><font color='0000ff'>Error - Your resume format is not .doc, .docx, .pdf or .rtf. Please resubmit the form to attempt your upload again.<br /><br /></font></center>";
}
else
{
         FileUpload1.SaveAs("C:\\Uploads\\" +
         FileUpload1.FileName);
         Label1.Text = "File name: " +
         FileUpload1.PostedFile.FileName + "<br>" +
         FileUpload1.PostedFile.ContentLength + " kb<br>" +
         "Content type: " +
         FileUpload1.PostedFile.ContentType;
}

         }
         catch (Exception ex)
         {
         Label1.Text = "ERROR: " + ex.Message.ToString();
         }
       else
       {
         Label1.Text = "You have not specified a file.";
       }
   }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   <title>Upload Files</title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       <asp:FileUpload ID="FileUpload1" runat="server" /><br />
       <br />
       <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
        Text="Upload File" /> <br />
       <br />
       <asp:Label ID="Label1" runat="server"></asp:Label></div>
   </form>
</body>  

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Srini Nagarajan

Expertise

can answer any kind of questions in ASP.NET, C#, VB.NET, ASP, SharePoint 2007, Coldfusion, Powerbuilder 7.00 / 8.00, JAVA servlets, MS SQL 2000 / MSSQL7, Sybase

Experience

Contact me if you need any custom development on ASP.NET, ASP, Coldfusion, Powerbuilder

©2012 About.com, a part of The New York Times Company. All rights reserved.