Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

i m using this code.


private void btnSave_Click(object sender, EventArgs e)
{
try
{
string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
string query = "insert into CustomerTable values('" + txtCustomerName.Text + "','" + txtAddress.Text + "','"
+ txtMobileNo.Text + "','" + lblGoldBalance.Text + "','" + lblSilverBalance.Text + "','" + lblCashBalance.Text + "')";
SqlConnection conn = new SqlConnection(connstr);

if (txtCustomerName.Text != "" & txtAddress.Text != "")
{
MessageBox.Show("Enter some input");
conn.Open();
//Checking User Name Exists in CustomerTable

string query1 = "select txtCustomerName from CustomerTable where txtCustomerName='" + txtCustomerName.Text + "'";
SqlCommand cmd = new SqlCommand(query1,conn);
SqlDataReader reader = cmd.ExecuteReader();

if (reader != null && reader.HasRows)
{
//User exists in db do something
MessageBox.Show("User Already Exists!!");
}

else
{
SqlCommand cmd1 = new SqlCommand(query, conn);
cmd1.ExecuteNonQuery();
txtCustomerName.Clear();
txtAddress.Clear();
txtMobileNo.Clear();
MessageBox.Show("Values Save in DataBase");
}
reader.Close();
conn.Close();
}
else
{
MessageBox.Show("Only Mobile Field Can be Empty");
}
}
catch (Exception ex)
{
MessageBox.Show("At Least 0 value can be save in the Gold Silver Cash Area", ex.Message);
}



it show an error when i am fill only Customer name value then save (here it's show an error "only Mobile field can be empty" but it is not show.
it show a new error is : "There is already an openDataReader associated with this command which must be close first." i am already close the reader.
Posted

1 solution

You should use try/catch/finally and not just try/catch. If you encounter an error then the connection and reader are never closed. If you use finally then regardless if there is an error or not the reader and connection will be closed. You will also need to declare your reader and connection objects above the try/catch/finally block. Like so...
C#
SqlConnection conn = null; // declare here but set in try block
SqlDataReader reader = null; // declare here but set in try block
try
{
 // ...
}
catch
{
    MessageBox.Show("At Least 0 value can be save in the Gold Silver Cash Area", ex.Message);
}
finally
{
    if(reader != null)
    {
        reader.Close();
    }

    if(conn != null)
    {
        conn.Close();
    }
}

Also, close the reader before you attempt to make another connection.
C#
if (reader != null && reader.HasRows)
{
//User exists in db do something
MessageBox.Show("User Already Exists!!");
}
else
{
    if(reader != null)
    {
        reader.Close(); // close the reader before making a new connection
    }
    SqlCommand cmd1 = new SqlCommand(query, conn);
    cmd1.ExecuteNonQuery();
    txtCustomerName.Clear();
    txtAddress.Clear();
    txtMobileNo.Clear();
    MessageBox.Show("Values Save in DataBase");
}
 
Share this answer
 
v3
Comments
Manish Arya 3-Sep-13 2:49am    
same problem


private void btnSave_Click(object sender, EventArgs e)
{
string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
SqlDataReader reader = null;
SqlConnection conn = null;

try
{
string query = "insert into CustomerTable values('" + txtCustomerName.Text + "','" + txtAddress.Text + "','"
+ txtMobileNo.Text + "','" + lblGoldBalance.Text + "','" + lblSilverBalance.Text + "','" + lblCashBalance.Text + "')";
conn = new SqlConnection(connstr);

if (txtCustomerName.Text != "" & txtAddress.Text != "")
{
conn.Open();
//Checking User Name Exists in CustomerTable

string query1 = "select txtCustomerName from CustomerTable where txtCustomerName='" + txtCustomerName.Text + "'";
SqlCommand cmd = new SqlCommand(query1,conn);
reader = cmd.ExecuteReader();

if (reader != null && reader.HasRows)
{
//User exists in db do something
MessageBox.Show("User Already Exists!!");
}

else
{
SqlCommand cmd1 = new SqlCommand(query, conn);
cmd1.ExecuteNonQuery();
txtCustomerName.Clear();
txtAddress.Clear();
txtMobileNo.Clear();
MessageBox.Show("Values Save in DataBase");
}
reader.Close();
conn.Close();
}
else
{
MessageBox.Show("Only Mobile Field Can be Empty");
}
}
catch (Exception ex)
{
MessageBox.Show("At Least 0 value can be save in the Gold Silver Cash Area", ex.Message);
}
finally
{
reader.Close();
conn.Close();
}
idenizeni 3-Sep-13 2:57am    
Close the reader before you make another connection, see my updated solution.

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