2

At the beginning of my journey with C#, I am developing this simple Flashcard app. It can be used for learning vocabulary in new language.

I have created Flashcard class, also have been able to create functions allowing users to enter new words, revise them and play simple guessing game.

When creating new words, program ask user how many one, would like to create. Then uses object of Flashcard function n times, filling list with them.

After every operation like this, I'd like to insert new words, and its translation into a SQL Server database, although I've encountered first problem.

Flashcard.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlashCardApp
{
class Flashcard
{
    private string Word;
    private string Translation;
    private string Description;
    public bool IsKnownTemporarly;

    public Flashcard(string word, string translation, string description)
    {
        Description = description;
        Word = word;
        Translation = translation;
        IsKnownTemporarly = false;
    }

    public Flashcard()
    {
        Description = "Default description";
        Translation = "Defualt translation";
        Word = "Default word";
        IsKnownTemporarly = false;
    }

    public Flashcard(Flashcard flashcard)
    {
        Description = flashcard.Description;
        Word = flashcard.Word;
        Translation = flashcard.Translation;
        IsKnownTemporarly = flashcard.IsKnownTemporarly;
    }

    public string returnWord()
    {
        return Word;
    }

    public string returnDescription()
    {
        return Description;
    }

    public string returnTranslation()
    {
        return Translation;
    }

    public void CreateNewFlashcard()
    {
        Console.Write ("Word => ");
        Word = Console.ReadLine();
        Word = Word.ToLower();
        Console.Write("Description => ");
        Description =  Description = Console.ReadLine();
        Description = Description.ToLower();
        Console.Write("Translation => ");
        Translation = Console.ReadLine();
        Translation = Translation.ToLower();
        IsKnownTemporarly = false;
        Console.Clear();
    }

    public void DisplayFlaschard()
    {
        Console.WriteLine(Word + " means " + Translation + " [" + Description + "]");
        Console.ReadKey();
    }

    public void GuessWord()
    {
        Console.WriteLine("Do you remember this one? [" + Word + "]");
        
        string userInput = Console.ReadLine();

        if (userInput.ToLower() == Translation)
        {
            this.IsKnownTemporarly = true;
            Console.WriteLine("Indeed, that is correct!");
       
            Console.ReadKey();
            Console.Clear();
        }
        else
        {
            Console.WriteLine("I'll help you this time, try to memorize!");
            Console.WriteLine(Word + " means " + Translation + " [" + Description + "]");
            this.IsKnownTemporarly = false;

            Console.ReadKey();
            Console.Clear();
        }
    }
  }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace FlashCardApp
{
class Program
{
    static void Main(string[] args)
    {
        List<Flashcard> FlashcardsList = new List<Flashcard>();

        Flashcard flashcard1 = new Flashcard("Car",  "auto" ,"it drives 
through streets");
        Flashcard flashcard2 = new Flashcard("street",  "droga" , "Lead you 
around cities");
        Flashcard flashcard3 = new Flashcard("screen", "ekran", "It displays 
info");
        Flashcard flashcard4 = new Flashcard("stapler",  "zszywacz", 
"costam");
        Flashcard flashcard5 = new Flashcard("paper",  "papier", "costam");
        Flashcard flashcard6 = new Flashcard("pen", "dlugopis", "dligpsfs");

        FlashcardsList.Add(flashcard1);
        FlashcardsList.Add(flashcard2);
        FlashcardsList.Add(flashcard3);
        FlashcardsList.Add(flashcard4);
        FlashcardsList.Add(flashcard5);
        FlashcardsList.Add(flashcard6);

        ConnectDB();
    }

    public static void ConnectDB()
    {
        string connectionString;
        SqlConnection conn;

        connectionString = @"Data Source=DESKTOP-RDM63QN\SQLEXPRESS; Initial 
Catalog=Fishcards; User ID=MyName ;Password=MyPassword" ;

        conn = new SqlConnection(connectionString);
        conn.Open();
        Console.WriteLine("Done!");
        conn.Close();
    }

    public static void AddNewFlaschards(List<Flashcard> FlashcardsList)
    {
        Console.WriteLine("ADD NEW FLASCHARDS!");
        Console.WriteLine("How many would you like to add?");

        int count = Convert.ToInt32(Console.ReadLine());

        Console.Clear();

        for (int i = 0; i < count; i++)
        {
             Flashcard flashcard = new Flashcard();

             Console.WriteLine("No. " + (i+1));
             flashcard.CreateNewFlashcard();
             FlashcardsList.Add(new Flashcard(flashcard));
        }

        Console.Read();
        Console.Clear();
    } 

    public static void ReviseFlashcards(List<Flashcard> FlashcardsList)
    {
        Console.WriteLine("REVISE YOUR SAVED FLASHCARDS!");
        FlashcardsList.ForEach(fl => fl.DisplayFlaschard());
        Console.Read();
        Console.Clear();
    }

    public static bool IsThereAnyUnknownLeft(List<Flashcard> FlashcardsList)
    {
        int var = FlashcardsList.Count; // Merging the size of FlashcardList
        int i = 0;
        int sum = 0;

        int[] array = new int[var];

        foreach (Flashcard flashcard in FlashcardsList)
        {
            if(flashcard.IsKnownTemporarly == false)
            {
                array[i] = 0;
            }
            else
            {
                array[i] = 1;
            }
            i++;
        }

        for(int a = 0; a<var; a++)
        {
            sum = sum + array[a];
        }

        if (sum == var)
            return true;
        else
            return false;
    }

    public static void PlayGuessingGame(List<Flashcard> FlashcardsList)
    {
        while(!IsThereAnyUnknownLeft(FlashcardsList))
        {
            foreach (Flashcard flashcard in FlashcardsList.Where(flashcard 
 => flashcard.IsKnownTemporarly == false))
            {
                flashcard.GuessWord();
            }
        }

        Console.Read();
        Console.Clear();
    }
}
}

Problem is that Visual Studio returns an error on conn.Open() with this info:

System.Data.SqlClient.SqlException: Cannot open database "Fishcards" requested by the login. The login failed.

Login failed for user 'MyNameisHere'.

Image

Community
  • 1
  • 1
mSwierkot
  • 31
  • 3
  • Also, Could I ask for some comments on my code ? – mSwierkot Nov 14 '18 at 20:37
  • Possible duplicate of [Cannot open database “test” requested by the login. The login failed. Login failed for user 'xyz\ASPNET'](https://stackoverflow.com/q/2575907/1260204) – Igor Nov 14 '18 at 20:44
  • 2
    Maybe a typo in `Fishcards`? Should it be `Flashcards`? – Rubens Farias Nov 14 '18 at 20:47
  • Did you try windows authentication instead? @"Data Source=.\SQLEXPRESS; Database=Flashcards; Trusted_Connection=yes" – Cetin Basoz Nov 14 '18 at 20:49
  • Can you open your DB in SSMS with the same login/pass? – Miamy Nov 14 '18 at 20:51
  • Yes, that is possible. Connection is made when I do it through SSMS – mSwierkot Nov 14 '18 at 20:56
  • I have changed connection string: connectionString = "Server=.\\SQLExpress;Database=Flashcard;User ID=MarekSwierkot;pwd=qtpf"; But still cannot connect – mSwierkot Nov 14 '18 at 20:57
  • 1
    It seems clearly about the part "Initial Catalog=Fishcards; User ID=MyName ;Password=MyPassword" – aliassce Nov 14 '18 at 21:06
  • According to comments you can connect via SSMS using SQL Authentication with your username and password and see the database "Fishcards" listed. After that can you explained tables under that database and do a simple select on one of the tables? – Tim Mylott Nov 14 '18 at 21:30
  • @TimMylott, I think, you meant "*expanded* tables" ;) – Miamy Nov 14 '18 at 21:36
  • @Miamy, oops you're right :), typing too fast. Expand tables is what I meant. Also I'm a little unclear on database name. Could we get a screenshot of that? Reading all the comments I'm confused if it's called "Fishcards" or "Flashcard" as the error says one thing, and multiple iterations of the connection string are showing different. I feel like it's either permissions, connection string or possibly both. – Tim Mylott Nov 14 '18 at 21:40
  • Apparently, it was all because some stupid inside this ConnectDB function. Anyway thank you all for trying to help :) – mSwierkot Nov 17 '18 at 08:03

0 Answers0