Pages

Search This Blog

Saturday, 5 July 2014

Code for sending SMS....

Hi friends, in this session I will show the code for sending messages using ASP.NET.

Messaging was so useful to us in many situations like sending confirmation code, sending passwords and etc...

Here I took two textboxes and two labels and one button for submit. 1 textbox is used to take phone number as input (ID=”tbphone”) and another textbox is for message (ID=”msg”) .

Here is the code....

using System.Net.Mail;

//Message code
  string sURL;
  StreamReader objReader;

 //Specify the API you used to send SMS in sURL

       sURL = "http://smsdaddy.in/webapi.php?username=xyz &password=abc&sender=id &to=" + tbphone.Text + "&message=Thank You for using our website " + msg.Text+ "   ";

        WebRequest wrGETURL = WebRequest.Create(sURL);
        Stream objStream;
        objStream = wrGETURL.GetResponse().GetResponseStream();
        objReader = new StreamReader(objStream);
        objReader.Close();
      
//Message code ends here 

 You can rate my class on right side top of my blog.......

                                                                       -Your's santoshklpkl

Wednesday, 2 July 2014

How to store and retrieve image from Database in ASP.NET

Hi friends, today I will say you “how to store an image into database and how to retrieve image from database??”

Normally to upload an image into your application we use the tag called “Upload”.

Store.aspx:

<asp:FileUpload ID="FileUpload1" runat="server" />

Store.aspx.cs:

int img = FileUpload1.PostedFile.ContentLength;

byte[] msdata = new byte[img];

FileUpload1.PostedFile.InputStream.Read(msdata, 0, img);

SqlParameter p1 = new SqlParameter("@bill", msdata);

cmd.Parameters.Add(p1);

There is a data type known as “image” which is used to store an image into database tables. Normally, in user point of view the image is stored but if we see the data in the table it will store it in the form of binary code. This is because normally an image is consists of pixels that we can’t store in to our tables. So, database itself will store it in the form of binary code.

Here comes the real problem..... If the user wants to retrieve image from the database “He can’t .... Why??” because it was stored as binary code. To convert binary code into image we will use “Handlers”. First we must create a “Handler” and we will use that handler in our application where we need to display an image.

Handler.ashx:

<%@ WebHandler Language="C#" CodeBehind="Handler1.ashx.cs" Class="EEM.Handler1" %>

Handler1.ashx.cs:
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

namespace EEM
{
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=Santosh-PC\\SQLEXPRESSGH;Initial Catalog=EEM;Integrated Security=True";

            // Create SQL Command
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select Ticketcopies from Travel" +
                              " where Voucherid=@Voucherid";
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = con;

            SqlParameter ImageID = new SqlParameter("@Voucherid", System.Data.SqlDbType.Int);
            ImageID.Value = context.Request.QueryString["Voucherid"];
            cmd.Parameters.Add(ImageID);
            con.Open();
            SqlDataReader dReader = cmd.ExecuteReader();
            dReader.Read();
            context.Response.BinaryWrite((byte[])dReader["bill"]);
            dReader.Close();
            con.Close();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

How to use handler in our application

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1"
                              
ForeColor="#333333" GridLines="None">
                                
<RowStyle BackColor="#EFF3FB" />
                              
 <Columns>
                                  
<asp:BoundField DataField="VoucherId" HeaderText="VoucherId"
 SortExpression="VoucherId" />
                                   
<asp:TemplateField HeaderText="Bill">

  <ItemTemplate>
   
   <asp:Image ID="Image1" runat="server" Height="150px" ImageUrl='<%#"Handler.ashx?VoucherId=" + Eval("voucherid")%>' Width="150px" />
                                      
   </ItemTemplate>

 </asp:TemplateField>
 
</Columns>

<FooterStyle BackColor="#0CB9BB" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                               
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                               
<HeaderStyle BackColor="#0CB9BB" Font-Bold="True" ForeColor="White" />
                               
<EditRowStyle BackColor="#2461BF" />
                               
<AlternatingRowStyle BackColor="White" />
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:EEMConnectionString %>"
     SelectCommand="SELECT * FROM [Medical]"></asp:SqlDataSource>

                        

Tuesday, 1 July 2014

How to add new pages and images into project.

Hi friends, “In this is session I will show you how to add new page and pictures into your project”.

A project can't be done with in one page. It must navigate from one page to another page and it will do many activities. So we need to create many pages in our website. Some times we need to add already

Some times we need to use pictures in our website. So it looks more better and nice. Some time awesome also.

Steps to add a page/picture..

Step 1: 

Create a project

Step 2:

Go to view--> Solution Explorer

Step 3:

Right click on project name "Sample" --> Add new item/ Existing item  

Step 4:

Create/Select the page/image 

Step 5:

Ok



That's it. Now a new page/image was added into your project.

You can rate my class on right side top of my blog ....
                          
                                                                      -Your's Santoshklpkl

Thursday, 26 June 2014

Code for mail ....

Hi friends, today in this session I will show the code for sending mail.

Mail code was useful to us in many situations like recover password and if you want to send login details to the user after registration.

Here I wrote a code for recover password where the user enters his email id to get username and password to his mail.... 

Here in my code I had took one textbox (to enter user mail id), two label (to specify enter mail and message as ‘sent’ or ‘invalid email’) and one button (to submit mail id)

Here is the code for button onclick function:-

using System.Net.Mail;

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
           DataSet ds = new DataSet();
//Server connection      
 using (SqlConnection con = new SqlConnection("DataSource=SANTOSHKLPKL-HP\\SQLEXPRESS;Initial
Catalog=blood_bank;Integrated Security=True"))
            {

                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT                         USER_NAME,PASSWORD FROM users Where MAIL_ID= '" +  txtEmail.Text.Trim() + "'", con);
            
               SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(txtEmail.Text);
                // Recipient e-mail address.
                Msg.To.Add(txtEmail.Text);
                Msg.Subject = "Your Password Details";
                Msg.Body = "Hi, <br/>Please check your Login Detailss<br/><br/>Your Username: " + ds.Tables[0].Rows[0]["USER_NAME"] + "<br/><br/>Your Password: " + ds.Tables[0].Rows[0]["PASSWORD"] + "<br/><br/>";
                Msg.IsBodyHtml = true;
                // your remote SMTP server IP.
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
             
             // Mail will be sent from the below username          
             smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com""your password");
                
                smtp.EnableSsl = true;
                smtp.Send(Msg);
                
               //Msg = null;
                lbltxt.Text = "Your Password Details Sent to your mail";
               
                // Clear the textbox values & where Textbox id="txtEmail"
                txtEmail.Text = "";
            }
            else
            {
                lbltxt.Text = "The Email you entered not exists.";
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }
   } 

you can rate my class on right side top of my blog ..... 

                                                                         -Your's santoshklpkl

Saturday, 21 June 2014

Database connection in ASP.NET



Hi friends, in this session I will say how to add database into our visual studio. So you can use that database tables in your application.

Let us take an example and I will show how to add database into our application.

Here I have created a database which is named as “blood_bank” and my server name was “sqlexpress”. I will show how to add that database into our application....




That’s it, Now you can use your database tables in your application...... J

 You can rate my ASP.NET classes on right side top of my blog.......


                                                                             -Your's santoshklpkl

How to create a sample project in ASP.NET ??

Hi friends, in this session I say “How to create a website/project using .ASP.net”.

Before saying about that I just want to say you to install Visual studio 2008/2010..... (http://download.cnet.com/Microsoft-Visual-Studio-2010-Professional/3000-2212_4-10618634.html) this is the link to download VS2010.....

There is no much difference between VS2008(3.5) and VS2010(4.0) the framework they uses was different ....

Here there is a sample program to create a project ....






You can rate my class on right side top of my blog ....

Thank you for watching video and wait for more classes.

                                                            -Your's Santoshklpkl

Logout test...

Hi friends, I think you are practising and creating a web application using my tips and with help of “Google”.

If you say “Yes” I had almost done it. Then I want to test your application. Cool I won’t ask to send your web application to me ..... ;) I just say something to try in your application.

Just press “Logout” (See session class) button in your application. You may think “what?? That’s it ?? what you have tested in my application??” .

Wait .... Wait .... Now press back arrow in your keyboard. Then see what happens in your application.

“Yes” that’s it....

Today I will say “what has to be done in our project after pressing logout button so that it never navigates back page again”.

Normally, we will create a session and assign user value while user login in to the application and then we will make session value into “Null” or we will “expire” session.

Normally in logic we will write “if session value equal to null goes to home page”.

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] != null)
        {

    Label.Text = "Welcome" + " " + Session["Username"].ToString();
        }

        else
        {
       
        Label.Text = "expired";

       Response.Redirect("./home.aspx");

        }
    }

I too write the same logic but sometimes to make session value null it takes some “millsec”. If session has expired also if we press back arrow button it navigates to before page in our application.

So here I had a solution to overcome this problem.... J We will disable the functionality in the browser. We will do this by writing a code in “javascript” in “head” tag.

<script type = "text/javascript" >
    function changeHashOnLoad() {
        window.location.href += "#";
        setTimeout("changeHashAgain()", "50");
    }

    function changeHashAgain() {
        window.location.href += "1";
    }

    var storedHash = window.location.hash;
    window.setInterval(function () {
        if (window.location.hash != storedHash) {
            window.location.hash = storedHash;
        }
    }, 50);

</script>


We will call the “changeHashOnLoad” function in body.

<body onload="changeHashOnLoad();">


Simple now your application is perfect. 

                                                               -Your's Santoshklpkl
Animated Social Gadget - Blogger And Wordpress Tips Twitter Bird Gadget