I tried this and a couple of other things but the main problem I am phasing is that I am getting a connection error in sql server browser
using MySqlConnector;
using System;
using System.Data.SqlClient;
using Xamarin.Forms;
namespace JersonVillaceran
{
public partial class Login : ContentPage
{
public Login()
{
InitializeComponent();
testConnection();
}
private void testConnection()
{
string connectionString = "Server=DESKTOP-7RTEHS2\\SQLEXPRESS;Database=MCC;Uid=;Pwd=;";
SqlConnection connection = new SqlConnection(connectionString);
try
{
connection.Open();
Console.WriteLine("Successfully connected to databse");
}
catch (Exception ex)
{
Console.WriteLine("Successfully connected to databse");
}
finally
{
if (connection.State == System.Data.ConnectionState.Open)
{
connection.Close();
}
}
}
private void insertUser()
{
string connectionString = "Server=DESKTOP-7RTEHS2\\SQLEXPRESS;Database=MCC;Integrated Security=True;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string query = "INSERT INTO users (username, password) VALUES (username, Password)";
SqlCommand cmd = new SqlCommand(query, connection);
string userName = txtUsername.Text;
string userPass = txtPassword.Text;
cmd.Parameters.AddWithValue("username", userName);
cmd.Parameters.AddWithValue("Password", userPass);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
Console.WriteLine("User inserted successfully.");
}
else
{
Console.WriteLine("Failed to insert user.");
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
private void OnLoginButtonClicked(object sender, EventArgs e)
{
string username = txtUsername.Text;
string password = txtPassword.Text;
// Validate username and password
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
// Display error message
DisplayAlert("Error", "Please enter both username and password.", "OK");
return;
}
string constring = "Server=DESKTOP-7RTEHS2\\SQLEXPRESS;Database=MCC;Uid=;Pwd=;";
SqlConnection connection = new SqlConnection(constring);
{
try
{
connection.Open();
// Construct the query
string queryString = "SELECT Id, username, password FROM users WHERE username = Username and password = Password";
// Create a SqlCommand object with the query and connection
using (SqlCommand command = new SqlCommand(queryString, connection))
{
// Add parameters to the command
command.Parameters.AddWithValue("username", username);
command.Parameters.AddWithValue("Password", password);
// Execute the query
SqlDataReader reader = command.ExecuteReader();
// Check if any rows were returned
if (reader.HasRows)
{
// Successful login
Console.WriteLine("Login successful. Redirecting to dashboard...");
// Redirect to the dashboard page
App.Current.MainPage.Navigation.PushAsync(new Dashboard());
}
else
{
// Invalid username or password
DisplayAlert("Error", "Invalid username or password.", "OK");
}
// Close the reader
reader.Close();
}
}
catch (Exception ex)
{
// Handle any exceptions
Console.WriteLine("Error: " + ex.Message);
DisplayAlert("Error", "An error occurred while logging in.", "OK");
}
}
}
}
}
using MySqlConnector;
using System;
using System.Data.SqlClient;
using Xamarin.Forms;
namespace JersonVillaceran
{
public partial class Login : ContentPage
{
public Login()
{
InitializeComponent();
testConnection();
}
private void testConnection()
{
string connectionString = "Server=DESKTOP-7RTEHS2\\SQLEXPRESS;Database=MCC;Uid=;Pwd=;";
SqlConnection connection = new SqlConnection(connectionString);
try
{
connection.Open();
Console.WriteLine("Successfully connected to databse");
}
catch (Exception ex)
{
Console.WriteLine("Successfully connected to databse");
}
finally
{
if (connection.State == System.Data.ConnectionState.Open)
{
connection.Close();
}
}
}
private void insertUser()
{
string connectionString = "Server=DESKTOP-7RTEHS2\\SQLEXPRESS;Database=MCC;Integrated Security=True;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string query = "INSERT INTO users (username, password) VALUES (username, Password)";
SqlCommand cmd = new SqlCommand(query, connection);
string userName = txtUsername.Text;
string userPass = txtPassword.Text;
cmd.Parameters.AddWithValue("username", userName);
cmd.Parameters.AddWithValue("Password", userPass);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
Console.WriteLine("User inserted successfully.");
}
else
{
Console.WriteLine("Failed to insert user.");
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
private void OnLoginButtonClicked(object sender, EventArgs e)
{
string username = txtUsername.Text;
string password = txtPassword.Text;
// Validate username and password
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
// Display error message
DisplayAlert("Error", "Please enter both username and password.", "OK");
return;
}
string constring = "Server=DESKTOP-7RTEHS2\\SQLEXPRESS;Database=MCC;Uid=;Pwd=;";
SqlConnection connection = new SqlConnection(constring);
{
try
{
connection.Open();
// Construct the query
string queryString = "SELECT Id, username, password FROM users WHERE username = Username and password = Password";
// Create a SqlCommand object with the query and connection
using (SqlCommand command = new SqlCommand(queryString, connection))
{
// Add parameters to the command
command.Parameters.AddWithValue("username", username);
command.Parameters.AddWithValue("Password", password);
// Execute the query
SqlDataReader reader = command.ExecuteReader();
// Check if any rows were returned
if (reader.HasRows)
{
// Successful login
Console.WriteLine("Login successful. Redirecting to dashboard...");
// Redirect to the dashboard page
App.Current.MainPage.Navigation.PushAsync(new Dashboard());
}
else
{
// Invalid username or password
DisplayAlert("Error", "Invalid username or password.", "OK");
}
// Close the reader
reader.Close();
}
}
catch (Exception ex)
{
// Handle any exceptions
Console.WriteLine("Error: " + ex.Message);
DisplayAlert("Error", "An error occurred while logging in.", "OK");
}
}
}
}
}
Attachments
-
You do not have permission to view the full content of this post. Log in or register now.