Dynamic where clause using Linq To SQL
var query = from c in context.Customers select c; if (name != null) query = query.Where ( customer => customer.Name == name ); if (contactName != null) query = query.Where ( customer => customer.ContactName == contactName ); This way we can pass different combinations of arguments to the method and it will still build the […]
Linq to SQL Like Operator
Starting from a simple query from Northwind Database; var query = from c in ctx.Customers where c.City == “London” select c; The query that will be sent to the database will be: SELECT CustomerID, CompanyName, … FROM dbo.Customers WHERE City = [London] There are some ways to write a Linq query that results in using […]
Visual Studio DBML and SQL column default value
Usually when creating a database table, you will tend to add “default values” to some columns (Guid, Dates, …). BEGIN ALTER TABLE [dbo].[User] ADD DEFAULT (getdate()) FOR [Created] END Adding the table to a DBML file under visual studio will ‘by default’ disallow the default value in the table. The mapping with default values will […]
Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF.
This message is raised when you try to insert values in an identity column. Identity columns are auto incremented by the system and thus, does not allow manual modifications/insertions. To resolve this error, you have to set the Column_Identity ON by using the following syntax. SET IDENTITY_INSERT tblXYZ ON — your insert statement here SET […]
Winforms Textbox Autocomplete in c# using Linq
xyzDataContext dc = new xyzDataContext(); AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection(); var query = from c in dc.Clients select c.Company; foreach (String comp in query) { namesCollection.Add(comp); } tBoxCompany.AutoCompleteSource = AutoCompleteSource.CustomSource; tBoxCompany.AutoCompleteMode = AutoCompleteMode.SuggestAppend; tBoxCompany.AutoCompleteCustomSource = namesCollection; Result => AutoCompleteMode accept thoose values : External link : AutoComplete TextBox In WinForms AutoComplete TextBox in C#
DataGridView datasource : Use BindingSource or direct query ???
LINQ to SQL translates LINQ queries to SQL for execution on a database. The results are strongly typed IEnumerable. Because these objects are ordinary common language runtime (CLR) objects, ordinary object data binding can be used to display the results. On the other hand, sorting and change operations (inserts, updates, and deletes) require additional steps. […]
C# SFTP library
Libraries for accessing a SFTP server (FTP over SSH) using C# are not included in the framework ((not yet?)). Anyway, some snippets or libraries are available for free on the internet. I will list two of them : – SharpSSH SharpSSH is quite a good library, standalone, that implement the jsch library. – SFTP using […]
[HowTo] Application start on “Wake up” event
Using this code will allow you to configure your application to start or to display following a system wakeup event. /// <summary> /// This function starts running an application when a specified event occurs. /// </summary> /// <param name=”application”>The application. eg: @”\Program Files\testapp\testapp.exe”</param> /// <param name=”notificationEvent”>Event at which the application is to be started. Use […]
Battery Life Matters
Some articles about power management under pocket pc devices. Pocket PC Power Management Series 1: the Challenge to Developers Pocket PC Power Management Series 3: When is My Device Sleeping, and when is it Running? Pocket PC Power Management Series 9: Battery Life Matters
Invoke Required when updating UI via another thread.
As you know, creating a Thread in the UI using System.Threading object (or any other thread object that is not UI related) does not allow that thread to access directly the UI objects (Eg: accessing a label “Text” property is not allowed). The well know work around is to “Invoke the action in the UI” […]