Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to Connect Local Database Server in Button Click With C#.net ? Pin
Mycroft Holmes30-Sep-09 22:45
professionalMycroft Holmes30-Sep-09 22:45 
JokeRe: How to Connect Local Database Server in Button Click With C#.net ? Pin
Greg Chelstowski30-Sep-09 23:09
Greg Chelstowski30-Sep-09 23:09 
GeneralRe: How to Connect Local Database Server in Button Click With C#.net ? Pin
Mycroft Holmes30-Sep-09 23:18
professionalMycroft Holmes30-Sep-09 23:18 
Questionread the changed value from "Workbook_SheetCalculate(object sh)" function for Excel Pin
NarVish30-Sep-09 21:43
NarVish30-Sep-09 21:43 
Questiondatagridview Pin
Anjani Poornima30-Sep-09 21:43
Anjani Poornima30-Sep-09 21:43 
QuestionJava to C# Pin
satsumatable30-Sep-09 21:41
satsumatable30-Sep-09 21:41 
AnswerRe: Java to C# Pin
SeMartens30-Sep-09 22:05
SeMartens30-Sep-09 22:05 
Questionmulti-user winforms application Pin
An Enigma30-Sep-09 21:36
An Enigma30-Sep-09 21:36 
Hi,

What I am trying to achieve is that in the winforms application I am creating (a journal one), it allows multiple users to register/login and add/edit/delete their entries, my questions are:

1. How do I create the application that allows different users to login?

2. Once logged in, how will the application know which user is which and to update the logged in user's particular entries?

Like in PHP you can use sessions do this easily, but how to do it in C#?

I have created a simple register and login form right now, but the difference of user is my problem, I am using the SqlceDataReader but I can only login with the last user that exists in the DB and if a change that user's password, every user's password in the db is changed to that particular password. Is it better to use datasets?

Here are the relevant bits from my code for both winforms:
Main form:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlServerCe;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            MainForm frmLogin = new MainForm();
            frmLogin.ShowDialog();
        }

        private void btnLogin_Click(object sender, System.EventArgs e)
        {
            if (txtuser.Text.Equals(""))
            {
                MessageBox.Show("Please Enter User Name", "Organiser", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtpwd.Text.Equals(""))
            {
                MessageBox.Show("Please Enter Password", "Organiser", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            int count = 0;
           
            con = new SqlCeConnection();
            con.ConnectionString = "Data Source=Database.sdf";
            com = new SqlCeCommand("select * from [User]", con);
            con.Open();
            if (con.State == ConnectionState.Open)
            {
                SqlCeDataReader dtr = com.ExecuteReader();
                while (dtr.Read())
                {
                    us = dtr["username"].ToString();
                    ps = dtr["password"].ToString();
                    if (us == txtuser.Text && ps == txtpwd.Text)
                    {
                        count = 1;                                             

                    }
                    else count = 2;                    
                }
            }            
            if (count == 1)
            {
                MessageBox.Show("Login successful " + us, "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ChangePassword pass = new ChangePassword();
                pass.ShowDialog();

            }
            else if (count == 2)
            {
                MessageBox.Show("WRONG USER NAME OR PASSWORD ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }
        private void btnRegister_Click(object sender, System.EventArgs e)
        {
            this.Close();
            this.Dispose();
            RegisterUser reg = new RegisterUser();
            reg.ShowDialog();
        }
        private void btnExit_Click(object sender, System.EventArgs e)
        {
            this.Close();
            this.Dispose();
            Application.Exit();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
                        
        }

        private void MainForm_Load_1(object sender, EventArgs e)
        {

        }
    }
}


For changing password:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlServerCe;

        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
            this.Dispose();            
        }

        private void btnSave_Click(object sender, System.EventArgs e)
        {
            MainForm mf = new MainForm();
            
            int count = 0;
            string connectionString = "Data Source=..\\..\\Database.sdf";
            SqlCeConnection conn = new SqlCeConnection(connectionString);
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                SqlCeCommand com;
                com = new SqlCeCommand("select * from [User]", conn);
                SqlCeDataReader dtr = com.ExecuteReader();
                while (dtr.Read())
                {
                   string pass = dtr["password"].ToString();
                   username = dtr["username"].ToString();
                   password = txtNew.Text;
                   if (pass == txtOld.Text)
                   {
                       count = 1;
                       string query = "UPDATE [User] SET password = '" + password + "' WHERE username = '" + username + "'";
                       try
                       {
                           SqlCeCommand myCommand1 = new SqlCeCommand();
                           myCommand1.CommandText = query;
                           myCommand1.Connection = conn;
                           myCommand1.ExecuteNonQuery();
                           MessageBox.Show(username +"'s password changed to " + password, "Password change", MessageBoxButtons.OK, MessageBoxIcon.Information);

                       }
                       catch (Exception ex)
                       {
                           MessageBox.Show(ex.Message, "Problem.");
                       }
                   } else count = 2;
                   if (count == 2)
                   {
                       MessageBox.Show("WRONG USER NAME OR PASSWORD ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   }
                }
            }
            conn.Close(); 
        }
     }
}



Thanks
AnswerRe: multi-user winforms application Pin
SeMartens30-Sep-09 22:09
SeMartens30-Sep-09 22:09 
GeneralRe: multi-user winforms application Pin
Greg Chelstowski30-Sep-09 22:37
Greg Chelstowski30-Sep-09 22:37 
GeneralRe: multi-user winforms application Pin
An Enigma30-Sep-09 22:43
An Enigma30-Sep-09 22:43 
GeneralRe: multi-user winforms application Pin
Greg Chelstowski30-Sep-09 22:49
Greg Chelstowski30-Sep-09 22:49 
GeneralRe: multi-user winforms application Pin
An Enigma30-Sep-09 22:52
An Enigma30-Sep-09 22:52 
GeneralRe: multi-user winforms application Pin
J4amieC30-Sep-09 23:22
J4amieC30-Sep-09 23:22 
GeneralRe: multi-user winforms application Pin
Mycroft Holmes30-Sep-09 22:56
professionalMycroft Holmes30-Sep-09 22:56 
GeneralRe: multi-user winforms application Pin
An Enigma30-Sep-09 23:02
An Enigma30-Sep-09 23:02 
GeneralRe: multi-user winforms application Pin
Mycroft Holmes30-Sep-09 23:25
professionalMycroft Holmes30-Sep-09 23:25 
Questiononshutdown() problem Pin
Ajithevn30-Sep-09 20:54
Ajithevn30-Sep-09 20:54 
AnswerRe: onshutdown() problem Pin
N a v a n e e t h30-Sep-09 21:11
N a v a n e e t h30-Sep-09 21:11 
GeneralRe: onshutdown() problem Pin
Ajithevn30-Sep-09 21:25
Ajithevn30-Sep-09 21:25 
QuestionHow to view a Column Name in listbox by selecting the Column in DataGridView Pin
mumair78630-Sep-09 20:51
mumair78630-Sep-09 20:51 
AnswerRe: How to view a Column Name in listbox by selecting the Column in DataGridView Pin
Mycroft Holmes30-Sep-09 23:09
professionalMycroft Holmes30-Sep-09 23:09 
GeneralRe: How to view a Column Name in listbox by selecting the Column in DataGridView Pin
mumair7861-Oct-09 0:38
mumair7861-Oct-09 0:38 
GeneralRe: How to view a Column Name in listbox by selecting the Column in DataGridView Pin
Henry Minute1-Oct-09 1:12
Henry Minute1-Oct-09 1:12 
GeneralRe: How to view a Column Name in listbox by selecting the Column in DataGridView Pin
mumair7861-Oct-09 2:21
mumair7861-Oct-09 2:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.