Direct connection between Android app and MS SQL database

GNA1996GNA1996 Member ✭✭

Hello everyone,

I am working on an app that saves the username and password at signup into the database. I am using a connection string to establish a connection between the Android app and MS SQL database. The connection string works fine when I tested in on Windows Form, but I am getting an exception error when I used it in Xamarin.

This is the error:

System.Data.SqlClient.SqlException (0x80131904): Snix_Connect (provider: SNI_PN7, error: 35 - SNI_ERROR_35)

This is the code:
void InsertInfo(string userPar, string passPar)
{
//The connection string and query to input data in database.
string conString = ("Data Source=xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx");
string queryString = ("INSERT INTO dbo.signup(Username,Password) VALUES (@user,@pass)");

        try
        {
            using (SqlConnection connection = new SqlConnection(conString))
            {
                // To open connection if the connection is closed.
                if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();                        
                }

                using (SqlCommand query = new SqlCommand(queryString, connection))
                {
                    query.Parameters.AddWithValue("@user", userPar);
                    query.Parameters.AddWithValue("@pass", passPar);
                    query.ExecuteNonQuery();
                    tvTips.Text = "Successful Signup";
                }

            }
        }
        catch (SqlException ex)
        {
            StringBuilder errorMessages = new StringBuilder();

            for (int i = 0; i < ex.Errors.Count; i++)
            {
                // To show the error message.
                errorMessages.Append("Index #" + i + "\n" +
                    "Message: " + ex.Errors[i].Message + "\n" +
                    "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                    "Source: " + ex.Errors[i].Source + "\n" +
                    "Procedure: " + ex.Errors[i].Procedure + "\n");

                tvTips.Text = errorMessages.ToString();
            }
        }

I have looked at similar questions in the forum regarding this and most suggests using Web Service/API. I am aware that connecting the app directly to an Android app is not the right way of doing things, but I am just a beginner and the app is not going to be published. At the moment, I really just want to learn how to establish a connection with the SQL database directly on Xamarin. Also I only added the System.Data and System.Data.SqlClient references in the solution, I am wondering if there are additional things I have to do? I would greatly appreciate it if anyone could take a look at the code and point me in the right direction in fixing the error. Thanks.

Sign In or Register to comment.