Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
jacobjohn196 3-Jan-13 1:48am    
Can anyone correct the mistake?
Christian Graus 3-Jan-13 1:50am    
Not based on this, no
Christian Graus 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 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 4-Feb-13 4:04am    
Please don't post non-answers as "solution".
—SA

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.

C#
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.
 
Share this answer
 
v2
Comments
jacobjohn196 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 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 3-Jan-13 5:35am    
I've solved it.
jacobjohn196 3-Jan-13 5:36am    
I'm abeginner..:)
In the following method

C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900