-2

Hello Stackoverflow Community,Im new in C# and databanks and I have the following problem:

I am writing a windows forms application, one of the forms is a logon-screen where the user can type his name into a text field. I want to check if the name already exists in my database and dont know how to hand over the textfield information to the database.

I saw a few tutorials, but they are all working with the SqlConnectionClass. I connected my project with the database through the Visual Studio surface (View > Other Windows > Data Sources).

Can you tell me how I can solve this problem ?

Riccardo
  • 1,083
  • 2
  • 15
  • 25
Antoine
  • 21
  • 8
  • 4
    Hi and welcome to Stack Overflow. You should find a tutorial on how to work with databases, the best answer I can give is that you need to write code that involves using `SqlConnection` or something that uses it, but the question is far too broad and vague as it stands now, anything I write will likely just spark new questions. Can you take this article for a spin and see if it works for you? [Walkthrough: Creating a Simple Data Application](https://msdn.microsoft.com/en-us/library/ms171884.aspx). – Lasse V. Karlsen Jun 22 '18 at 08:36

1 Answers1

0

i have no idea about your window form design but i hope following code will help :)

SqlConnection conn = new SqlConnection(@"here goes your connection string"); // select your data source goto properties and you may see your connection string over there
        SqlDataAdapter sda = new SqlDataAdapter("select count(*) from your_table_name where username ='" + textBox1.Text + "' and password='" + textBox2.Text + "'", conn);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        if (dt.Rows[0][0].ToString() == "1")
        {
            this.Hide();
            Form2 f = new Form2();
            f.Show();
        }
        else
        {
            MessageBox.Show("please enter correct username and password", "alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }