saving the other information to database if no content is present in file upload control using asp.net with c#
-
hi , iam using asp.net with c# i want to store the other information to database even the user doesnot select the image in file upload control .how to do can you give example which helps me. the code works fine when the user selects the image , but when user doesnt select the image it gives errror ,but i want to store the other information if user doesnt selects the image the code is here please correct the code
SqlCommand cmdsave = new SqlCommand("AddBranchProc11", con);
cmdsave.CommandType = CommandType.StoredProcedure;
con.Open();cmdsave.Parameters.AddWithValue("@BranchId", txtbranchid.Text); cmdsave.Parameters.AddWithValue("@BranchName", txtBranchname.Text); cmdsave.Parameters.AddWithValue("@CompanyId", ddlcompanyid1.SelectedItem.Value); cmdsave.Parameters.AddWithValue("@ContractNo", txtcontractno.Text); cmdsave.Parameters.AddWithValue("@Region", ddlRegions.SelectedItem.Value); cmdsave.Parameters.AddWithValue("@City", txtcity.Text); cmdsave.Parameters.AddWithValue("@Area", txtarea.Text); cmdsave.Parameters.AddWithValue("@Address", txtaddress.Text); cmdsave.Parameters.AddWithValue("@StartDate", txtstartdate1.Text); cmdsave.Parameters.AddWithValue("@EndDate", txtEnd.Text); cmdsave.Parameters.AddWithValue("@ContractReminder", ddlcontractreminder.SelectedItem.Value);
FileInfo branchinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
byte[]branchcontent=new byte[branchinfo.Length];
FileStream branchstream = branchinfo.OpenRead();
branchstream.Read(branchcontent,0,branchcontent.Length);
branchstream.Close();
cmdsave.Parameters.AddWithValue("@ReminderType", ddlremindertype.SelectedItem.Value);
cmdsave.Parameters.AddWithValue("@BranchPicture",branchcontent);cmdsave.Parameters.AddWithValue("@Active", x);
if (cmdsave.ExecuteNonQuery()!= 0)
{Functions.ShowAlertMessage("saved"); con.Close(); }
-
hi , iam using asp.net with c# i want to store the other information to database even the user doesnot select the image in file upload control .how to do can you give example which helps me. the code works fine when the user selects the image , but when user doesnt select the image it gives errror ,but i want to store the other information if user doesnt selects the image the code is here please correct the code
SqlCommand cmdsave = new SqlCommand("AddBranchProc11", con);
cmdsave.CommandType = CommandType.StoredProcedure;
con.Open();cmdsave.Parameters.AddWithValue("@BranchId", txtbranchid.Text); cmdsave.Parameters.AddWithValue("@BranchName", txtBranchname.Text); cmdsave.Parameters.AddWithValue("@CompanyId", ddlcompanyid1.SelectedItem.Value); cmdsave.Parameters.AddWithValue("@ContractNo", txtcontractno.Text); cmdsave.Parameters.AddWithValue("@Region", ddlRegions.SelectedItem.Value); cmdsave.Parameters.AddWithValue("@City", txtcity.Text); cmdsave.Parameters.AddWithValue("@Area", txtarea.Text); cmdsave.Parameters.AddWithValue("@Address", txtaddress.Text); cmdsave.Parameters.AddWithValue("@StartDate", txtstartdate1.Text); cmdsave.Parameters.AddWithValue("@EndDate", txtEnd.Text); cmdsave.Parameters.AddWithValue("@ContractReminder", ddlcontractreminder.SelectedItem.Value);
FileInfo branchinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
byte[]branchcontent=new byte[branchinfo.Length];
FileStream branchstream = branchinfo.OpenRead();
branchstream.Read(branchcontent,0,branchcontent.Length);
branchstream.Close();
cmdsave.Parameters.AddWithValue("@ReminderType", ddlremindertype.SelectedItem.Value);
cmdsave.Parameters.AddWithValue("@BranchPicture",branchcontent);cmdsave.Parameters.AddWithValue("@Active", x);
if (cmdsave.ExecuteNonQuery()!= 0)
{Functions.ShowAlertMessage("saved"); con.Close(); }
hi, use the code below,
if (FileUpload1.HasFile)
{
FileInfo branchinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
byte[] branchcontent = new byte[branchinfo.Length];
FileStream branchstream = branchinfo.OpenRead();
branchstream.Read(branchcontent, 0, branchcontent.Length);
branchstream.Close();
cmdsave.Parameters.AddWithValue("@BranchPicture", (branchcontent == null ? DBNull.Value : branchcontent));
}Hope this will help
-
hi , iam using asp.net with c# i want to store the other information to database even the user doesnot select the image in file upload control .how to do can you give example which helps me. the code works fine when the user selects the image , but when user doesnt select the image it gives errror ,but i want to store the other information if user doesnt selects the image the code is here please correct the code
SqlCommand cmdsave = new SqlCommand("AddBranchProc11", con);
cmdsave.CommandType = CommandType.StoredProcedure;
con.Open();cmdsave.Parameters.AddWithValue("@BranchId", txtbranchid.Text); cmdsave.Parameters.AddWithValue("@BranchName", txtBranchname.Text); cmdsave.Parameters.AddWithValue("@CompanyId", ddlcompanyid1.SelectedItem.Value); cmdsave.Parameters.AddWithValue("@ContractNo", txtcontractno.Text); cmdsave.Parameters.AddWithValue("@Region", ddlRegions.SelectedItem.Value); cmdsave.Parameters.AddWithValue("@City", txtcity.Text); cmdsave.Parameters.AddWithValue("@Area", txtarea.Text); cmdsave.Parameters.AddWithValue("@Address", txtaddress.Text); cmdsave.Parameters.AddWithValue("@StartDate", txtstartdate1.Text); cmdsave.Parameters.AddWithValue("@EndDate", txtEnd.Text); cmdsave.Parameters.AddWithValue("@ContractReminder", ddlcontractreminder.SelectedItem.Value);
FileInfo branchinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
byte[]branchcontent=new byte[branchinfo.Length];
FileStream branchstream = branchinfo.OpenRead();
branchstream.Read(branchcontent,0,branchcontent.Length);
branchstream.Close();
cmdsave.Parameters.AddWithValue("@ReminderType", ddlremindertype.SelectedItem.Value);
cmdsave.Parameters.AddWithValue("@BranchPicture",branchcontent);cmdsave.Parameters.AddWithValue("@Active", x);
if (cmdsave.ExecuteNonQuery()!= 0)
{Functions.ShowAlertMessage("saved"); con.Close(); }
I have made some changes in the above reply
if (FileUpload1.HasFile)
{
FileInfo branchinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
byte[] branchcontent = new byte[branchinfo.Length];
FileStream branchstream = branchinfo.OpenRead();
branchstream.Read(branchcontent, 0, branchcontent.Length);
branchstream.Close();
cmdsave.Parameters.AddWithValue("@BranchPicture", (branchcontent == null ? DBNull.Value : branchcontent));
}
else
{
cmdsave.Parameters.AddWithValue("@BranchPicture", DBNull.Value);
}