• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

What's wrong

What's wrong with this code?

private void Page_Load(object sender, System.EventArgs e)
{            
   if (!Page.IsPostBack)
  {
      string testStr;
      string[] strArray;
      testStr = User.Identity.Name
      strArray= testStr.Split('\');     <-----------------------  This line has error, if I change '\' to '/', there is no error.
   }
}

When I build the project, it gives error for that line: newline in constant, Too many characters in character literal.  I need split domain name\nt username and store into an array.  Thank you.
0
5281
Asked:
5281
  • 3
  • 3
1 Solution
 
dstanley9Commented:
\ is an escape character in C#.

Use

strArray= testStr.Split(@'\');    


or

strArray= testStr.Split('\\');    
0
 
5281Author Commented:
dstanley9,
You are right, it doesn't give error if use \\
But I have another question, if I want to output an array item, I use response.write strArray[1], it gives error too.  How to display an array item?

if (!Page.IsPostBack)
{
            string testStr;
            string[] strArray;
                        
            testStr = User.Identity.Name;
            strArray= testStr.Split('\\');
            response.write  strArray[1];
}
0
 
dstanley9Commented:
One of two things:

            response.write  strArray[1];

1) C# requires parentheses for function calls:

            response.write  (strArray[1]);

2) if strArray has only one item, this call will fail at run-time (but not at compile time)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
5281Author Commented:
For some reason, I can't use response.write at all, it gives this message when i compile.

The type or namespace name 'response' could not be found (are you missing a using directive or an assembly reference?)

Any idea?
0
 
dstanley9Commented:
You need to capitalize Response and Write:

Response.Write(strArray[1]);

Unlike VB, C# is case-sensitive ;)
0
 
5281Author Commented:
Thank you so much, dstanley9.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now