Login Box with VC++ and SQL Server

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 14150 Views - Last Post: 13 November 2011 - 10:26 AM Rate Topic: -----

#1 winni_07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 11

Login Box with VC++ and SQL Server

Posted 01 October 2011 - 05:33 AM

Hi @all,

my name is Ben and I'm new here. I'm from Germany, so please don't observe my English to strictly ;-)

I want to create a Login Box in Visual C++. There will be a windows form application with two TextBoxes. At the first one, you have to write the username and at the second the password. Than the programm will connect to an SQL Server and will compare if the username and password exist. If this will be true the program opens an new window like a member area or something like this.

I started with the code but something won't be working alright, but there won't be any errors from the compiler.

#pragma once

namespace Touch_Test_2 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Windows::Forms;
	using namespace System::Data::SqlClient;

	/// <summary>
	/// Zusammenfassung für Login_Test
	/// </summary>
	public ref class Login_Test : public System::Windows::Forms::Form
	{
		String ^verbindungsstr;
		SqlConnection ^verbindung;
		SqlCommand ^befehl;
		SqlDataReader ^leser;

		array<String^> ^dbNachname;
		array<String^> ^dbVorname;
                array<String^> ^dbPasswort;


	private: System::Windows::Forms::TextBox^  tb_Name;
	private: System::Windows::Forms::TextBox^  tb_Passwort;
	private: System::Windows::Forms::Button^  btn_Anmelden;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
			 
	public:
		Login_Test(void)
		{
			InitializeComponent();
			//
			verbindungsstr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Konten.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
			verbindung = nullptr;
			befehl = nullptr;
			leser = nullptr;

			dbNachname = gcnew array<String^>(16);
			dbVorname = gcnew array<String^>(16);
			dbPasswort = gcnew array<String^>(16);

			try
			{
				// Verbindung herstellen
				verbindung = gcnew SqlConnection(verbindungsstr);
				verbindung->Open();

				// Daten aus Datenbank abfragen 
				befehl = verbindung->CreateCommand();
				befehl->CommandText = "SELECT count(*) FROM konto WHERE Nachname='Wynands' and Passwort='abc'";
				leser = befehl->ExecuteReader();

				dbNachname[0] = leser->GetString(1);
				dbVorname[0] = leser->GetString(2);
				dbPasswort[0] = leser->GetString(3);
			}
			catch (SqlException ^e)
         {
             MessageBox::Show(e->Message, "Datenbankfehler");
         }
         catch (Exception ^e)
         {
             MessageBox::Show(e->Message, "Datenbankfehler");
         }
         finally  
         {        
             if (leser != nullptr)
                 leser->Close();
             if (verbindung != nullptr)
                 verbindung->Close();
         }
			//
		}

	protected:
		/// <summary>
		/// Verwendete Ressourcen bereinigen.
		/// </summary>
		~Login_Test()
		{
			if (components)
			{
				delete components;
			}
		}

	private:
		/// <summary>
		/// Erforderliche Designervariable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Erforderliche Methode für die Designerunterstützung.
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
		/// </summary>
		void InitializeComponent(void)
		{
			this->tb_Name = (gcnew System::Windows::Forms::TextBox());
			this->tb_Passwort = (gcnew System::Windows::Forms::TextBox());
			this->btn_Anmelden = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// tb_Name
			// 
			this->tb_Name->Location = System::Drawing::Point(158, 73);
			this->tb_Name->Name = L"tb_Name";
			this->tb_Name->Size = System::Drawing::Size(215, 20);
			this->tb_Name->TabIndex = 0;
			// 
			// tb_Passwort
			// 
			this->tb_Passwort->Location = System::Drawing::Point(158, 148);
			this->tb_Passwort->Name = L"tb_Passwort";
			this->tb_Passwort->Size = System::Drawing::Size(215, 20);
			this->tb_Passwort->TabIndex = 1;
			// 
			// btn_Anmelden
			// 
			this->btn_Anmelden->BackColor = System::Drawing::Color::Transparent;
			this->btn_Anmelden->FlatAppearance->BorderSize = 3;
			this->btn_Anmelden->FlatAppearance->MouseDownBackColor = System::Drawing::Color::Olive;
			this->btn_Anmelden->FlatAppearance->MouseOverBackColor = System::Drawing::Color::DarkKhaki;
			this->btn_Anmelden->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
			this->btn_Anmelden->Font = (gcnew System::Drawing::Font(L"Magneto", 9, static_cast<System::Drawing::FontStyle>((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)), 
				System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0)));
			this->btn_Anmelden->ForeColor = System::Drawing::Color::DarkRed;
			this->btn_Anmelden->Location = System::Drawing::Point(151, 202);
			this->btn_Anmelden->Margin = System::Windows::Forms::Padding(4, 3, 4, 3);
			this->btn_Anmelden->Name = L"btn_Anmelden";
			this->btn_Anmelden->Size = System::Drawing::Size(165, 27);
			this->btn_Anmelden->TabIndex = 4;
			this->btn_Anmelden->Text = L"Anmelden";
			this->btn_Anmelden->UseVisualStyleBackColor = false;
			this->btn_Anmelden->Click += gcnew System::EventHandler(this, &Login_Test::btn_Anmelden_Click);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(27, 81);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(35, 13);
			this->label1->TabIndex = 2;
			this->label1->Text = L"Name";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(27, 147);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(52, 13);
			this->label2->TabIndex = 3;
			this->label2->Text = L"Kennwort";
			// 
			// Login_Test
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(466, 262);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->btn_Anmelden);
			this->Controls->Add(this->tb_Passwort);
			this->Controls->Add(this->tb_Name);
			this->Name = L"Login_Test";
			this->Text = L"Login_Test";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
   private: 
      void btn_Anmelden_Click(Object^  sender, EventArgs^  e) 
      {
		  // Wenn eins oder beide Eingabefelder leer sind
         if (tb_Name->Text == "" || tb_Passwort->Text == "")
         {
            MessageBox::Show("Sie müssen beide Eingabefelder bearbeiten", "Meldung");
         }
		 // Wenn der Benutzername und das Kennwort richtig sind
         if (tb_Name->Text==dbNachname[0] && (tb_Passwort->Text == dbPasswort[0] ))
         {
            Form^ _DB = gcnew Login_Test;
			_DB->Show();
         }
		 // Wenn Benutzername und Kennwort nicht übereinstimmen
		 if (tb_Name->Text != tb_Passwort->Text)
		 {
			 MessageBox::Show("Ihr Benutzername und/oder Kennwort ist falsch !!!", "ACHTUNG !!!");
		 }

      }	

	};
}



Is This A Good Question/Topic? 0
  • +

Replies To: Login Box with VC++ and SQL Server

#2 Ionut   User is offline

  • D.I.C Lover
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Login Box with VC++ and SQL Server

Posted 01 October 2011 - 09:51 AM

First of all, you have only one column in your sql statement, so GetString(2) and GetString(3) are not going to return anything. Plus I think that the first position in a reader is 0 (GetString(0)).
Another thing, Count(*) returns a scalar value => all rows from your table. A reader executed with that command will have only one row.
What you have to do:
1. Execute your command with ExecuteScalar. This returns an integer.
2. Check if the integer is 0 or 1. If 0, no account, if 1, the account exists. Proceed.
Was This Post Helpful? 0
  • +
  • -

#3 winni_07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 11

Re: Login Box with VC++ and SQL Server

Posted 03 October 2011 - 06:04 AM

Thanks for your answer. Do you mean something like this:

System::Object ^_object = ExecuteScalar(verbindung,"SELECT count(*) FROM Konto WHERE Nachname='Wynands' and Passwort='abc'" );

if (_object != nullptr)
         {


Was This Post Helpful? 0
  • +
  • -

#4 Ionut   User is offline

  • D.I.C Lover
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Login Box with VC++ and SQL Server

Posted 03 October 2011 - 06:49 AM

you don't have to use a variable of type Object. Put it int.
int count = .. //what you wrote
if (count == 0)
//do something
else 
//do something else


Was This Post Helpful? 0
  • +
  • -

#5 winni_07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 11

Re: Login Box with VC++ and SQL Server

Posted 03 October 2011 - 08:12 AM

if I make it like this:

[code]int count = ExecuteScalar("SELECT * FROM bndlaender WHERE Nachname='Wynands' and Passwort='abc'");[\code]

I get an error:

Error C2660 Function does not take 1 argument
Was This Post Helpful? 0
  • +
  • -

#6 winni_07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 11

Re: Login Box with VC++ and SQL Server

Posted 03 October 2011 - 08:48 AM

I've changed into that:

befehl->CommandText = "SELECT * FROM bndlaender WHERE Nachname='Wynands' and Passwort='abc'";
int count = (int)ExecuteScalar();
         if (count != 0)
         {



but now get an error like this:

error LNK2020 "unresolved token"
Was This Post Helpful? 0
  • +
  • -

#7 Ionut   User is offline

  • D.I.C Lover
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Login Box with VC++ and SQL Server

Posted 03 October 2011 - 10:45 AM

Now I saw the error. ExecuteScalar is method of Command object
int count = befehl->ExecuteScalar();


Was This Post Helpful? 0
  • +
  • -

#8 winni_07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 11

Re: Login Box with VC++ and SQL Server

Posted 04 October 2011 - 10:57 AM

Hi, now I made it like yours, but I get another error. In German it like this:

error C2440: 'Initialisierung': 'System::Object ^' kann nicht in 'int' konvertiert werden

It means that the 'System::Object' can't convert to 'int'
Was This Post Helpful? 0
  • +
  • -

#9 Ionut   User is offline

  • D.I.C Lover
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Login Box with VC++ and SQL Server

Posted 04 October 2011 - 12:17 PM

Quote

public:
virtual Object^ ExecuteScalar() override

Ok, I made an error. ExecuteScalar returns an object, but you can explicitly convert it to int.

int count =(int)Command.ExecuteScalar()


I'm not sure this is correct in VC++, it's been a while since I did something with this language.
Using .NET Framework, you have Convert class that has some static methods. ToInt32 is one of them.
int count = Convert.ToInt32(Command.ExecuteScalar())


Was This Post Helpful? 0
  • +
  • -

#10 winni_07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 11

Re: Login Box with VC++ and SQL Server

Posted 04 October 2011 - 12:29 PM

He give me another error again. Will there something I have to include, before I could you the Convert.ToInt32 ??

Because I get this one:

warning C4832: Das Token '.' ist nach UDT 'System::Convert' illegal

or this one for this option:

int count = (int)Command.ExecuteScalar();

error c2065 'Command' undeclared identifier
Was This Post Helpful? 0
  • +
  • -

#11 Ionut   User is offline

  • D.I.C Lover
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Login Box with VC++ and SQL Server

Posted 04 October 2011 - 12:32 PM

It would be great if you can keep translating the errors. I'm planning in taking a course in german, but it is a long term idea :)
What I posted was C# code, in C++ is
   Int32 iNumber = Convert::ToInt32( dNumber );



If you get a hint(class name, namespace, anything), go to MSDN, they have explained everything there.
Convert on MSDN

This post has been edited by Ionut: 04 October 2011 - 12:32 PM

Was This Post Helpful? 0
  • +
  • -

#12 winni_07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 11

Re: Login Box with VC++ and SQL Server

Posted 04 October 2011 - 01:06 PM

Oh sorry, I thought I translate both of the error-messages. ;-)

If I write it like this:

int count = Convert::ToInt32(ExecuteScalar());


I get the error LNK 2020 again.
I've read on msdn that there will be missing something.

That's the link to the msdn - site:

http://msdn.microsof...=VS.100%29.aspx
Was This Post Helpful? 0
  • +
  • -

#13 Ionut   User is offline

  • D.I.C Lover
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Login Box with VC++ and SQL Server

Posted 04 October 2011 - 01:15 PM

Again, ExecuteScalar is a method of befehl object.
Correct
 befehl->ExecuteScalar()

This post has been edited by Ionut: 04 October 2011 - 01:15 PM

Was This Post Helpful? 0
  • +
  • -

#14 winni_07   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 11

Re: Login Box with VC++ and SQL Server

Posted 04 October 2011 - 02:03 PM

Ok, but what is the meaning of iNumber and dNumber, if I will use it in my programm ?
Was This Post Helpful? 0
  • +
  • -

#15 Ionut   User is offline

  • D.I.C Lover
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Login Box with VC++ and SQL Server

Posted 04 October 2011 - 02:18 PM

There are variables. iNumber is an integer variable and dNumber a double. You don't have to use them, they are only for exemplification. Name your own variables and work with them.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2