Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
Hi Sir,

Good day to all!
I tried to test my asp.net application over LAN (other computer) but it's failed to connect to the sql server 2005 express edition over LAN (windows authentication). My first webform is the Login.aspx page. (but in my development environment it was connection success, my sql server 2005 express edition installed locally on my computer) Below is my code: (Thank you in Advance)
private bool ValidateUser(string userName, string passWord)
  {
      SqlConnection conn;
      SqlCommand cmd;
      string lookupPassword = null;

      // Check for invalid userName.
      // userName must not be null and must be between 1 and 15 characters.
      if ((null == userName) || (0 == userName.Length) || (userName.Length > 15))
      {
          System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.");
          return false;
      }

      // Check for invalid passWord.
      // passWord must not be null and must be between 1 and 25 characters.
      if ((null == passWord) || (0 == passWord.Length) || (passWord.Length > 25))
      {
          System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.");
          return false;
      }

      try
      {
          // Consult with your SQL Server administrator for an appropriate connection
          // string to use to connect to your local SQL Server.
          conn = new SqlConnection("server=GD467887\\SQLEXPRESS;Integrated Security=SSPI;database=Irocdb");
          conn.Open();

          // Create SqlCommand to select pwd field from users table given supplied userName.
          cmd = new SqlCommand("Select pwd from users where uname=@userName", conn);
          cmd.Parameters.Add("@userName", SqlDbType.VarChar, 25);
          cmd.Parameters["@userName"].Value = userName;

          // Execute command and fetch pwd field into lookupPassword string.
          lookupPassword = (string)cmd.ExecuteScalar();

          // Cleanup command and connection objects.
          cmd.Dispose();
          conn.Dispose();
      }
      catch (Exception ex)
      {
          // Add error handling here for debugging.
          // This error message should not be sent back to the caller.
          System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " + ex.Message);
      }

      // If no password found, return false.
      if (null == lookupPassword)
      {
          // You could write failed login attempts here to event log for additional security.
          return false;
      }

      // Compare lookupPassword and input passWord, using a case-sensitive comparison.
      return (0 == string.Compare(lookupPassword, passWord, false));

  }
Posted
Updated 8-Mar-11 17:22pm
v2
Comments
Silver Lightning 8-Mar-11 21:56pm    
Please help me on this sir. Your help is really much appreciated.
Orcun Iyigun 8-Mar-11 23:06pm    
conn = new SqlConnection("server=.\\SQLEXPRESS;Integrated Security=SSPI;database=Irocdb");

Here do the other db have the same Irocdb ? this might help..

also check here;
http://www.connectionstrings.com/sql-server-2005#p1

what is the error?

1 solution

Try using SQL authentication or impersonate[^] your web application with a local user having sufficient rights to connect to database (your user id might be fine).
 
Share this answer
 
Comments
Silver Lightning 9-Mar-11 0:06am    
what do you mean sir? can you give an example? thanks
Prerak Patel 9-Mar-11 0:19am    
Use "Data Source=GD467887\\SQLEXPRESS;Initial Catalog=Irocdb;User Id=sa;Password=yourSAPassword;" as connection string first
Silver Lightning 9-Mar-11 0:57am    
thanks sir, but I used only the windows authentication, I have no sql server authentication.What will I do sir?
Prerak Patel 9-Mar-11 1:04am    
Then you impersonation as shown in the link in answer.
Silver Lightning 9-Mar-11 3:41am    
Thanks sir Prerak. :)

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