how to put conditions for this code
the total code will be in same button control if all these if ,else and foreach conditions are satisfide i want to enter the data into database how to put condition for that
if (txtcomutedleaves.Text == string.Empty)
lblcomutedleaves.Visible = true;
else
lblcomutedleaves.Hide();
if (txtmaternity.Text == string.Empty)
lblmaternity.Visible = true;
else
lblmaternity.Hide();
if (txtmiscarriage.Text == string.Empty)
lblmaxcarriage.Visible = true;
else
lblmaxcarriage.Hide();
if (txtrhs.Text == string.Empty)
lblmaxrhs.Visible = true;
else
lblmaxrhs.Hide();
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
try
{
int.Parse(c.Text);
}
catch
{
c.Text= "Enter Int";
}
}
}
/// if above conditions are satisfied then only the bellow code should be executed how to put the condition for this ,please tell me
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationSettings.AppSettings["conn"].ToString();
con.Open();
string strsql = "insert into maxleaves values('" + txtcls.Text + "','" + txthpls.Text + "','" + txtels.Text + "','" + txtpaternity.Text + "','" + txtsleave.Text + "','" + txtcomutedleaves.Text + "','" + txtmaternity.Text + "','" + txtmiscarriage.Text + "','" + txtrhs.Text + "')";
SqlCommand cmd = new SqlCommand(strsql, con);
cmd.ExecuteNonQuery();
con.Close();
Amit ChoudharyPosted Mar 26, 2010, 9:03 AM
Take a bool Flag=false; and use following function to check the int type inside your foreach() loop:
if( int.TryParse(c.Text))
{
Flag=true;
}
else
{
Flag=false;
}
and put your connectivity Code inside the if() like this:
If(Flag)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationSettings.AppSettings["conn"].ToString();
con.Open();
// ............. etc and your code will goes here.
}
please mark as answer if it helps.
Hirendra SisodiyaPosted Mar 26, 2010, 9:13 AM
Hello pramod
you can try this:
Boolean Success = true;
if (txtcomutedleaves.Text == string.Empty)
lblcomutedleaves.Visible = true;
else
lblcomutedleaves.Hide();
Success = false;
if (txtmaternity.Text == string.Empty)
lblmaternity.Visible = true;
else
lblmaternity.Hide();
Success = false;
if (txtmiscarriage.Text == string.Empty)
lblmaxcarriage.Visible = true;
else
lblmaxcarriage.Hide();
Success = false;
if (txtrhs.Text == string.Empty)
lblmaxrhs.Visible = true;
else
lblmaxrhs.Hide();
Success = false;
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
try
{
int.Parse(c.Text);
}
catch
{
c.Text = "Enter Int";
Success = false;
}
}
}
if (Success)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationSettings.AppSettings["conn"].ToString();
con.Open();
string strsql = "insert into maxleaves values('" + txtcls.Text + "','" + txthpls.Text + "','" + txtels.Text + "','" + txtpaternity.Text + "','" + txtsleave.Text + "','" + txtcomutedleaves.Text + "','" + txtmaternity.Text + "','" + txtmiscarriage.Text + "','" + txtrhs.Text + "')";
SqlCommand cmd = new SqlCommand(strsql, con);
cmd.ExecuteNonQuery();
con.Close();
}
thanks
Please mark as ansere if you like my answere
Sailaja YPosted Mar 26, 2010, 9:03 AM
You can introduce a boolean flag and set it to true, if the condition is satisfied or else false.
Execute the database statements if the flag is true.
bool flag=false;
if (txtcomutedleaves.Text == string.Empty)
lblcomutedleaves.Visible = true;
bool flag=true;
else
lblcomutedleaves.Hide();
if (txtmaternity.Text == string.Empty)
lblmaternity.Visible = true;
bool flag=true;
else
lblmaternity.Hide();
if (txtmiscarriage.Text == string.Empty)
lblmaxcarriage.Visible = true;
bool flag=true;
else
lblmaxcarriage.Hide();
if (txtrhs.Text == string.Empty)
lblmaxrhs.Visible = true;
bool flag=true;
else
lblmaxrhs.Hide();
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
try
{
int.Parse(c.Text);
bool flag=true;
}
catch
{
c.Text= "Enter Int";
}
}
}
/// if above conditions are satisfied then only the bellow code should be executed how to put the condition for this ,please tell me
if(flag)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationSettings.AppSettings["conn"].ToString();
con.Open();
string strsql = "insert into maxleaves values('" + txtcls.Text + "','" + txthpls.Text + "','" + txtels.Text + "','" + txtpaternity.Text + "','" + txtsleave.Text + "','" + txtcomutedleaves.Text + "','" + txtmaternity.Text + "','" + txtmiscarriage.Text + "','" + txtrhs.Text + "')";
SqlCommand cmd = new SqlCommand(strsql, con);
cmd.ExecuteNonQuery();
con.Close();
}
Please mark it as answer, if this helps.
Regards,