Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public bool existPationts()
{
con.Open();
s = "select * from Patients where P_id = " +
Convert.ToString(txt_Pid.Text);
sCommand = new SqlCommand(s, con);
int isexist;
isexist = Convert.ToInt32(sCommand.ExecuteScalar());
if(isexist > 0)
{
MessageBox.Show("its exist");
txt_Pid.Focus();
txt_Pid.Clear();
return true;

con.Close();
}
else
{
return false;
con.Close();
}

private void btn_Save_Click(object sender, EventArgs e)
{
try
{
if(!existPationts())
{
Validation();
if (validateID == true && validateName == true && validatePhone1
== true && validateRadio == true && validateCB_City == true
&& validateCB_Center == true && validateCB_doctor ==
true && validateCB_Side == true)
{

if (MessageBox.Show("Are you want save?", "save", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{

s = "insert into Patients (P_id, P_Name, P_Gender,
P_DateOfBirth, City_id, Center_id, Phone1, Phone2,
Doctor_id, Side_id, P_Weight, P_Length, Notes)";
s = s + "values (@P_id, @P_Name, @P_Gender,
@P_DateOfBirth, @City_id, @Center_id, @Phone1, @Phone2,
@Doctor_id, @Side_id, @P_Weight, @P_Length, @Notes)";
sCommand = new SqlCommand(s, con);
con.Open();
sCommand.Parameters.AddWithValue("@P_id", txt_Pid.Text);
sCommand.Parameters.AddWithValue("@P_Name",
txt_PName.Text);
sCommand.Parameters.AddWithValue("@P_Gender", x);
sCommand.Parameters.AddWithValue("@P_DateOfBirth",
dtp_PBirthDate.Text);
sCommand.Parameters.AddWithValue("@City_id",
CB_City.SelectedValue);
sCommand.Parameters.AddWithValue("@Center_id",
CB_Center.SelectedValue);
sCommand.Parameters.AddWithValue("@Phone1",
txt_Phone1.Text);
sCommand.Parameters.AddWithValue("@Phone2",
txt_Phone2.Text);
sCommand.Parameters.AddWithValue("@Doctor_id",
CB_Doctor.SelectedValue);
sCommand.Parameters.AddWithValue("@Side_id",
CB_Side.SelectedValue);
sCommand.Parameters.AddWithValue("@P_Weight",
txt_Weight.Text);
sCommand.Parameters.AddWithValue("@P_Length",
txt_Length.Text);
sCommand.Parameters.AddWithValue("@Notes",
txt_Notes.Text);
sCommand.ExecuteNonQuery();
con.Close();

MessageBox.Show("it's saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Cancel");
}
}
}

}
catch
{
return;
}

}

What I have tried:

How i solve this problem in this code
Posted
Updated 19-Feb-16 21:39pm

Your Connection Current State is Open.
When you open connection, best practice to do below type
C#
if(con.State == con.Open)
{
lable1.text= "Connection is Open";
}
else
{
lable1.text= "Connection is close";
}


In your Code. apply a condition when you open your connection

C#
if(con.State != con.Open)
{
con.Open();
}



Regards,
AARIF SHAIKH
 
Share this answer
 
Try this

public bool existPationts()
{
con.Open();
s = "select * from Patients where P_id = " +
Convert.ToString(txt_Pid.Text);
sCommand = new SqlCommand(s, con);
int isexist;
isexist = Convert.ToInt32(sCommand.ExecuteScalar());
if(isexist > 0)
{
MessageBox.Show("its exist");
txt_Pid.Focus();
txt_Pid.Clear();
con.Close();
return true;


}
else
{
con.Close();
return false;

}
 
Share this answer
 
v2

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