Click here to Skip to main content
11,500,844 members (73,988 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        SqlConnection con =new SqlConnection("Data Source=CCN123-PC\\ESTOCKCARD3;Initial Catalog=sample;Integrated Security=True");
        string x,pa;
        string a, b;
        
        public Form1()
        {
            InitializeComponent();
        }
        public static string CreateHash(string unHashed)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.ASCII.GetBytes(unHashed);
            data = x.ComputeHash(data);
            return System.Text.Encoding.ASCII.GetString(data);
        }
        private void btnsubmit_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand usercheck = new SqlCommand("select * from login where username='"+txtuser.Text+"'",con);
            SqlDataReader read = usercheck.ExecuteReader();
            if (read.Read())
            {
                MessageBox.Show("Username already exists");
                
            }
            else
            {
                x = txtpass.Text;
                string y = CreateHash(x);
                string query = "insert into login values ('" + txtuser.Text + "','" + y + "')";
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.ExecuteNonQuery();
                
                MessageBox.Show("Inserted");
                
            }
            read.Close();
            con.Close();
        }
 
        private void btnlogin_Click(object sender, EventArgs e)
        {
            con.Open();
            a = txtpass.Text;
            b = CreateHash(a);
 
            SqlCommand cmdcheck = new SqlCommand("select * from login where username='" + txtuser.Text + "'", con);
            SqlDataReader rdr = cmdcheck.ExecuteReader();
            if (rdr.Read())
            {
                pa = rdr["password"].ToString();
                if (pa == b)
                {
                    MessageBox.Show("Authorized user");
                }
                else
                {
                    MessageBox.Show("Unauthorized user, register first");
                }
            }
            con.Close();
        }
    }
}
Posted 2-Jan-13 19:47pm
Comments
jacobjohn196 at 3-Jan-13 1:48am
   
Can anyone correct the mistake?
Christian Graus at 3-Jan-13 1:50am
   
Not based on this, no
Christian Graus at 3-Jan-13 1:49am
   
The error means what it says. You need to tell us when it occurs, what you've done before hand, etc. This is a code dump, not a question. You don't even say which line or method has the error
Christian Graus at 3-Jan-13 1:50am
   
Also, your design is rubbish. You should have a data layer class that you call, not a connection string in every page and a member connection.
Sergey Alexandrovich Kryukov at 4-Feb-13 4:04am
   
Please don't post non-answers as "solution".
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First of all, I can erase your db by trying to log in. Read up on SQL injection. Second, you need a data layer. Then you need to use try/finally blocks to make sure all DB connections get closed and disposed of. Once you do this, the error will go away, the issue is that your connection has not been closed from a previous action.

Keeping a DB connection open for the life time of your app is also part of your problem.

I found the issue. If you told us what line had the issue, I'd have found it the firs time. Your code is really bad and you have no idea what you're doing. Buy a book.

 x = txtpass.Text;
                string y = CreateHash(x);
                string query = "insert into login values ('" + txtuser.Text + "','" + y + "')";
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.ExecuteNonQuery();
                
                MessageBox.Show("Inserted");

This code creates a new command, against the connection called con. The old command has not been closed, that's why you get the error. It means what it says, in plain english, and as you could see the line, that and google should have been enough. If it's not, then you have a long way to go before you can write code like this. Buy a book.
  Permalink  
v2
Comments
jacobjohn196 at 3-Jan-13 2:06am
   
While executing the program,an exception arises that shows "There is already an open DataReader associated with this Command which must be closed first".
I want to insert the username and password into the database if it is not already existing.
Christian Graus at 3-Jan-13 4:07am
   
I have a headache. I've told you the answer. Please respond to what I said, don't just tell me the same thing again.
jacobjohn196 at 3-Jan-13 5:35am
   
I've solved it.
jacobjohn196 at 3-Jan-13 5:36am
   
I'm abeginner..:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In the following method

private void btnsubmit_Click(object sender, EventArgs e)
{
    con.Open();
    SqlCommand usercheck = new SqlCommand("select * from login where username='"+txtuser.Text+"'",con);
    SqlDataReader read = usercheck.ExecuteReader();
    if (read.Read())
    {
        MessageBox.Show("Username already exists");
        
    }
    else
    {
        x = txtpass.Text;
        string y = CreateHash(x);
        string query = "insert into login values ('" + txtuser.Text + "','" + y + "')";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.ExecuteNonQuery();
        
        MessageBox.Show("Inserted");
        
    }
    read.Close();
    con.Close();
}

you have used connection object in else case to create SqlCommand object (cmd), but this connection is already associated with SqlDataReader object and which is still open, so if you want to use same connection object then you have to close SqlDataReader (read.Close();) associated with this connection before its use.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 385
1 Peter Leow 260
2 DamithSL 225
3 Maciej Los 215
4 Ralf Meier 134
0 Sergey Alexandrovich Kryukov 435
1 Maciej Los 345
2 DamithSL 285
3 Peter Leow 260
4 OriginalGriff 177


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100