- Joined
- Oct 16, 2016
- Posts
- 243
- Solutions
- 1
- Reaction
- 257
- Points
- 188
Hello, sinubukan kopo pag aralan at gumawa po ng mobile app sa Maui .net with Mongodb pero tama naman po yung connectionString ko pero ito po yung error ano po kaya yung error dito? [DOTNET] Error during admin insertion: The list of configured name servers must not be empty. (Parameter 'servers')
this is my DBConnection class
this is my SignupViewModel
sinubukan kona po idebug pero ayaw parin mag mag store ng data saking mongodb database
this is my DBConnection class
Code:
public class DBConnection
{
private IMongoDatabase _database;
public DBConnection()
{
try
{
var mongoClient = new MongoClient("mongodb+srv://<username>:<password>@cluster0.4jhaesp.mongodb.net/?retryWrites=true&w=majority");
_database = mongoClient.GetDatabase("homefinder");
Console.WriteLine("Connection to the database is successful.");
}
catch (MongoException ex)
{
Console.WriteLine($"Error during database connection: {ex.Message}");
throw;
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error during database connection: {ex}");
throw;
}
}
public IMongoDatabase GetDatabase()
{
if (_database == null)
{
throw new InvalidOperationException("Database is not initialized. Make sure DBConnection constructor is called successfully.");
}
return _database;
}
}
this is my SignupViewModel
Code:
public class Admins
{
[BsonId]
public ObjectId Id { get; set; }
public string Fullname { get; set; }
public string Username { get; set; }
public string PasswordHash { get; set; }
public string Role { get;set; }
}
public class SignupViewModel : BindableObject
{
private string _fullname;
private string _username;
private string _password;
private string _selectedRole;
public string Fullname
{
get => _fullname;
set
{
_fullname = value;
OnPropertyChanged(nameof(Fullname));
}
}
public string Username
{
get => _username;
set
{
_username = value;
OnPropertyChanged(nameof(Username));
}
}
public string Password
{
get => _password;
set
{
_password = value;
OnPropertyChanged(nameof(Password));
}
}
public string SelectedRole
{
get => _selectedRole;
set
{
_selectedRole = value;
OnPropertyChanged(nameof(SelectedRole));
}
}
private async Task Signup(string password)
{
try
{
if (string.IsNullOrWhiteSpace(Fullname) || string.IsNullOrWhiteSpace(Username) || string.IsNullOrWhiteSpace(Password))
{
Console.WriteLine("Please fill in all required fields.");
return;
}
var hashedPassword = BCrypt.Net.BCrypt.HashPassword(Password);
var admin = new Admins
{
Fullname = Fullname,
Username = Username,
PasswordHash = hashedPassword,
Role = SelectedRole
};
await InsertAdminAsync(admin);
await App.Current.MainPage.Navigation.PushAsync(new MainPage());
}
catch (Exception ex)
{
Console.WriteLine($"Error during signup: {ex.Message}");
}
}
private async Task InsertAdminAsync(Admins admin)
{
try
{
var dbConnection = new DBConnection();
var database = dbConnection.GetDatabase();
var adminsCollection = database.GetCollection<Admins>("admins");
await adminsCollection.InsertOneAsync(admin);
}
catch (Exception ex)
{
Console.WriteLine($"Error during admin insertion: {ex.Message}");
}
}
public Command<string> SignupCommand => new Command<string>(async (password) => await Signup(password));
}
Code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:homefinderpro.ViewModels"
x:Class="homefinderpro.Page.SignupPage"
Title="SignupPage">
<StackLayout Background="#4F59DE" Padding="30">
<Label Text="SIGN UP"
TextColor="White" FontFamily="Poppins-Bold" FontSize="30"
HorizontalOptions="Center" />
<Frame BackgroundColor="White" Padding="20" CornerRadius="10">
<StackLayout>
<Entry x:Name="FullnameEntry" Placeholder="Full Name" Text="{Binding Fullname}" />
<Entry x:Name="UsernameEntry" Placeholder="Username" Text="{Binding Username}" />
<Entry x:Name="PasswordEntry" Placeholder="Password" IsPassword="True" Text="{Binding Password}"/>
<Picker x:Name="RolePicker" Title="Select Role" SelectedItem="{Binding SelectedRole, Mode=TwoWay}"/>
<Button Text="SIGN UP"
Background="Orange" TextColor="White"
Command="{Binding SignupCommand}"/>
</StackLayout>
</Frame>
<Button Text="Already have an account? Login"
BackgroundColor="Orange" TextColor="White"
Margin="0,20,0,0"/>
</StackLayout>
</ContentPage>
sinubukan kona po idebug pero ayaw parin mag mag store ng data saking mongodb database