Category: Asp.Net


C# ile metin sıralamalarını değiştirme

Veritabanından çektiğim kategori navigasyonu için tekrardan veritabanı işlemleri yapmamak adına pratik bir fonksiyon yazayım dedim. Fonksiyon Ürünler » Çikolatalar » Alpella gibi bir metinsel ifadeyi Alpella « Çikolatalar « Ürünler şekline çevirmekte kullanılabilir. Stackoverflow’da sorduğum soruya daha kısa alternatif ise Linq sınıfını kullanan bir kod tavsiye edildi. Onu da altta veriyorum.


public static string ReverseString(string input, string separator, string outSeparator)
{
string result = String.Empty;
string[] temp = Regex.Split(input, separator, RegexOptions.IgnoreCase);
Array.Reverse(temp);
for (int i = 0; i < temp.Length; i++)
{
result += temp[i] + " " + outSeparator + " ";
}
return result;
}

Not: System.Text.RegularExpressions sınıfını kullanır.

Stackoverflow alternatifi:


public static string ReverseString(string input, string separator, string outSeparator)
{
return String.Join(" " + outSeparator + " ", input.Split(new[] { " " + separator + " " }, StringSplitOptions.RemoveEmptyEntries).Reverse().ToArray());
}

Not: System.Linq sınıfını kullanır.

Ayrıca stackoverflow tavsiyelerine göre fonksiyonu StringBuilder ile kullanmak uzun metinsel ifadelerde performans sağlayacaktır:


public static string ReverseString(string input, string separator, string outSeparator)
{
StringBuilder result = new StringBuilder();
string[] temp = Regex.Split(input, separator, RegexOptions.IgnoreCase);
Array.Reverse(temp);
for (int i = 0; i < temp.Length; i++)
{
result.Append(temp[i] + " " + outSeparator + " ");
}
return result.ToString();
}

Asp.Net ile Türkçe e-posta adresi doğrulama

Asp.Net’te normal olarak bir metin kutusuna e-posta adresi doğrulaması eklerseniz (regularExpressioVlidator) hasanöşiğüı@hasangürsoy.com.tr gibi bir e-posta adresini doğrulamayı geçemez. Bu da tasarladığımız web sitesindeki iletişim formuna Türkçe domain sahiplerinin e-postalarını girmemesine neden olacaktır.

İletişim formu

Son tasarladığım bir web projesinde bu nokta sonunda beni araştırmaya sevk etti. Fakat pek de istediğim gibi birşey bulamadığımdan ifadeyi (expression) kendim yazdım. İfade şu şekilde:

\w*[\wçığöşü]+([-+.']\w+)*@\w*[\wçığöşü]+([-.]\w+)*\.\w+([-.]\w+)*

Bunu bir örnekte açıklayacak olursak formumuzda aşağıdaki kod yer almalı. Ayrıca bir de requiredFieldValidator koyuyorum ki e-posta kutucuğu boş da bırakılamasın.


<asp:TextBox ID="tEPosta" runat="server" CssClass="input" />
<asp:RequiredFieldValidator ID="rfvEPosta" runat="server" ControlToValidate="tEPosta" ErrorMessage="* zorunlu" Display="Dynamic" />
<asp:RegularExpressionValidator ID="revEPosta" runat="server" ControlToValidate="tEPosta" ErrorMessage="* geçersiz" Display="Dynamic" ValidationExpression="\w*[\wçığöşü]+([-+.']\w+)*@\w*[\wçığöşü]+([-.]\w+)*\.\w+([-.]\w+)*" />

Bu soruya stackoverflow‘da ve asp.net‘te yanıt aradım.

Today I got this error while I was coding a cms panel. I’ve researched for solution but there was nothing to do with most of them. Then I’ve looked up my code and I’ve realized that I haven’t closed my reader. After I added rdr.Close(); to my code block before closing using statement, problem was solved.

Also you can add rdr.Dispose(); In my code I delete previous uploaded files and sometimes it gives error when the file that needs to be deleted is already deleted. And this is the reason why command cannot be closed. So close and dispose the reader. Problemo solved :)

Error:
COM object that has been separated from its underlying RCW cannot be used.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.

Stack Trace:

[InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.]
   System.Data.Common.IAccessor.ReleaseAccessor(IntPtr hAccessor, Int32& pcRefCount) +0
   System.Data.OleDb.RowBinding.Dispose() +86
   System.Data.OleDb.Bindings.Dispose() +30
   System.Data.OleDb.OleDbCommand.CloseInternalParameters() +18
   System.Data.OleDb.OleDbCommand.ResetConnection() +23
   System.Data.OleDb.OleDbCommand.Dispose(Boolean disposing) +27
   System.ComponentModel.Component.Dispose() +17
   Admin_addContent.LoadCategory() in z:\siteRoot\*.com\Admin\addContent.aspx.cs:223
   Admin_addContent.Page_Load(Object sender, EventArgs e) in z:\siteRoot\*.com\Admin\addContent.aspx.cs:29
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Button click event runs twice in Asp.net

I had encountered with this problem twice. Solution of the problem is simple:

If your aspx page has AutoEventWireup = “true” property and the button has onclick = “Upload_Click” property and at the same time the procedure has Handles Upload.Click that will cause code run twice when the button is clicked.

Solution: Set AutoEventWireup = “false” and remove onclick property from the button or leave AutoEventWireup = “true” and remove Handles Upload.Click from the related event’s procedure.

Asp.net’te buton tıklama olayı iki defa çalışıyor

Bu problemle iki defa karşılaşmıştım. Sorunun çozümü gayet basit:

Eğer aspx sayfanızda AutoEventWireup=”true” olarak belirtilmişse se butonunuzda onclick=”Upload_Click” yordamını çağırma varsa ve aynı zamanda Upload_Click yordamında da Handles Upload.Click eki varsa bu butonunuza iki defa basılmış gibi bir sonuç verecektir.

Çözüm: AutoEventWireup=”false” yapıp onclick özelliğini silin ya da AutoEventWireup=”true” iken ilgili yordamdaki Handles Upload.Click olayını silin.

WordPress.com'dan blog alın. | Tema Motion, volcanic tarafından yapılmıştır.
Follow

Get every new post delivered to your Inbox.