CS1061 errors in converting a MySQL Server code to MS SQL Server code

wire_jp 216 Reputation points
2023-03-12T17:28:10.6033333+00:00

Hello,

I am converting some MySql Server Code to MS SQL Server code in a .Net Maui app which I am working on. I received three instances of CS1061 errors in a Contact.cs file as shown below: -


public static bool CheckIfUserExists(Contact contact)  //Error CS1061 ‘Contact.CheckIfUserExists(Contact)’:not all code paths return a value’
        {
            string stringConnection = "Data Source=190.165.2.15;Initial Catalog=InventoryContext;User ID=username;Password=password;Trusted_Connection=False;";
            using (SqlConnection connection = new SqlConnection(stringConnection))
            {
                var contacts = InsertContacts(UserId => Convert.ToInt32(contact.UserId)).ToString();
                foreach (var userContact in contacts.Where(x => x.UserId == contact.UserId).FirstOrDefault()) //x.UserId is highlighted: Error  CS1061 ‘char’ does not contain a definition for ‘UserId’ and no accessible extension method ‘UserId’ accepting first argument of type ‘char’ could be found (are you missing a directive or an assembly reference?)
                {
                    if (userContact == null)
                        return false;
                    else
                        return true;
                }
                contacts.ToString();

                //using (SQLiteConnection connection = new SQLiteConnection(App.DatabasePath))
                //{
                //var contacts = connection.Table<Contact>();
                //var userContact = contacts.Where(x => x.UserId == contact.UserId).FirstOrDefault();
                //if (userContact == null)
                //    return false;
                //else
                //    return true;
                //}
            }
        }

public static int GetContactsCount()  //Error CS1061 ‘Contact.GetContactsCount()’:not all code paths return a value’
        {
            string stringConnection = "Data Source=190.165.2.15;Initial Catalog=InventoryContext;User ID=username;Password=password;Trusted_Connection=False;";
            using (SqlConnection connection = new SqlConnection (stringConnection))
            {
                SqlConnection sqlConnection = new SqlConnection(stringConnection);
                sqlConnection.Open();
                SqlCommand sqlCommand = new SqlCommand("SELECT COUNT(*) FROM [dbo].[tblContact]");
                sqlCommand.Connection = sqlConnection;

                int RecordCount = Convert.ToInt32(sqlCommand.ExecuteNonQuery());
                Console.WriteLine("Contact", sqlCommand);


                //Console.WriteLine(connection.Table<Contact>().Count());
                //return connection.Table<Contact>().Count();
            }
        }


Developer technologies .NET .NET MAUI
Developer technologies .NET Other
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-03-13T08:30:59.71+00:00

    Hello,

    //Error CS1061 //‘Contact.CheckIfUserExists(Contact)’:not all code paths return a value’ //Error CS1061 //‘Contact.GetContactsCount()’:not all code paths return a value’

    For the Not All Code Paths Return a Value error message, it says that your program will not have a return value in all cases.

    For example, in the CheckIfUserExists method, if contacts is empty and foreach is not executed, the method has no return value.

    And in the GetContactsCount method you commented out the return statement, which must have a corresponding return value for non-void methods.

    //x.UserId is highlighted: Error //CS1061 ‘char’ does not contain a definition for ‘UserId’ and no //accessible extension method ‘UserId’ accepting first argument of type //‘char’ could be found (are you missing a directive or an assembly //reference?)

    This problem occurs because your InsertContacts method returns a string, which causes contacts to be a string instead of a list about Contacts, so the x type is char.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.