Rabu, 19 Juni 2013

Saving Data in TxtFile using C#

How to save in data at TxtFile using c#.

Oke Lets See screenshoot and code Below:



default.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="default.ascx.cs" Inherits="Module.ReadtxtFileSave._default" %>

<asp:UpdatePanel ID ="updatepanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <table>
    <tr>
        <td>Name
        </td>
        <td>:</td>
        <td>
            <asp:TextBox ID="txtnama" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Address
        </td>
        <td>:</td>
        <td>
            <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Hoby
        </td>
        <td>:</td>
        <td>
            <asp:TextBox ID="txthoby" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>gender
        </td>
        <td>:</td>
        <td>
            <asp:TextBox ID="txtgender" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td colspan ="3">

            <asp:Button ID ="btnsave" runat ="server" Text ="SAVE" OnClick="btnsave_txtclick" />
        </td>

    </tr>
</table>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnsave" EventName ="Click" />
    </Triggers>
</asp:UpdatePanel>


default.ascx.cs.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
namespace Module.ReadtxtFileSave
{
    public partial class _default : PortalModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnsave_txtclick(object sender, EventArgs e)
        {
            try
            {
                var name = txtnama.Text;
                var Address = txtAddress.Text;
                var hoby = txthoby.Text;
                var gender = txtgender.Text;

                var userdata = name + "#" + Address + "#" + hoby + "#" + gender;
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(Server.MapPath("~/App_Data/simpanbiodata.txt"), true))
                {
                    file.WriteLine(userdata);
                }
            }
            catch (Exception)
            {
            }
            txtnama.Text = "";
            txtAddress.Text = "";
            txtgender.Text = "";
            txthoby.Text = "";
        }
    }
}


before you must make txtfile with name simpanbiodata.txt on your pc .

display result saving in txt file you can see screenshot this below




you can donload source code this link below


http://www.4shared.com/rar/Db0HB1cG/ModuleReadtxtFileSave.html?



Tidak ada komentar:

Posting Komentar