Pages

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, February 4, 2017

using Entity Framework or EF to copy an Object.

using Entity Framework or EF to copy an Object.
This can be used to avoid Error : PK cannot be modified :
"The property 'MyPrimaryKey' is part of the object's key information and cannot be modified. "
we can use this query :
var item = _context.MyTableName.First(z => z.Name == "Ardi");
var newItem = (MyTableName)_context.Entry(item).GetDatabaseValues().ToObject();
newItem.MyPrimaryKey = "Generated_" + DateTime.Now.ToString();
_context.SaveChanges();


Tuesday, June 4, 2013

Creating ASPNET WebForm GridView Paging

Here is code for Creating ASPNET WebForm GridView Paging :

On *.aspx files, gridview need : properties AllowPaging and events OnPageIndexChanging
You can set and configure the pager style & setting by changing the PagerStyle and PagerSettings.

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"  OnPageIndexChanging="GridView1_PageIndexChanging>
        <PagerStyle HorizontalAlign="Center" CssClass="foo" />
        <PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageText="First" LastPageText="Last" />
</asp:GridView>

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
        GridView1.PageIndex = e.NewPageIndex;
        LoadData();
}

private void LoadData()
{
        GridView1.DataSource = mylist.ToList();
        GridView1.DataBind();
}

LoadData is a function to bind list or array to GridView. 
We can Manage the list by put them in a ViewState or in a Session to Keep Consistent List.
This could happend when you have searched list or sorted it.

Saturday, February 11, 2012

CompiledQuery Linq to SQL C#

using CompiledQuery Linq to SQL C# using CompiledQuery can do more good performance and more efficient.
 using System.Data.Linq;
 public static Func> userData
        = CompiledQuery.Compile((myDataContext db) => db.TableUsers);

Friday, July 29, 2011

ASP.NET WebService Error

An Error was Found : 
An endpoint configuration section for contract 'TempConvert.TempConvertSoap' could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name.

You should : 
Open your web.config or app.config
Find Binding ...
Then, on your SoapClient
Replace
TempConvertSoapClient svc = new TempConvertSoapClient();
with
TempConvertSoapClient svc = new TempConvertSoapClient("TempConvertSoap");

Wednesday, June 1, 2011

MD5 Hash Encript (C#)

using System.Security.Cryptography;
private string MD5Encrypt(string plaintext)
    {
        ciphertext = "";
        MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
        byte[] data = System.Text.Encoding.ASCII.GetBytes(plaintext);
        data = md5Hasher.ComputeHash(data);
        for (int i = 0; i < data.Length; i++)
        {
            ciphertext += data[i].ToString("x2").ToLower();
        }
        return ciphertext;
}

Kirim Email ASP.NET (C#)

using System.Net.Mail;
using System.Text;
using System.Net;

public bool KirimEmail(List<string> recipients, string sub,string textbody)
{       
        MailMessage mail = new MailMessage();

        foreach (var item in recipients)
        {
            mail.To.Add(item);
        }

        mail.From = new MailAddress("AlamatEmailAnda@Gmail.com");

        mail.Subject = sub;
        //email's body, this is going to be html.
        //note that we attach the image as using cid
        mail.Body = textbody; //<br/> &lt;img src=\ " cid:tmpImage.gif\ " >";
        //set email's body to html
        mail.IsBodyHtml = true;

        //add our attachment
        //Attachment imgAtt = new Attachment(Server.MapPath("tmpImage.gif"));
 //give it a content id that corresponds to the src we added in the body img tag
        //imgAtt.ContentId = "tmpImage.gif";
        //add the attachment to the email
       // mail.Attachments.Add(imgAtt);

        //setup our smtp client, these are Gmail specific settings
        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        client.EnableSsl = true; //ssl must be enabled for Gmail
        //our Gmail account credentials
NetworkCredential credentials = new NetworkCredential("AlamatEmailAnda@Gmail.com", "pass");
        //add credentials to our smtp client
        client.Credentials = credentials;

        try
        {
            client.Send(mail);
            return true;
        }
        catch
        {
            return false;
        }
}

Monday, May 16, 2011

Form inside Form ( MDI )

Menampilkan Form di dalam Form

public FormParent()
{
InitializeComponent();
this.IsMdiContainer = true;
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frmchild = new Form2();
frmchild.MdiParent = this;
frmchild.Show();
}

ShareThis