Wednesday, 12 September 2018
Sunday, 9 September 2018
Difference between ExecuteNonQuery() and ExecuteScalar()
ExecuteNonQuery() | ExecuteScalar() | |
1 |
It Returns the count of Rows Effected by the Query.
|
It Returns the First Row and First Column Value of the Query.
|
2 |
Int in Return Type.
|
Object in Return Type.
|
3 |
ExecuteNonQuery() works Queries only. Create,Alter,Drop,Insert,Update,Delete.
|
ExecuteScalar() works Only Aggregate Functions
Max,Min,Count,Sum,Total.
|
4 |
Can be assigned to an integer variable.
Return value is optional. |
Return value is Compulsory.
|
5 |
SqlCommand cmd=new SqlCommand();
cmd.ExecuteNonQuery(); |
SqlCommand cmd=new SqlCommand();
id = (Int16)cmd.ExecuteScalar(); return id; |
Thursday, 6 September 2018
MailCoding-HTML
MailCoding in HTML with attachment using asp.net C#
HTML Page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
<script src="Script/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function SendEmail() {
var name="TestUser";
var fileName="";
$.ajax({
type: "post",
url: 'SendEmail.aspx/SendMail',
data: "{name:'" + name + "',fileName:'"+ fileName +"'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert(data.d);
},
error: function() {
alert('Problem...');
}
});
return false;
}
</script>
</head>
<body>
<input id="Button1" type="button" value="button" onclick="SendEmail()" />
</body>
</html>
C# Page
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Net;
public partial class SendEmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod()]
public static string SendMail(string name, string fileName)
{
string msg = string.Empty;
try
{
SendEmailProcess(name, fileName);
msg = "mail send suscessfully...";
}
catch (Exception ex)
{
}
return msg;
}
private static void SendEmailProcess(string name, string fileName)
{
System.Net.Mail.Attachment attachment = null;
MailMessage message = new MailMessage();
try
{
string tomailids = "athi.litztech@gmail.com"; //.....if add multiple mailid seperated by commas.....
fileName = @"D:\athi.txt"; //file path
string smtpUsername = "athirarajinesh@gmail.com"; // Client Mail Id
string smtpPwd = "password"; // Client Mail Pwd
message.From = new MailAddress(smtpUsername);
message.To.Add(tomailids);
message.Bcc.Add("levale.xlevale@gmail.com");
message.Subject = "Testing Mail";
message.Body = BodyMessage(name);
message.IsBodyHtml = true;
attachment = new System.Net.Mail.Attachment(fileName);
message.Attachments.Add(attachment);
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(smtpUsername, smtpPwd);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
attachment.Dispose();
}
catch (Exception ex)
{
if (attachment != null)
{
attachment.Dispose();
}
}
}
// Body Message Alignment
private static string BodyMessage(string name)
{
string strHTML = string.Empty;
strHTML = "<HTML>";
strHTML = strHTML + "<head>";
strHTML = strHTML + "<style>";
strHTML = strHTML + "<!--";
strHTML = strHTML + " /* Style Definitions */";
strHTML = strHTML + "p.MsoNormal, li.MsoNormal, div.MsoNormal";
strHTML = strHTML + " {margin:0in;";
strHTML = strHTML + " margin-bottom:.0001pt;";
strHTML = strHTML + " font-size:10.0pt;";
strHTML = strHTML + " font-family:'Arial';}";
strHTML = strHTML + "@page Section1";
strHTML = strHTML + " {size:8.5in 11.0in;";
strHTML = strHTML + " margin:1.0in 1.25in 1.0in 1.25in;}";
strHTML = strHTML + "div.Section1";
strHTML = strHTML + " {page:Section1;}";
strHTML = strHTML + "p.disclaimer";
strHTML = strHTML + " {margin:0in;";
strHTML = strHTML + " margin-bottom:.0001pt;";
strHTML = strHTML + " font-size:6.0pt;";
strHTML = strHTML + " font-family:'Arial';}";
strHTML = strHTML + "-->";
strHTML = strHTML + "</style>";
strHTML = strHTML + "</head>";
strHTML = strHTML + "<body lang=EN-US>";
strHTML = strHTML + "<div class=Section1>";
strHTML = strHTML + "<p class=MsoNormal> </p>";
strHTML = strHTML + "<p class=MsoNormal>Dear <b>" + name + "</b></p>";
strHTML = strHTML + "<p class=MsoNormal> </p>";
strHTML = strHTML + "<p class=MsoNormal>Thanks & Regards</p>";
strHTML = strHTML + "<p class=MsoNormal> </p>";
strHTML = strHTML + "<p class=MsoNormal>Client Name </p>";
strHTML = strHTML + "<p class=MsoNormal> </p>";
strHTML = strHTML + "</div>";
strHTML = strHTML + "</body>";
strHTML = strHTML + "</BODY>";
strHTML = strHTML + "</HTML>";
return strHTML;
}
}
Sunday, 2 September 2018
Multiple Image Upload
Multiple image upload in Asp.net C#
HTML Coding
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="True" />
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload" />
<br />
</div>
</form>
</body>
</html>
C# Coding
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class ImageMultipleUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpFileCollection imageCollection = Request.Files;
for (int i = 0; i < imageCollection.Count; i++)
{
HttpPostedFile uploadImages = imageCollection[i];
string fileName = Path.GetFileName(uploadImages.FileName);
uploadImages.SaveAs(Server.MapPath("~/images/") + fileName);
}
Response.Write("<script>alert('Image Uploded')</script>");
}
}
Multiple image upload and Restrict file size
Multiple image upload and Restrict file size using Asp.Net C#
HTML Coding
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageUpload.aspx.cs" Inherits="ImageUpload" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Multiple image upload and Restrict file size using Asp.Net C#</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<br />
<asp:FileUpload ID="FileUpload1" AllowMultiple="True" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
C# Coding
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
public partial class ImageUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
con.Open();
HttpFileCollection hfcc = Request.Files;
if (hfcc.Count != 0) //
{
// Allow Only 2 Images
if (hfcc.Count <= 2)
{
if (FileUpload1.PostedFile.ContentLength < 1048576) // Allow Size upto 1MB
{
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
hpf.SaveAs(Server.MapPath("Image") + "\\" + Path.GetFileName(hpf.FileName));
SqlCommand cmd = new SqlCommand("insert into Reg(imagename,imgpath) values ('" +hpf.FileName+ "','" +"Image/"+hpf.FileName + "')", con);
cmd.ExecuteNonQuery();
}
}
con.Close();
Response.Write("<script>alert('Image Uploaded')</script>");
}
else
{
Response.Write("<script>alert('Maximum File Size 1MB')</script>");
}
}
else
{
Response.Write("<script>alert('Allowed only 2 Images')</script>");
}
}
else
{
Response.Write("<script>alert('Atleast One Image Upload')</script>");
}
}
}
C# Coding Standards Series – Naming Conventions
Terminology and Definitions for Naming Conventions:
Camel Case (camelCase): The first letter of the word is lower case and then each first letter of the part of the word is upper case;
Pascal Case (PascalCase): The first letter of the word is upper case and then each first letter of the part of the word is upper case;
Underscode Prefix (_underscore): The word begins with underscore char and for the rest of the word use camelCase rule;
General Rules
1. Use Pascal Case for Class names:
Ex: public class HelloWorld { ... };
2. Use Pascal Case for Method names:
Ex: public void SayHello(string name) { ... };
3. Use Camel Case for variables and method parameters:
Ex: int totalCount = 0;
void SayHello(string name)
{
string fullMessage = "Hello " + name;
//...
}
4. Avoid all upper case or all lower case names;
5. Do not use Hungarian notation:
Ex: string m_sName; (the prefix m_ means that is a member variable and s means that is a string data type);
6. Avoid abbreviations longer than 5 characters;
7. Avoid using abbreviations unless the full name is excessive:
Ex: Good: string address;
Not good: string addr;
8. Use meaningfull, descriptive words for naming variables;
9. Try to prefix Boolean variables with “Can”, “Is” or “Has”;
10. Do not use Underscore Prefix for local variables names;
11. All member variables must use Underscore Prefix so that they can be identified from other local variables names;
12. Avoid naming conflicts with existing .NET Framework namespaces or types;
13. Do not include the parent class name within a property name:
Ex: Good: Customer.Name;
Not good: Customer.CustomerName;
14. When defining a root namespace, use a Product, a Company or a Developer name as the root:
Ex: NorthwindApplication.Utilities;
15. Use Pascal Case for file names;
16. Method name should tell you what it does;
17. A method should do only “one job”. Do not combine multiple jobs in one method even if those jobs have very few lines of code.
Ex:
protected void SaveCustomerName(string customerName)
{
//code here...
}
Naming Conventions for ASP.NET Controls
In general, naming ASP.NET controls is made using Camel Case naming convention, where the prefix of the name is the abbreviation of the control type name.
Camel Case (camelCase): The first letter of the word is lower case and then each first letter of the part of the word is upper case;
Pascal Case (PascalCase): The first letter of the word is upper case and then each first letter of the part of the word is upper case;
Underscode Prefix (_underscore): The word begins with underscore char and for the rest of the word use camelCase rule;
General Rules
1. Use Pascal Case for Class names:
Ex: public class HelloWorld { ... };
2. Use Pascal Case for Method names:
Ex: public void SayHello(string name) { ... };
3. Use Camel Case for variables and method parameters:
Ex: int totalCount = 0;
void SayHello(string name)
{
string fullMessage = "Hello " + name;
//...
}
4. Avoid all upper case or all lower case names;
5. Do not use Hungarian notation:
Ex: string m_sName; (the prefix m_ means that is a member variable and s means that is a string data type);
6. Avoid abbreviations longer than 5 characters;
7. Avoid using abbreviations unless the full name is excessive:
Ex: Good: string address;
Not good: string addr;
8. Use meaningfull, descriptive words for naming variables;
9. Try to prefix Boolean variables with “Can”, “Is” or “Has”;
10. Do not use Underscore Prefix for local variables names;
11. All member variables must use Underscore Prefix so that they can be identified from other local variables names;
12. Avoid naming conflicts with existing .NET Framework namespaces or types;
13. Do not include the parent class name within a property name:
Ex: Good: Customer.Name;
Not good: Customer.CustomerName;
14. When defining a root namespace, use a Product, a Company or a Developer name as the root:
Ex: NorthwindApplication.Utilities;
15. Use Pascal Case for file names;
16. Method name should tell you what it does;
17. A method should do only “one job”. Do not combine multiple jobs in one method even if those jobs have very few lines of code.
Ex:
protected void SaveCustomerName(string customerName)
{
//code here...
}
Naming Conventions for ASP.NET Controls
In general, naming ASP.NET controls is made using Camel Case naming convention, where the prefix of the name is the abbreviation of the control type name.
Abbreviation | ASP.NET Control |
STANDARD CONTROLS | |
btn | Button |
cb | CheckBox |
cbl | CheckBoxList |
ddl | DropDownList |
fu | FileUpload |
hdn | HiddenField |
lnk | Hyperlink |
img | Image |
ibtn(btn) | ImageButton |
lbl | Label |
lbtn(btn) | LinkButton |
lb | ListBox |
lit | Literal |
mv | MultiView |
pnl | Panel |
ph | PlaceHolder |
rb | RadioButton |
rbl | RadioButtonList |
tbl | Table |
txt | TextBox |
v | View |
DATA CONTROLS | |
dtl | DataList |
dp | DataPager |
dtv | DetailsView |
ets | EntityDataSource |
fv | FormView |
gv | GridView |
lds | LinqDataSource |
lv | ListView |
ods | ObjectDataSource |
qe | QueryExtender |
rpt | Repeater |
smd | SiteMapDataSource |
sds | SqlDataSource |
xds | XmlDataSource |
VALIDATION CONTROLS | |
cpv | CompareValidator |
ctv | CustomValidator |
rv | RangeValidator |
rev | RegularExpressionValidator |
rfv | RequiredFieldValidator |
vs | ValidationSummary |
Saturday, 1 September 2018
Auto Increment Id or Number Using Asp.net C#
Automatically increasing Id or Number and save the data in the Database using Asp.net C#
HTML Coding
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Auto Increment Id or Number</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<table><tr><td>
<asp:Label ID="Label1" runat="server" Text="ID"></asp:Label>
</td><td>
<asp:Label ID="lblID" runat="server" ForeColor="#FF3399"></asp:Label>
</td></tr>
<tr><td>
<asp:Label ID="Label2" runat="server" Text="Course"></asp:Label>
</td><td>
<asp:TextBox ID="txtCourse" runat="server"></asp:TextBox>
</td></tr>
<tr><td></td><td>
<asp:Button ID="Button1" runat="server" Text="Register" OnClick="Button1_Click" />
</td></tr>
</table></div>
</form>
</body>
</html>
C# Coding
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getid();
}
}
protected void getid()
{
SqlConnection con = new SqlConnection (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select MAX (CAST( Id as INT)) from Reg", con);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.Read())
{
string Value = rd[0].ToString();
if (Value == "")
{
lblID.Text = "1";
}
else
{
lblID.Text =rd[0].ToString();
lblID.Text = (Convert.ToInt64(lblID.Text) + 1).ToString();
}
}
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("insert into Reg values('"+lblID.Text+"','"+txtCourse.Text+"')", con);
cmd.ExecuteNonQuery();
txtCourse.Text = "";
getid();
}
}
Create the table
Subscribe to:
Posts (Atom)