.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

Serach Within Attachmnet

Posted By: kamlesh     Posted Date: March 02, 2011    Points:2   Category :Windows Application
hi

i have one application where i have created one drop down, one text box and one datagrid
that datagrid will fill by mails with attachment when i select dropdown for Search within attachment and text box for user search value so it should search within attachment and search result will display on datagrid so how to search within attachment please help me

thanks
kamlesh

Responses
Author: Karthikeyan Anbarasan       [Moderator]      
Posted Date: March 02, 2011     Points: 20   

Hi Kamalesh,

If im right, u going to search through the list of attachments rite? So what kind of search you need to perform? Please brief on that so that we can guide you...

Thanks
Karthik
http://www.f5debug.net
Mark as Answer if the answer solves your issue!!!
Author: kamlesh             
Posted Date: March 02, 2011     Points: 20   

suppose i got some mail from u and it is having attachment like .txt,.pdf,.doc
and that mail will store on database and it will shown on database i have take one search option there and i want to search some value into attachment without opening attachment with only textbox how to do



Thanks And Regards.
Kamlesh Nikam
Author: Sunil Yadav       [Moderator]      
Posted Date: March 02, 2011     Points: 20   

Hi,

Are you storing a attachment file name in the table?
If yes then perform a search on the table where column name contains .txt or .doc file or any other format.

I hope this makes sense.

Author: kamlesh             
Posted Date: March 02, 2011     Points: 20   

yes i am storing attachment in database but only file name and file path not internal text

so how to search sunil

Thanks And Regards.
Kamlesh Nikam
Author: Sunil Yadav       [Moderator]      
Posted Date: March 02, 2011     Points: 20   

Hi,

You can loop through the file path and get all the files from a directory which you will get from file path.

 string filepath = @"D:\Images\";// File path retrieved from database
string filetype = ".doc";//File type retrieved from database. Here you can use wild card to search for all files.

FileInfo[] files = new DirectoryInfo(filepath).GetFiles(filetype, SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
string filename = file.FullName;

}


Hope it helps.

Author: kamlesh             
Posted Date: March 14, 2011     Points: 20   

private void drawTree()
{
try
{
TreeMailNodes.Nodes.Clear();

List<MailNodesDTO> NodeList = MailService.GetNodeList();
bool isExpended = false;
foreach (MailNodesDTO NodeDTO in NodeList)
{
TreeNode RouteNode = new TreeNode();

FileInfo file = new FileInfo(NodeDTO.RouteFolder.Name);
String routFolder = file.Name;
if (routFolder.Length > 4)
{
routFolder = routFolder.Substring(0, routFolder.Length - 4);
}
RouteNode.Text = routFolder;//NodeDTO.RouteFolder.Name;
RouteNode.Tag = NodeDTO.RouteFolder.ID;
foreach (MailSubFolderDTO SubFolderDTO in NodeDTO.SubFolderDTOList)
{
if (SubFolderDTO.Lavel == 1)
{
TreeNode SubNode = new TreeNode();
SubNode.Text = SubFolderDTO.Name;
SubNode.Tag = SubFolderDTO.ID;
string ItemType = SubFolderDTO.Name.Trim().ToLower();
SetTreeNodeIcon(SubNode, ItemType);
if (SubFolderDTO.ChieldNodeList.Count > 0)
{
List<TreeNode> TempNodeList = GetSubTreeNode(SubFolderDTO.ChieldNodeList);
for (int n = 0; n < TempNodeList.Count; n++)
{
SubNode.Nodes.Add(TempNodeList[n]);
}
}
RouteNode.Nodes.Add(SubNode);
}
}
TreeMailNodes.Nodes.Add(RouteNode);
if (!isExpended)
{
RouteNode.Expand();
isExpended = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
clsError.LogError(ex);
}
}


private void TreeMailNodes_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{

x = e.X;
y = e.Y;
}

dtgMail.ClearSelection();
dtgContact.ClearSelection();
dtgCalander.ClearSelection();
dtgTask.ClearSelection();
dtgNote.ClearSelection();
dtgJournal.ClearSelection();

}


private void TreeMailNodes_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeMailNodes.SelectedNode = e.Node;
TreeMailNodes.SelectedNode = e.Node;
if (e.Button == MouseButtons.Right)
{
if (e.Button == MouseButtons.Right)
contextMenuStrip1.Show(TreeMailNodes, e.X, e.Y);
else
contextMenuStrip1.Hide();
}

foreach (TreeNode otree in e.Node.Nodes)
otree.Checked = e.Node.Checked;


dtgMail.ClearSelection();
dtgContact.ClearSelection();
dtgCalander.ClearSelection();
dtgTask.ClearSelection();
dtgNote.ClearSelection();
dtgJournal.ClearSelection();

}


private void TreeMailNodes_Click(object sender, EventArgs e)
{
string ItemType = "";
if (TreeMailNodes.SelectedNode != null)
ItemType = TreeMailNodes.SelectedNode.Text.Trim().ToLower();
switch (ItemType)
{
case "tasks":
if (btnCancelSearchTask.Visible == true)
BindTask();
break;
case "notes":
if (btnCancelSearchNote.Visible == true)
BindNotes();
break;
case "calendar":
if (btnCancelSearchCalander.Visible == true)
BindCalander();
break;
case "contacts":
if (btnCancelSearchContact.Visible == true)
BindContact();
break;
case "journal":
if (btnCancelSearchJournal.Visible == true)
BindJournal();
break;
default:
if (btnCancelSearchMail.Visible == true)
BindMail();
break;

}

ClearsearchFields();
ClearValidation();
}


private void SearchMail()
{
if (ValidateMailSearch())
{
btnCancelSearchMail.Visible = true;
ClearMailBody();
List<MailDetailDTO> MailList;
if (TreeMailNodes.SelectedNode != null)
{

//if (TreeMailNodes.SelectedNode.Level > 0)
//{
// MailList = MailService.GetMailDetailBySubNode(Convert.ToInt32(TreeMailNodes.SelectedNode.Tag), txtMailSearch.Text.Trim());
//}
//else
//{
// MailList = MailService.GetMailDetailByRouteNode(Convert.ToInt32(TreeMailNodes.SelectedNode.Tag), txtMailSearch.Text.Trim());

//}
string StrQuery = GenerateMailSearchQuery();
MailList = MailService.GetMailDetailByCondition(StrQuery);

}
else
{
MailList = new List<MailDetailDTO>();
}
dtgMail.DataSource = MailList;

}
}


private void TreeMailNodes_AfterSelect(object sender, TreeViewEventArgs e)
{
ClearValidation();
ClearsearchFields();
string ItemType = e.Node.Text.Trim().ToLower();
switch (ItemType)
{
case "tasks":
BindTask();
break;
case "notes":
BindNotes();
break;
case "calendar":
BindCalander();
break;
case "contacts":
BindContact();
break;
case "journal":
BindJournal();
break;
default:
BindMail();
break;
}
}


private void cmbMailsearchFilterOn_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbMailsearchFilterOn.SelectedIndex == 7)
{
//Sent StartTimeUTC Search
//cmbMailsearchConditionDate.Visible = true;
//CmbMailSearchCondition.Visible = false;
cmbMailSearchConditionSize.Visible = false;
txtMailSearch.Visible = false;
txtSearchMailSizeInput.Visible = false;
DtpMailSearchEndDate.Visible = true;
dtpMailSearchStartDate.Visible = true;
txtSearchMailSizeInput.Visible = false;
}
else if (cmbMailsearchFilterOn.SelectedIndex == 8)
{
//Size Search
//cmbMailsearchConditionDate.Visible = false;
//CmbMailSearchCondition.Visible = false;
cmbMailSearchConditionSize.Visible = false;
txtMailSearch.Visible = false;
txtSearchMailSizeInput.Visible = false;
DtpMailSearchEndDate.Visible = false;
dtpMailSearchStartDate.Visible = false;
txtSearchMailSizeInput.Visible = true;
}
else
{
//cmbMailsearchConditionDate.Visible = false;
//CmbMailSearchCondition.Visible = true;
cmbMailSearchConditionSize.Visible = false;
txtMailSearch.Visible = true;
txtSearchMailSizeInput.Visible = false;
DtpMailSearchEndDate.Visible = false;
dtpMailSearchStartDate.Visible = false;
txtSearchMailSizeInput.Visible = false;
}

}




private void dtgMail_SelectionChanged(object sender, EventArgs e)
{
if (dtgMail.Rows.Count > 0)
{
this.htmlEditor1.LoadDocument(Convert.ToString(dtgMail.CurrentRow.Cells["MailBody"].Value));
lblMailSubject.Text = Convert.ToString(dtgMail.CurrentRow.Cells["MailSubject"].Value);
lblTime.Text = "Sent On:" + Convert.ToString(dtgMail.CurrentRow.Cells["MailDate"].Value);
lblMailBcc.Text = "Bcc:" + Convert.ToString(dtgMail.CurrentRow.Cells["MailBcc"].Value);
lblMailCc.Text = "Cc:" + Convert.ToString(dtgMail.CurrentRow.Cells["MailCc"].Value);
lblMailFrom.Text = "From:" + Convert.ToString(dtgMail.CurrentRow.Cells["MailFrom"].Value);
lblMailSize.Text = "Size:" + Convert.ToString(dtgMail.CurrentRow.Cells["MailSize"].Value);
lblMailTo.Text = "To:" + Convert.ToString(dtgMail.CurrentRow.Cells["MailTo"].Value);
lstAttachment.Items.Clear();
if (Convert.ToBoolean(dtgMail.CurrentRow.Cells["HasAttechment"].Value))
{
pnlAttechment.Visible = true;
List<Attachment> AttachmentList = MailService.GetAttachmentList(Convert.ToInt32(dtgMail.CurrentRow.Cells["ID"].Value));
for (int i = 0; i < AttachmentList.Count; i++)
{
lstAttachment.Items.Add(AttachmentList[i].FileName);
}
}
else
{
pnlAttechment.Visible = false;
}
}
else
{
ClearMailBody();

}
}



private void txtMailSearch_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SearchMail();
}
}




private void dtgMail_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{

x = e.X;
y = e.Y;
}
}




private void dtgMail_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (dtgMail.Rows.Count > 0)
{
if (e.RowIndex >= 0)
{
dtgMail.Rows[e.RowIndex].Selected = true;

if (e.Button == MouseButtons.Right)
{
if (e.Button == MouseButtons.Right)
contextMenuStrip2.Show(dtgMail, x, y);
else
contextMenuStrip2.Hide();
}
}
}
}



private void dtgMail_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
FillMailDetailDTOFromDatagrid();
}



private void dtgMail_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0)
FillMailDetailDTOFromDatagrid();
}



private void BindMail()
{
try
{
tabContainer.SelectedTab = Tabmail;
ClearMailBody();
List<MailDetailDTO> MailList;
if (TreeMailNodes.SelectedNode != null)
{
if (TreeMailNodes.SelectedNode.Level > 0)
{
MailList = MailService.GetMailDetailBySubNode(Convert.ToInt32(TreeMailNodes.SelectedNode.Tag), "");
}
else
{
MailList = new List<MailDetailDTO>();
dtgMail.DataSource = MailList;
}
}
else
{
MailList = new List<MailDetailDTO>();
dtgMail.DataSource = MailList;
}

dtgMail.DataSource = MailList;
ItemCount.Text = dtgMail.RowCount.ToString();
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
clsError.LogError(ex);
}
}



private string GenerateMailSearchQuery()
{
string SearchQuery = "";

if (TreeMailNodes.SelectedNode.Level > 0)
//If ROute Folder has been selected
SearchQuery = "Select * from dbo.MailMaster where SubFolderId=" + TreeMailNodes.SelectedNode.Tag;
else
//If Sub Folder has been selected
SearchQuery = "Select * from dbo.MailMaster where RouteFolderId=" + TreeMailNodes.SelectedNode.Tag;

if (cmbMailsearchFilterOn.SelectedIndex == 0)
{
//0 All Fields
// if (CmbMailSearchCondition.SelectedIndex == 0)
SearchQuery = SearchQuery + " and (MailSubject like '%" + txtMailSearch.Text.Trim() + "%' or MailFrom like '%" + txtMailSearch.Text.Trim() + "%' or MailTo like '%" + txtMailSearch.Text.Trim() + "%' or MailCc like '%" + txtMailSearch.Text.Trim() + "%' or MailBcc like '%" + txtMailSearch.Text.Trim() + "%' or MailBodyPlain like '%" + txtMailSearch.Text.Trim() + "%' or convert(varchar,MailDate,101) like '" + txtMailSearch.Text.Trim() + "' or MailSize like '%" + txtMailSearch.Text.Trim() + "%')";
//else
// SearchQuery = SearchQuery + " and (MailSubject ='" + txtMailSearch.Text.Trim() + "' or MailFrom ='" + txtMailSearch.Text.Trim() + "' or MailTo ='" + txtMailSearch.Text.Trim() + "' or MailCc ='" + txtMailSearch.Text.Trim() + "' or MailBcc ='" + txtMailSearch.Text.Trim() + "' or MailBody='" + txtMailSearch.Text.Trim() + "')";
}
else if (cmbMailsearchFilterOn.SelectedIndex == 1)
{
//1 From
//if (CmbMailSearchCondition.SelectedIndex == 0)
SearchQuery = SearchQuery + " and (MailFrom like '%" + txtMailSearch.Text.Trim() + "%')";
//else
// SearchQuery = SearchQuery + " and (MailFrom ='" + txtMailSearch.Text.Trim() + "')";
}
else if (cmbMailsearchFilterOn.SelectedIndex == 2)
{
//2 To
// if (CmbMailSearchCondition.SelectedIndex == 0)
SearchQuery = SearchQuery + " and (MailTo like '%" + txtMailSearch.Text.Trim() + "%')";
//else
// SearchQuery = SearchQuery + " and (MailTo ='" + txtMailSearch.Text.Trim() + "')";
}
else if (cmbMailsearchFilterOn.SelectedIndex == 3)
{
//3 Cc
// if (CmbMailSearchCondition.SelectedIndex == 0)
SearchQuery = SearchQuery + " and (MailCc like '%" + txtMailSearch.Text.Trim() + "%')";
//else
// SearchQuery = SearchQuery + " and (MailCc ='" + txtMailSearch.Text.Trim() + "')";
}
else if (cmbMailsearchFilterOn.SelectedIndex == 4)
{
//4 Bcc

// if (CmbMailSearchCondition.SelectedIndex == 0)
SearchQuery = SearchQuery + " and (MailBcc like '%" + txtMailSearch.Text.Trim() + "%')";
//else
// SearchQuery = SearchQuery + " and (MailBcc ='" + txtMailSearch.Text.Trim() + "')";
}
else if (cmbMailsearchFilterOn.SelectedIndex == 5)
{
//5 Subject

// if (CmbMailSearchCondition.SelectedIndex == 0)
SearchQuery = SearchQuery + " and (MailSubject like '%" + txtMailSearch.Text.Trim() + "%')";
//else
// SearchQuery = SearchQuery + " and (MailSubject ='" + txtMailSearch.Text.Trim() + "')";
}

//6 Body
else if (cmbMailsearchFilterOn.SelectedIndex == 6)
{


// if (CmbMailSearchCondition.SelectedIndex == 0)
SearchQuery = SearchQuery + " and (MailBodyPlain like '%" + txtMailSearch.Text.Trim() + "%')";
//else
// SearchQuery = SearchQuery + " and (MailBody ='" + txtMailSearch.Text.Trim() + "')";
}

else if (cmbMailsearchFilterOn.SelectedIndex == 9)
{
// if (CmbMailSearchCondition.SelectedIndex == 0)
// SearchQuery = SearchQuery + " and (FileName like '%" + txtMailSearch.Text.Trim() + "%')";
//else
// SearchQuery = SearchQuery + " and (MailBody ='" + txtMailSearch.Text.Trim() + "')";

MailService objMailService = new MailService();

string strID = objMailService.GetIDForSearchKeyInAttachment(txtMailSearch.Text.Trim());
SearchQuery = SearchQuery + "and ID in( " + strID + ")";

}

//7 Sent StartTimeUTC
else if (cmbMailsearchFilterOn.SelectedIndex == 7)
{
// if (cmbMailsearchFilterOn.SelectedIndex == 0)
SearchQuery = SearchQuery + " and (MailDate between '" + Convert.ToDateTime(dtpMailSearchStartDate.Value).ToString("yyyy-MM-dd 00:00:00") + "' and '" + Convert.ToDateTime(DtpMailSearchEndDate.Value).ToString("yyyy-MM-dd 23:59:59") + "' )";
// else
// SearchQuery = SearchQuery + " and (MailDate between '" + Convert.ToDateTime(dtpMailSearchStartDate.Value).ToString("yyyy-MM-dd 00:00:00") + "' and '" + Convert.ToDateTime(dtpMailSearchStartDate.Value).ToString("yyyy-MM-dd 23:59:59") + "' )";
}
else if (cmbMailsearchFilterOn.SelectedIndex == 8)
{
//8 Size
if (cmbMailSearchConditionSize.SelectedIndex == 0)
SearchQuery = SearchQuery + " and (MailSize=" + txtSearchMailSizeInput.Value + ")";
//else if (cmbMailSearchConditionSize.SelectedIndex == 1)
// SearchQuery = SearchQuery + " and (MailSize>" + txtSearchMailSizeInput.Value + ")";
//else
// SearchQuery = SearchQuery + " and (MailSize<" + txtSearchMailSizeInput.Value + ")";
}
return SearchQuery;

}


private void FillMailDetailDTOFromDatagrid()
{
if (dtgMail.Rows.Count > 0)
{
MailDetailDTO MailDetail = new MailDetailDTO();
MailDetail.ID = Convert.ToInt32(dtgMail.CurrentRow.Cells["ID"].Value);
MailDetail.RouteFolderId = Convert.ToInt32(dtgMail.CurrentRow.Cells["RouteFolderId"].Value);
MailDetail.SubFolderId = Convert.ToInt32(dtgMail.CurrentRow.Cells["SubFolderId"].Value);
MailDetail.MailBody = Convert.ToString(dtgMail.CurrentRow.Cells["MailBody"].Value);
MailDetail.MailSubject = Convert.ToString(dtgMail.CurrentRow.Cells["MailSubject"].Value);
MailDetail.MailDate = Convert.ToDateTime(dtgMail.CurrentRow.Cells["MailDate"].Value);
MailDetail.MailBcc = Convert.ToString(dtgMail.CurrentRow.Cells["MailBcc"].Value);
MailDetail.MailCc = Convert.ToString(dtgMail.CurrentRow.Cells["MailCc"].Value);
MailDetail.MailFrom = Convert.ToString(dtgMail.CurrentRow.Cells["MailFrom"].Value);
MailDetail.MailSize = Convert.ToInt32(dtgMail.CurrentRow.Cells["MailSize"].Value);
MailDetail.MailTo = Convert.ToString(dtgMail.CurrentRow.Cells["MailTo"].Value);
MailDetail.HasAttechment = Convert.ToBoolean(dtgMail.CurrentRow.Cells["HasAttechment"].Value);
frmMail MailObj = new frmMail(MailDetail);
MailObj.ShowDialog(this);
}
}

Thanks And Regards.
Kamlesh Nikam


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend