1
Reply

insert is happening for already inserted files in c#

yamini kudeti

yamini kudeti

Sep 12 2017 3:25 PM
181

Below is my code which is used to check whether we have duplicates or not if they are already there in database it should not insert of those files which are in database and inserts remaining files into database but what is happening with my below code is it is inserting all the files again already if i have them in database but it should not insert those files which are already in database and insert the files only which are not in database

My Modified code-
 protected void btnadd_Click(object sender, EventArgs e)
{
var existingFiles = new List<string>();
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile postedFile = Request.Files[i];
string fileName = Path.GetFileName(postedFile.FileName);
string extension = Path.GetExtension(postedFile.FileName);
if (extension.ToLower() == ".mp3" || extension.ToUpper() == ".MP3")
{
// string newtempfile = fileName.Replace("-t", "");
if (value + ".mp3" == fileName)
{
if (CheckFileExist(Session["subjectid"].ToString(), value + ".mp3", Session["type"].ToString(), Session["physician"].ToString(), Docname, TempFileName, DrftFileName, filuploadmp3.HasFile, Type, DrftFileName))
{
existingFiles.Add(fileName);
}
else
{
}
}
}
}
string uname = Session["uid"].ToString();
//string FinalDraftText = txtDocument.Text;
string FinalDraftText = BaseDoc;
string TempText = TemplDoc;
if (ddlstype.SelectedItem.Text == "Snippet")
{
FinalDraftText = string.Empty;
TempText = string.Empty;
TempFileName = string.Empty;
DrftFileName = string.Empty;
adm.AddSubjectItems(Session["ModuleId"].ToString(), Session["subjectid"].ToString(), Session["physician"].ToString(), Session["audioname"].ToString(), Session["type"].ToString(), path.Trim(), Docname, uname, BaseDoc, DraftDoc, FinalDraftText, TempText, TempFileName, DrftFileName);
}
else if (ddlstype.SelectedItem.Text == "General")
{
FinalDraftText = string.Empty;
DrftFileName = string.Empty;
TempFileName = ViewState["TempFileName"].ToString();
adm.AddSubjectItems(Session["ModuleId"].ToString(), Session["subjectid"].ToString(), Session["physician"].ToString(), Session["audioname"].ToString(), Session["type"].ToString(), path.Trim(), Docname, uname, BaseDoc, DraftDoc, FinalDraftText, TempText, TempFileName, DrftFileName);
}
else
{
TempText = string.Empty;
TempFileName = string.Empty;
DrftFileName = ViewState["DocFileName"].ToString();
adm.AddSubjectItems(Session["ModuleId"].ToString(), Session["subjectid"].ToString(), Session["physician"].ToString(), Session["audioname"].ToString(), Session["type"].ToString(), path.Trim(), Docname, uname, BaseDoc, DraftDoc, FinalDraftText, TempText, TempFileName, DrftFileName);
}
BindGrid();
// Clear();
lblerrmsg.Visible = true;
lblerrmsg.Text = "Your Records Saved Successfully...!";
ViewState["BaseDocfilename"] = null;
}
}
Clear();
}
private bool CheckFileExist(string SubjName, string FileName, string Stype, string PhysiName, string DocName, string TempFilName, string DraftFileName, bool HasFile, string Type, string DraftName)
{
// string qry = string.Empty;
var qry = Type == "Add"
? Stype == "Draft"
? "select ID,SubjectID,auditoName from SubjectItems where (SubjectID='" + SubjName + "' and auditoName='" + FileName + "' and AuditoType='" + Stype + "' and Physician='" + PhysiName + "') or (BaseDocumentName='" + DocName + "') or( DraftDocumentName='" + DraftName + "') "
: "select ID,SubjectID,auditoName from SubjectItems where (SubjectID='" + SubjName + "' and auditoName='" + FileName + "' and AuditoType='" + Stype + "' and Physician='" + PhysiName + "') or BaseDocumentName='" + DocName + "' "
: Stype == "Draft"
? "select ID,SubjectID,auditoName from SubjectItems where (BaseDocumentName = '" + DocName + "' and ID <> " + Session["ID"] + ") or (DraftDocumentName = '" + DraftName + "' and ID <> " + Session["ID"] + ") or (SubjectID='" + SubjName + "' and auditoName='" + FileName + "' and AuditoType='" + Stype + "' and Physician='" + PhysiName + "' and ID <> " + Session["ID"] + ")"
: "select ID,SubjectID,auditoName from SubjectItems where (BaseDocumentName = '" + DocName + "' and ID <> " + Session["ID"] + ") or (SubjectID='" + SubjName + "' and auditoName='" + FileName + "' and AuditoType='" + Stype + "' and Physician='" + PhysiName + "' and ID <> " + Session["ID"] + ")";
var cmd = new SqlCommand(qry, con);
var da = new SqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds);
return (ds.Tables[0].Rows.Count == 0);
}
My original code
protected void btnadd_Click(object sender, EventArgs e)
{
var flag1 = CheckFileExist(Session["subjectid"].ToString(), value + ".wav", Session["type"].ToString(), Session["physician"].ToString(), Docname, TempFileName, DrftFileName, filuploadmp3.HasFile, Type, DrftFileName);
if (!flag1)
return;
}
private bool CheckFileExist(string SubjName, string FileName, string Stype, string PhysiName, string DocName, string TempFilName, string DraftFileName, bool HasFile, string Type, string DraftName)
{
var flag1 = false;
string qry = string.Empty;
if (Type == "Add")
{
if (Stype == "Draft")
{
qry = "select ID,SubjectID,auditoName from SubjectItems where (SubjectID='" + SubjName + "' and auditoName='" + FileName + "' and AuditoType='" + Stype + "' and Physician='" + PhysiName + "') or (BaseDocumentName='" + DocName + "') or( DraftDocumentName='" + DraftName + "') ";
}
else
{
qry = "select ID,SubjectID,auditoName from SubjectItems where (SubjectID='" + SubjName + "' and auditoName='" + FileName + "' and AuditoType='" + Stype + "' and Physician='" + PhysiName + "') or BaseDocumentName='" + DocName + "' ";
}
}
else
{
if (Stype == "Draft")
{
qry = "select ID,SubjectID,auditoName from SubjectItems where (BaseDocumentName = '" + DocName + "' and ID <> " + Session["ID"] + ") or (DraftDocumentName = '" + DraftName + "' and ID <> " + Session["ID"] + ") or (SubjectID='" + SubjName + "' and auditoName='" + FileName + "' and AuditoType='" + Stype + "' and Physician='" + PhysiName + "' and ID <> " + Session["ID"] + ")";
}
else
{
qry = "select ID,SubjectID,auditoName from SubjectItems where (BaseDocumentName = '" + DocName + "' and ID <> " + Session["ID"] + ") or (SubjectID='" + SubjName + "' and auditoName='" + FileName + "' and AuditoType='" + Stype + "' and Physician='" + PhysiName + "' and ID <> " + Session["ID"] + ")";
}
}
SqlCommand cmd = new SqlCommand(qry, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
if (HasFile == false)
{
lblerrmsg.Visible = true;
lblerrmsg.Text = "File Already Exist ...!";
return false;
}
else
{
lblerrmsg.Visible = true;
lblfname.Text = "";
lbldocname.Text = "";
pnl1.Visible = true;
btnupdate.Enabled = false;
lblerrmsg.Visible = true;
btnadd.Enabled = true;
string Subjects = "select ID, Subject from Subjects where Module = '" + null + "'";
BindDropDownList(ddlsubjname, Subjects, "Subject", "ID", "--Select--");
lblerrmsg.Text = "File Already Exist ...!";
if (IsPostBack)
{
BindGrid();
}
Clear();
return false;
}
}
flag1 = true;
return flag1;
}

Answers (1)