I have this code included in my Save button to save image in the the Database.
it's working perfect. but i want to do it for 5 FileUpload Controller how can i do it ?
string cn_str = ConfigurationManager.ConnectionStrings["REGConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(cn_str);
con.Open();
SqlCommand cmd_sql = new SqlCommand("imageAdd", con);
cmd_sql.CommandType = CommandType.StoredProcedure;
string filePath = this.FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
Stream imgStream = FileUpload1.PostedFile.InputStream;
BinaryReader imgBinary = new BinaryReader(imgStream);
Byte[] bytes = imgBinary.ReadBytes((Int32)imgStream.Length);
imgBinary.Close();
imgStream.Close();
cmd_sql.Parameters.Add("@imgName", SqlDbType.Text).Value = filename;
cmd_sql.Parameters.Add("@imgType", SqlDbType.Text).Value = ext.ToString() ;
cmd_sql.Parameters.Add("@imgData", SqlDbType.Binary).Value = bytes;
cmd_sql.ExecuteNonQuery();
Sunny SharmaPosted May 30, 2013, 6:08 AM
sorry for responding a little late. Follow the code below on how to use the SQL transaction feature.
SqlConnection con = new SqlConnection("Server=SUNNY-PC\\SQLEXPRESS;Database=students;Integrated Security=true;");
try
{
con.Open();
SqlCommand cmd = new SqlCommand("...", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlTransaction fileUploadTransaction = con.BeginTransaction();
try
{
for (int i = 0; i < 5; i++)
{
cmd.CommandText = "
// set the parameters for each file like:
// cmd.Parameters.Add("@filename", .....
cmd.ExecuteNonQuery();
}
fileUploadTransaction.Commit(); // this will commit all the transactions made for current session.
}
catch (Exception ex)
{
fileUploadTransaction.Rollback(); //this will roll back the complete transaction in case of any error.
MessageBox.Show("Error while uploading file: " + ex.Message);
}
}
catch (Exception ex)
{
MessageBox.Show("Database connection error: " + ex.Message);
}
========================================================
Please don't forget to accept this as answer if it helps!
Thanks.
Sunny SharmaPosted Jun 5, 2013, 4:25 AM
Sunny SharmaPosted Jun 5, 2013, 4:23 AM
ToBePosted Jun 5, 2013, 3:56 AM
ToBePosted May 29, 2013, 5:19 AM
ok how to do it u mean ?
can u explain it more
Sunny SharmaPosted May 29, 2013, 4:46 AM
How about using a Transaction having five queries to insert five files one by one against a single id?
Hope it helps!
Thanks.