Monday, September 20, 2010

How to use Microsoft Chart Control from SQL database in asp.net C#

first download MS Chart control from: Here

than use
System.Web.UI.DataVisualization.Charting
System.Web.DataVisualization
SqlConnection cot = new SqlConnection(Your Connection String);
SqlCommand cmt = new SqlCommand("Select * your synatx", cot);
cot.Open();
SqlDataAdapter dat = new SqlDataAdapter(cmt);
DataSet ds = new DataSet();
dat.Fill(ds);

if (ds.Tables[0].Rows.Count > 0)
{
Chart1.Series["Series1"]["PointWidth"] = "0.5";// Show data points labels
Chart1.Series["Series1"].IsValueShownAsLabel = true;// Set data points label style
Chart1.Series["Series1"]["BarLabelStyle"] = "Center";// Show chart as 3D
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;// Draw chart as 3D Cylinder
Chart1.Series["Series1"]["DrawingStyle"] = "Cylinder";

// Define the database query
//string mySelectQuery = "SELECT CampaignName,TotalEmails FROM Campaign";
SqlConnection con = new SqlConnection(Your Connection String);

SqlCommand cmd = new SqlCommand("SELECT xxx,yyy from tablename", con);

Chart1.DataSource = cmd;
Chart1.Series["Series1"].XValueMember = "xxx";
Chart1.Series["Series1"].YValueMembers = "yyy";

Chart1.DataBind();


}
else
{
Chart1.Visible = false;
}

Creating Dynamic buttons using C#

protected void Page_Load(object sender, EventArgs e)
{
Button myButton = null;

//Create 5 button dynamically
for (int i = 0; i < 5; i++)
{
//Create new instance of Button class and assign it necessary property
myButton = new Button();
myButton.ID = "myButton" + i; //Assign Id
myButton.Text = "myButton" + i;

//Attach the button click event handler
myButton.Click += new EventHandler(myButton_Click);

//Add Newly created Button to Panel
pnlButton.Controls.Add(myButton);

//Add berek so the next Button will create in next new line
Literal lit = new Literal();
lit.Text = "

";
// add the Button to panel in a web page
pnlButton.Controls.Add(lit);

}
}
void myButton_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if(btn !=null)
{
Response.Write("Button id " + btn.ID + " clicked");
}
}

How to send email in asp.net using C# and smtp as gmail

using System.Net.Mail;

MailMessage mail = new MailMessage();
mail.From = new MailAddress("mailtesting22@gmail.com");
mail.To.Add(new MailAddress("receiver_id@gmail.com"));
mail.Subject = "Test mail";
mail.Body = "Hi! it is a simplemail testing";
mail.IsBodyHtml = true;
try
{
SmtpClient myclient = new SmtpClient();
myclient.Host = "smtp.gmail.com";
myclient.Port = 587;
myclient.Credentials = new
System.Net.NetworkCredential("mailtesting22@gmail.com", "password");
myclient.EnableSsl = true;
myclient.Send(mail);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}