Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
1
Answer
How to display false condition for my code
Raushan Raj
7y
565
1
Reply
This program is looking for "p" in 10th column of Excel Sheet. If "p" is found, then required information is extracted and sent to the desired email id. What to do if "p" is not present in the worksheet? It should display message that "p" is not present in the worksheet. How to do this?
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.IO;
using
System.Runtime.Interolservices;
using
System.Data;
using
Excel = Microsoft.Office.Interop.Excel;
using
Outlook = Microsoft.Office.Interop.Outlook;
namespace
ExcelFile
{
class
Stu
{
static
void
Main(
string
[] args)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
int
rCnt;
int
row = 0;
int
col = 0;
xlApp =
new
Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@
"F:\\Date_Plan.xlsm"
, 0,
true
, 5,
""
,
""
,
true
, Excel.XlPlatform.xlWindows,
"\t"
,
false
,
false
, 0,
true
, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[
"Main Page"
];
range = xlWorkSheet.UsedRange;
row = range.Rows.Count;
col = range.Columns.Count;
List<lst> lst =
new
List<lst>();
for
(rCnt = 1; rCnt < row; rCnt++)
{
if
((range.Cells[rCnt, 10]
as
Excel.Range).Value2 !=
null
)
{
if
((range.Cells[rCnt, 10]
as
Excel.Range).Value2.ToString() ==
"p"
)
{
lst ls =
new
lst();
ls.lstName = gettingvalue((range.Cells[rCnt, 1]
as
Excel.Range));
ls.lstUSN = gettingvalue((range.Cells[rCnt, 2]
as
Excel.Range));
ls.DOB = gettingvalue((range.Cells[rCnt, 3]
as
Excel.Range));
ls.Education = gettingvalue((range.Cells[rCnt, 4]
as
Excel.Range));
ls.Others = gettingvalue((range.Cells[rCnt, 5]
as
Excel.Range));
lst.Add(ls);
}
}
}
try
{
Outlook.Application oApp =
new
Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
var sb =
new
StringBuilder();
sb.AppendLine(
"<head><style> table {font-family: arial, sans-serif; border-collalse: collalse; width: 100%;} "
+
"td, th {border: 1px solid #dddddd;text-align: left; padding: 8px;} </style></head>"
);
sb.AppendLine(
"<body>"
);
sb.AppendLine(
"<table>"
);
sb.AppendLine(
"<thead>"
);
sb.AppendLine(
"<th>lst Name</th>"
);
sb.AppendLine(
"<th>lst USN</th>"
);
sb.AppendLine(
"<th>DOB</th>"
);
sb.AppendLine(
"<th>Education</th>"
);
sb.AppendLine(
"<th>Others</th>"
);
sb.AppendLine(
"</thead>"
);
foreach
(var row
in
lst)
{
sb.AppendLine(
"<tr>"
);
sb.AppendLine(
"<td>"
+ row.lstName +
"</td>"
);
sb.AppendLine(
"<td>"
+ row.lstUSN +
"</td>"
);
sb.AppendLine(
"<td>"
+ row.DOB +
"</td>"
);
sb.AppendLine(
"<td>"
+ row.Education +
"</td>"
);
sb.AppendLine(
"<td>"
+ row.Others +
"</td>"
);
sb.AppendLine(
"</tr>"
);
}
sb.AppendLine(
"</table>"
);
sb.AppendLine(
"</body>"
);
oMsg.HTMLBody = sb.ToString();
oMsg.Subject =
"List of all std "
;
Outlook.Recipients oRecils = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecils.Add(
"
[email protected]
"
);
oRecip.Resolve();
oMsg.Send();
oRecip =
null
;
oRecils =
null
;
oMsg =
null
;
oApp =
null
;
}
catch
(Exception ex)
{
}
xlWorkBook.colose(
true
,
null
,
null
);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
}
private
static
string
gettingvalue(Microsoft.Office.Interop.Excel.Range range)
{
if
(range.Value2 !=
null
)
{
return
range.Value2.ToString();
}
else
return
null
;
}
}
}
public
class
lst
{
public
string
lstName {
get
;
set
; }
public
string
lstUSN {
get
;
set
; }
public
string
DOB {
get
;
set
; }
public
string
Education {
get
;
set
; }
public
string
Others {
get
;
set
; }
}
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
Graph on Bot Framework
How do i change my c# code to be compatible with sql server