I am new to microsoft visual studio xamarin I wanted to create a form with data source,username,database and password input test fields with a connect button to connect to a microsoft sql server database
I have created the interfaces to connect to the sqlserver database but encountered some issues.
strings.xml
<?xml version = "1.0" encoding = "utf-8"?>
<resources>
<string name = "HelloXamarin">Server</string>
<string name = "ApplicationName">connect</string>
<string name = "app_name">connect</string>
<string name = "ButtonClick">Connect</string>
<string name = "username">Username</string>
<string name = "database">Database</string>
<string name = "password">Password</string>
</resources>
activity_main.axml
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:background = "#d3d3d3"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
<TextView
android:text = "@string/HelloXamarin"
android:textAppearance = "?android:attr/textAppearanceLarge"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:id = "@+id/textView2"
android:textColor = "@android:color/black" />
<EditText
android:id="@+id/plain_text_input"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="text"/>
<TextView
android:text = "@string/database"
android:textAppearance = "?android:attr/textAppearanceLarge"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:id = "@+id/database"
android:textColor = "@android:color/black" />
<EditText
android:id="@+id/database_text_input"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="text"/>
<TextView
android:text = "@string/username"
android:textAppearance = "?android:attr/textAppearanceLarge"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:id = "@+id/username"
android:textColor = "@android:color/black" />
<EditText
android:id="@+id/username_text_input"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="text"/>
<TextView
android:text = "@string/password"
android:textAppearance = "?android:attr/textAppearanceLarge"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:id = "@+id/password"
android:textColor = "@android:color/black" />
<EditText
android:id="@+id/password_text_input"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="text"/>
<Button
android:id = "@+id/MyButton"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "@string/ButtonClick" />
</LinearLayout>
Mainactivity.Cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Data.SqlClient;
//using System.Data.Sql;
namespace first
{
[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.activity_main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate {
// button.Text = "Hello world I am your first App";
string connectionString = @"Server='test';Database='connect';User Id='test';Password='test';Trusted_Connection=true";
string databaseTable = "Connect";
//string referenceAccountNumber = "0001134919";
string selectQuery = String.Format("SELECT * FROM {0} ", databaseTable);
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
//open connection
connection.Open();
button.Text = "Connection Successful";
SqlCommand command = new SqlCommand(selectQuery, connection);
command.Connection = connection;
command.CommandText = selectQuery;
var result = command.ExecuteReader();
//check if account exists
var exists = result.HasRows;
}
}
catch (Exception exception)
{
#region connection error
AlertDialog.Builder connectionException = new AlertDialog.Builder(this);
connectionException.SetTitle("Connection Error");
connectionException.SetMessage(exception.ToString());
connectionException.SetNegativeButton("Return", delegate { });
connectionException.Create();
connectionException.Show();
#endregion
}
};
}
}
}
However the program gives an error that the type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference)
But i already have defined the assembly
using System.Data.SqlClient;
Anyone has any idea what is wrong?