0

My friend and I are making an application using c#, WPF and visual studio. We've made a login feature using MS SQL Server and the database is on my computer. When I share the source code to my friend and he opens it in Visual Studio (on his PC) he can't login like I can. Anytime he presses the "Login" button the program instantly crashes, not even giving any errors.

On my PC when I click the "Login" button it moves to the next page successfully, so we believe it has something to do with the SQL Database. How can I make it so that it works for his PC too?

Code for Login Button

private void UserSignInBtn_Click(object sender, RoutedEventArgs e)
        {
            SqlConnection sqlCon = new SqlConnection(connectionString);


            try
            {
                if (sqlCon.State == ConnectionState.Closed)
                {
                    sqlCon.Open();
                    string query = "SELECT COUNT (1) FROM tblSignUP WHERE StudentName=@StudentName AND Password=@Password";
                    SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
                    sqlCmd.CommandType = CommandType.Text;
                    sqlCmd.Parameters.AddWithValue("@StudentName", tbID.Text);
                    sqlCmd.Parameters.AddWithValue("@Password", PB.Password);
                    int count = Convert.ToInt32(sqlCmd.ExecuteScalar());
                    if (count == 1)
                    {

                        // Custom Message Box and Dim Effect
                        var jim = new Dim();

                        jim.Show();
                        this.Effect = new BlurEffect();

                        var lsmb = new Custom_MessageBoxes.LoginSuccessfulMsgBox();
                        lsmb.ShowDialog();

                        this.Effect = null;
                        jim.Close();

                        var User_Homepage = new User_Homepage();
                        NavigationService.Navigate(User_Homepage);
                    }
                    else
                    {
                       // Custom Message Box and Dim Effect 2
                        var him = new Dim();

                        him.Show();
                        this.Effect = new BlurEffect();

                        var rmdlgb = new ReturnMessageDialogueBox();
                        rmdlgb.ShowDialog();

                        this.Effect = null;
                        him.Close();

                    }
                }
            }
            catch(Exception ex)
            {

            }
            finally
            {
                sqlCon.Close();
            }
        }```
  • 1
    Show some code - your login code. Where is your SQL db? Are you on the same network? Did you error handle the login procedure (using try/catch) etc? – Charleh May 25 '19 at 11:06
  • If you were doing exception handling you would probably notice a [`SqlException`](https://stackoverflow.com/questions/18060667/why-am-i-getting-cannot-connect-to-server-a-network-related-or-instance-speci). – Crowcoder May 25 '19 at 11:06
  • Make it so that he can access the SQL Server instance on your PC, or make sure he has the localdb file, depends on the technology you're using. There's not enough information for us to give any more advise than that; especially when we don't have any errors or code – Thom A May 25 '19 at 11:07
  • Yes there was a try/catch exception handling used in the login page. I've updated the original post with the Login code –  May 25 '19 at 11:25
  • Can add your code to the post with the exception – sayah imad May 25 '19 at 11:26
  • If the connectionstring points to your local database it is impossible for your friend to reach your database using the same connectionstring. You should read how to configure Sql Server to be visible between machines and how to prepare a connectionstring for your friend – Steve May 25 '19 at 11:35
  • 1
    Your `catch` block is empty. That is not "handling" the exception, that is ignoring it. – Crowcoder May 25 '19 at 11:36
  • So should I change the connection string somehow? or should the database be installed onto my friends PC, he also has MS SQL Server –  May 25 '19 at 11:47
  • @JojoAmankwa, assuming you want both application instances to share the same database and data, both connection strings should specify the same data source (your PC name). Make sure your SQL instance is [configured to allow remote connections](https://learn.microsoft.com/en-us/sql/relational-databases/sql-server-configuration-manager?view=sql-server-2017) and the port open in the Windows firewall. – Dan Guzman May 25 '19 at 12:07
  • And don't use [addwithvalue](http://www.dbdelta.com/addwithvalue-is-evil/). Learn to properly parameterize your queries. – SMor May 25 '19 at 14:05

1 Answers1

0

Enter the following into the Catch and review the error on your friend's machine it will tell you what the exact error is when you look into the eventviewer on his machine.

        using (EventLog eventLog = new EventLog("Application"))
        {
            eventLog.Source = "SQL Error: From My Application";
            eventLog.WriteEntry(ex.StackTrace, EventLogEntryType.Error, 101, 1);
        }
betelgeuce
  • 837
  • 5
  • 18