Below code is the best solution when, developer wish to execute 2nd class, only if 1st class get executed successfully
Class 1st
public bool SaveOtherInfo()
{
try
{
con.Open();
if (textBoxSrNo.Text == "" || textBoxCARD2.Text == "" || textBoxFirstName.Text == "")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('One or more field are empty..');", true);
return false;
}
if (FileUpload1.FileName == "")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Select Image');", true);
return false;
}
else
{
param = " '" + this.textBoxSrNo.Text + "' ," +
{
try
{
con.Open();
if (textBoxSrNo.Text == "" || textBoxCARD2.Text == "" || textBoxFirstName.Text == "")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('One or more field are empty..');", true);
return false;
}
if (FileUpload1.FileName == "")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Select Image');", true);
return false;
}
else
{
param = " '" + this.textBoxSrNo.Text + "' ," +
" '" + this.comboBoxRelocate.Text + " '";
strSQL = @"INSERT INTO student VALUES (" + param + ")";
cmd.Connection = con;
cmd.CommandText = strSQL;
cmd.CommandType = CommandType.Text;
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Information submitted Successfully');", true);
return true;
}
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + ex.Message + "');", true);
return false;
}
finally
{
con.Close();
}
return true;
}
strSQL = @"INSERT INTO student VALUES (" + param + ")";
cmd.Connection = con;
cmd.CommandText = strSQL;
cmd.CommandType = CommandType.Text;
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Information submitted Successfully');", true);
return true;
}
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + ex.Message + "');", true);
return false;
}
finally
{
con.Close();
}
return true;
}
Class 2nd
private bool SaveImage()
{
try
{
//Condition to check if the file uploaded or not
if (FileUpload1.HasFile)
{
//getting length of uploaded file
int length = FileUpload1.PostedFile.ContentLength;
//create a byte array to store the binary image data
byte[] imgbyte = new byte[length];
//store the currently selected file in memeory
HttpPostedFile img = FileUpload1.PostedFile;
//set the binary data
img.InputStream.Read(imgbyte, 0, length);
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO ImageTable VALUES(@srno,@image)", con);
cmd.Parameters.Add("@srno", SqlDbType.NVarChar, 50).Value = textBoxSrNo.Text;
cmd.Parameters.Add("@image", SqlDbType.Image).Value = imgbyte;
int count = cmd.ExecuteNonQuery();
con.Close();
if (count > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Image Inserted Successfully');", true);
}
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + ex.Message + "');", true);
return false;
}
finally
{
}
return true;
}
{
try
{
//Condition to check if the file uploaded or not
if (FileUpload1.HasFile)
{
//getting length of uploaded file
int length = FileUpload1.PostedFile.ContentLength;
//create a byte array to store the binary image data
byte[] imgbyte = new byte[length];
//store the currently selected file in memeory
HttpPostedFile img = FileUpload1.PostedFile;
//set the binary data
img.InputStream.Read(imgbyte, 0, length);
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO ImageTable VALUES(@srno,@image)", con);
cmd.Parameters.Add("@srno", SqlDbType.NVarChar, 50).Value = textBoxSrNo.Text;
cmd.Parameters.Add("@image", SqlDbType.Image).Value = imgbyte;
int count = cmd.ExecuteNonQuery();
con.Close();
if (count > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Image Inserted Successfully');", true);
}
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + ex.Message + "');", true);
return false;
}
finally
{
}
return true;
}
Button for checking classes execution and save data of both the classes if both classes executes successfully,
In short, If class"SaveOtherInfo" gets executed successfully then only class"SaveImage" get executed otherwise not..
protected void ButtonSave_Click(object sender, EventArgs e)
{
bool flag;
flag = SaveOtherInfo();
if (flag == true)
{
SaveImage();
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Can not Insert Data');", true);
}
}
{
bool flag;
flag = SaveOtherInfo();
if (flag == true)
{
SaveImage();
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Can not Insert Data');", true);
}
}
No comments:
Post a Comment