Şub 23 2011

web.config den mail ayarlarını okuma

Category: c#by_derkan @ 11:11

    public static MailSettingsSectionGroup MailAyar()
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
        return(MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
    }

 

 

Response.Write("host: " + MailAyar().Smtp.Network.Host + "<br />");
Response.Write("port: " +
MailAyar().Smtp.Network.Port + "<br />");
Response.Write("Username: " +
MailAyar().Smtp.Network.UserName + "<br />");
Response.Write("Password: " +
MailAyar().Smtp.Network.Password + "<br />");
Response.Write("from: " +
MailAyar().Smtp.From + "<br />");

 

http://www.dotnetspider.com/forum/179482-how-get-mail-settings-from-web-config-file.aspx

Tags:

Ağu 25 2010

connectionStrings e Programatik key ekleme,düzenleme,silme

Category: c#by_derkan @ 22:38

 

       
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration; // gerekmeyebilir

yoksa ekliyoruz.
 
 // connectionStrings e erişim

  public static string GetConnKey(string key) // varsa değer yoksa null döndürür
        {
            if (WebConfigurationManager.ConnectionStrings[key] != null)
            {
                return WebConfigurationManager.ConnectionStrings[key].ConnectionString;
            }
            else
            {
                return null;
            }
        }

// düzenleme
        public static bool SetConnValue(string anahtar, string deger) // bağlantı stringini set eder yoksa ekler :).anahtar boş olamaz!!!
        {
            try
            {
                Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
                ConnectionStringsSection connSettingsSection = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
 
                ConnectionStringSettings ss = new ConnectionStringSettings();
 
                ss.ConnectionString = deger;
                ss.Name = anahtar;
                ss.ProviderName = "System.Data.SqlClient";
 
                if (string.IsNullOrEmpty(GetConnKey(anahtar)))
                {
                    connSettingsSection.ConnectionStrings.Add(ss);
                    configuration.Save();
                }
                else
                {
                    connSettingsSection.ConnectionStrings[anahtar].ConnectionString = deger;
                    configuration.Save();
                }
 
                return true;
            }
            catch
            {
                return false;
            }
        }

// kaldırma


public static bool RemoveConnKey(string key) // conn keyi web.config den kaldırır
        {
            try
            {
                Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
                ConnectionStringsSection connSettingsSection = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
 
                if (GetConnKey(key) != null)
                {
                    connSettingsSection.ConnectionStrings.Remove(key);
                    configuration.Save();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


Tags:

Ağu 25 2010

appSettings e Programatik key ekleme,düzenleme,silme

Category: c#by_derkan @ 22:24
yazdığım,derlediğim işe yarar statik method,fonksiyonları paylaşıyorum.bir yerden başlamak lazım.

using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration; // gerekmeyebilir

yoksa ekliyoruz.
 
 // appSettings e erişim


      public static string GetAppKey(string key) // varsa değeri yoksa null döndürür
        {
            return WebConfigurationManager.AppSettings[key];
        }

        public static int GetKeyInt(string key) // varsa değeri yoksa 0 döndürür
        {
            int result;
            Int32.TryParse(WebConfigurationManager.AppSettings[key], out result);
            return result;
        }

        public static bool GetKeyBool(string key) // True/False değer kontrolü yapar.
        {
            bool sonuc;
            bool.TryParse(GetAppKey(key),out sonuc);
            return sonuc;
        }

// düzenleme

        public static bool SetAppValue(string anahtar, string deger) // ayar varsa günceller yoksa ekler :=)
        {
            try
            {
                Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
                AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
 
                if (GetAppKey(anahtar) == null)
                {
                    appSettingsSection.Settings.Add(anahtar, deger);
                    configuration.Save();
 
                }
                else
                {
                    appSettingsSection.Settings[anahtar].Value = deger;
                    configuration.Save();
                }
 
                return true;
            }
            catch
            {
                return false;
            }
        }

// kaldırma

        public static bool RemoveAppKey(string key) // keyi web.config den kaldırır
        {
            try
            {
                Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
                AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
 
                if (GetAppKey(key) != null)
                {
                    appSettingsSection.Settings.Remove(key);
                    configuration.Save();
                }
 
                return true;
            }
            catch
            {
                return false;
            }
        }


Tags:

Tem 9 2010

UserControl e parametre aktarımı

Category: Asp.net | c#by_derkan @ 14:42

 

using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using by_derkan;
public partial class UserControl_Haber1 : System.Web.UI.UserControl
{
    private int _HaberId;
    public int HaberId
    {
        get
        {
            return _HaberId;
        }
        set
        {
            _HaberId = value;
        }
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        //HaberId değişkeni değeriyle emrimize amade  :=)
// değere bağlı olarak yapılacak işlemler
    }
}


<%@ Register src="Haber1.ascx" tagname="Haber1" tagprefix="uc1" %>
<uc1:Haber1 ID="Haber1" HaberId='<%#Eval("Haber1") %>' runat="server" />

c# da herşeyin nesne olduğunu burada anladım.
class ta üye tanımla değer ver ona göre işlem yap :=)

Tags: ,

Oca 28 2010

SqlHelper ve faydaları :=)

Category: c#Admin @ 22:21

  
ilk zamanlar kendi db classımı yazmak için kasardım.Daha sonra microsoftun bizim için bunu yaptığını öğrendim  kasmaya gerek kalmadı +stored procedure destekli Smile
Parametresiz sorguda db ile ilgilenen kod 1 satıra düşüyor.conn.Open(),conn.Close() yok artık :=)

SqlHelperden önce

        //rasgele 3 ürün alınıyor

        SqlConnection conn = new SqlConnection(connStr);

        conn.Open();

        string sqlStr = "SELECT TOP 3  * FROM Urunler WHERE (KategoriID = @kategoriid) AND (Durum = 'True') ORDER BY newID()";

        SqlCommand cmd = new SqlCommand(sqlStr, conn);

        cmd.Parameters.Add("@kategoriid", SqlDbType.Int).Value = kategoriid;

        SqlDataReader dt = cmd.ExecuteReader();

        datallist1.DataSource = dt;

        datallist1.DataBind();

        dt.Close();

        cmd.Dispose();

        conn.Close();

 

SqlHelperden sonra

        //rasgele 3 ürün alınıyor       
        SqlParameter
[] parametreler = new SqlParameter[] { new SqlParameter("@kategoriid", kategoriid) };

        datallist1.DataSource = SqlHelper.ExecuteReader(connStr, CommandType.Text, "SELECT TOP 3  * FROM Urunler WHERE (KategoriID = @kategoriid) AND (Durum = 'True') ORDER BY newID()", parametreler);

        datallist1.DataBind();

SqlHelper Metotları

ExecuteNonQuery Satır Döndürmeyen bir komutu çalıştırır
ExecuteDataset Dataset olarak satırlar döndürür
ExecuteReader SqlDataReader olarak satırlar döndürür
ExecuteScalar Bir nesne olarak tek bir değer döndürür
ExecuteXmlReader XmlReader içinde bir Xml döndürür
FillDataset Gönderilen parametrelere göre Dataset'i doldurur
UpdateDataset Verilen update,insert,delete komutlarına göre DataSet in satırında değişiklik yapar
CreateCommand Verilen Stored Procedure ve Parametrelere göre Command nesnesi yaratır
ExecuteNonQueryTypedParams
Herhangi bir satır döndürmeyen bir komutu çalıştırır
ExecuteDatasetTypedParams
DataRow'un sütun değerlerini parametre olarak kullanıp DataSet döndüren bir komut işletir.
ExecuteReaderTypedParams
DataRow'un sütun değerlerini parametre olarak kullanıp SqlDataReader döndüren bir komut işletir.
ExecuteScalarTypedParams
DataRow'un sütun değerlerini parametre olarak kullanıp nesnenin değerini döndüren bir komut işletir.
ExecuteXmlReaderTypedParams
XMLReader içersinde DataRow'un sütun değerlerini parametre olarak kullanan bir XML döndüren bir komut işletir



SqlHelper.cs için tıklayın

Tags: