none
How to fix the code? RRS feed

  • Question

  • using System;
    using System.Windows.Forms;

    namespace MinimalApproachExample
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void UserNameTextBox_TextChanged(object sender, EventArgs e)
            {

            }

            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                PasswordTextBox.PasswordChar = checkBox1.Checked  '*';
            }

            private void ConnectButton_Click(object sender, EventArgs e)
            {
                if (!string.IsNullOrWhiteSpace(UserNameTextBox.Text) && !string.IsNullOrWhiteSpace(PasswordTextBox.Text))
                {
                    var ops = new DatabaseUser(Environment.UserName =="N10468000115\SQLHUNTER", "demo");
                    var loginResults = ops.SqlCredentialLogin(UserNameTextBox.Text, PasswordTextBox.Text);
                    if (loginResults)
                    {
                        var successValue = ops.DoWork(UserNameTextBox.Text, PasswordTextBox.Text);
                        var workResult = string.IsNullOrWhiteSpace(successValue);
                        if(workResult)
                        {
                            MessageBox.Show("Operation is successful");
                        }
                        else
                        {
                            MessageBox.Show(successValue);
                        }
                    }
                    else
                    {
                        MessageBox.Show("login is failed");
                    }
                }
                else
                {
                    MessageBox.Show("Incomplete information to continue");
                }
            }

            private void CancelButton_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
    • Moved by CoolDadTx Friday, November 8, 2019 1:52 PM Winforms related
    Thursday, November 7, 2019 10:02 AM

Answers

  • This means you didn't add it properly, see my image below where on the first line I used "peek" by right clicking on DoWork and it's found while in your code it's not because a) you didn't include it or b) you changed the scope of DoWork from public to either private or protected.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Decompressor Tuesday, November 12, 2019 2:49 PM
    Tuesday, November 12, 2019 12:20 PM
    Moderator

All replies

  • My guess is you copied my code into a new form which is the root of the problem, instead add my LoginForm to the project and adjust the code.

    This is passing the wrong information

    var ops = new DatabaseUser(Environment.UserName =="N10468000115\SQLHUNTER", "demo");

    In my case it would be KARENS-PC for the server as parameter 1, UserLoginExample for the database name as the second parameter e.g.

    var ops = new DatabaseUser("KARENS-PC", "UserLoginExample");

    Going with the error list

    • DatabaseUser does not contain DoWork, DoWork is line 58 in the class DatabaseUser.
    • ; expected would indicate you are missing a semi-colon,
    • Next may be a false positive caused by one of the other errors
    • Same with the next two errors.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, November 7, 2019 11:38 AM
    Moderator
  • What to do with

      PasswordTextBox.PasswordChar = checkBox1.Checked  '*';

    In your case it was:

    passwordTextBox.PasswordChar = ShowHidePasswordCheckBox.Checked ? '*' : '\0';

    I don't fully understand what it means and what I need write in my case?

    That's why error: ; expected

    What is DoWork? What must I make with my error connected with DoWork?


    Thursday, November 7, 2019 12:58 PM
  • What to do with

      PasswordTextBox.PasswordChar = checkBox1.Checked  '*';

    In your case it was:

    passwordTextBox.PasswordChar = ShowHidePasswordCheckBox.Checked ? '*' : '\0';

    I don't fully understand what it means and what I need write in my case?

    That's why error: ; expected

    What is DoWork? What must I make with my error connected with DoWork?


    This 

    PasswordTextBox.PasswordChar = ShowHidePasswordCheckBox.Checked ? '*' : '\0';

    Toggles the password Textbox to show or hide what is typed in by clicking ShowHidePasswordCheckBox. if checked use * to maske else remove the mask with \0

    Add DoWork from here

    https://github.com/karenpayneoregon/SqlServerUserLoginSharp/blob/master/MinimalApproachExample/DatabaseUser.cs#L58


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, November 7, 2019 3:13 PM
    Moderator
  • But when I type 

    PasswordTextBox.PasswordChar = checkBox1.Checked  '*';

    there is error: ; expected

    How to write it correctly?

    Thursday, November 7, 2019 3:42 PM
  • Did you read the error?

    PasswordTextBox.PasswordChar
    =
     checkBox1.Checked 
    '*';

    I'm using a conditional operator were the eval is either '*' or '\0'

    PasswordTextBox.PasswordChar = checkBox1.Checked ? 
        '*' : 
        '\0';
    In the end this is why you or anyone should not copy and paste code then expect it to work right away yet if you had used my login form you would be better off right now along with first studying my code.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Thursday, November 7, 2019 3:54 PM
    Moderator
  • As for "DoWork", there is different code on your link, I don't understand how to combine that code and this code,

    I would like more simple answers how to remove error

    Friday, November 8, 2019 9:26 AM
  • Hi pavlob,

    Thanks for posting here.

    According to your description, you seem to have encountered some errors while using Kareninstructor’s code.

    I want to know which database you are using now.

    Kareninstructor’s code is written for SQL server, but based on your previous question, you seem to be using Mysql.

    If my guess is correct, please let me know so I can modify this class to suit your requirements, or you can modify it yourself.

    It’s not difficult.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, November 12, 2019 2:21 AM
  • I use SQL server
    Tuesday, November 12, 2019 10:45 AM
  • using System;
    using System.Windows.Forms;
    
    namespace MinimalApproachExample
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void UserNameTextBox_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                PasswordTextBox.PasswordChar = checkBox1.Checked ? '*':'\0';
            }
    
            private void ConnectButton_Click(object sender, EventArgs e)
            {
                if (!string.IsNullOrWhiteSpace(UserNameTextBox.Text) && !string.IsNullOrWhiteSpace(PasswordTextBox.Text))
                {
                    var ops = new DatabaseUser("N10468000115\\SQLHUNTER", "demo");
                    var loginResults = ops.SqlCredentialLogin(UserNameTextBox.Text, PasswordTextBox.Text);
                    if (loginResults)
                    {
                        
                        var successValue = ops.DoWork(UserNameTextBox.Text, PasswordTextBox.Text);
                                            
                        var workResult = string.IsNullOrWhiteSpace(successValue);
                        if(workResult)
                        {
                            MessageBox.Show("Operation is successful");
                        }
                        else
                        {
                            MessageBox.Show(successValue);
                        }
                    }
                    else
                    {
                        MessageBox.Show("login is failed");
                    }
                }
                else
                {
                    MessageBox.Show("Incomplete information to continue");
                }
            }
            
    
            private void CancelButton_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
    

    Tuesday, November 12, 2019 10:50 AM
  • using System;
    using System.Data.SqlClient;
    using System.Security;
    
    namespace MinimalApproachExample
    {
        
        
            public class DatabaseUser
            {
                private string serverName;
                private string catalogName;
                public DatabaseUser(string pServerName, string pCatalogName)
                {
                    serverName = pServerName;
                    catalogName = pCatalogName;
                }
                public bool SqlCredentialLogin(string pUserName, string pPassword)
                {
    
                    string connectionString = $"Data source={serverName};" +
                        $"Initial catalog={catalogName};";
                    var securePassword = new SecureString();
                    foreach (var character in pPassword)
                    {
                        securePassword.AppendChar(character);
                    }
                    securePassword.MakeReadOnly();
                    var credentials = new SqlCredential(pUserName, securePassword);
                    using (var cn = new SqlConnection { ConnectionString = connectionString })
                    {
                        try
                        {
                            cn.Credential = credentials;
                            cn.Open();
                            return true;
                        }
                        catch (Exception e)
                        {
                            return false;
                        }
                    }
                }
    
           
    
        }        
    }

    Tuesday, November 12, 2019 10:51 AM
  • Tuesday, November 12, 2019 10:53 AM
  • This means you didn't add it properly, see my image below where on the first line I used "peek" by right clicking on DoWork and it's found while in your code it's not because a) you didn't include it or b) you changed the scope of DoWork from public to either private or protected.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Decompressor Tuesday, November 12, 2019 2:49 PM
    Tuesday, November 12, 2019 12:20 PM
    Moderator
  • public string DoWork(string pUserNName, string pPassword)
            {
    
                string connectionString = $"Data Source={serverName};" +
                                          $"Initial Catalog={catalogName};";
    
    
                var securePassword = new SecureString();
    
                foreach (var character in pPassword)
                {
                    securePassword.AppendChar(character);
           
    What it means "add it properly"? Because I've copied and pasted this code piece on your link to my "public class DatabaseUser, but had error "not all code paths return a value". When I tried to add "return true" to this code, I received error "cannot implicitly convert type 'bool' to 'string'. What to do next I don't know
    • Edited by Decompressor Tuesday, November 12, 2019 12:54 PM
    Tuesday, November 12, 2019 12:46 PM
  • using System;
    using System.Data.SqlClient;
    using System.Security;
    
    namespace MinimalApproachExample
    {
        
        
            public class DatabaseUser
            {
            public string DoWork(string pUserNName, string pPassword)
            {
    
                string connectionString = $"Data Source={serverName};" +
                                          $"Initial Catalog={catalogName};";
    
    
                var securePassword = new SecureString();
    
                foreach (var character in pPassword)
                {
                    securePassword.AppendChar(character);
                }
                return true;
            }
                private string serverName;
                private string catalogName;
                public DatabaseUser(string pServerName, string pCatalogName)
                {
                    serverName = pServerName;
                    catalogName = pCatalogName;
                }
                public bool SqlCredentialLogin(string pUserName, string pPassword)
                {
    
                    string connectionString = $"Data source={serverName};" +
                        $"Initial catalog={catalogName};";
                    var securePassword = new SecureString();
                    foreach (var character in pPassword)
                    {
                        securePassword.AppendChar(character);
                    }
                    securePassword.MakeReadOnly();
                    var credentials = new SqlCredential(pUserName, securePassword);
                    using (var cn = new SqlConnection { ConnectionString = connectionString })
                    {
                        try
                        {
                            cn.Credential = credentials;
                            cn.Open();
                            return true;
                        }
                        catch (Exception e)
                        {
                            return false;
                        }
                    }
                }
    
           
    
        }        
    }
    

    Tuesday, November 12, 2019 12:55 PM
  • Tuesday, November 12, 2019 12:57 PM
  • I've made it! Instead of "return true", I added "return "";"
    Tuesday, November 12, 2019 2:48 PM
  • I've made it! Instead of "return true", I added "return "";"

    I returned a string which has error text or nothing while nothing means no error. In my original code there are two return statements that are what is required.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, November 12, 2019 3:16 PM
    Moderator