c# Get selected item text from combobox

C# Combobox defined with name my_combobox
private System.Windows.Forms.ComboBox my_combobox;
Read the text of the selected combobox item and assign it to string variable, then show it in message box.
string selected_item_text = my_combobox.GetItemText(my_combobox.SelectedItem);MessageBox.Show(selected_item_text);


|
2017/10/18 06:03

Deploy ASP .NET MVC application using LocalDB to local IIS

When ASP application is published to IIS using LocalDB accessing the database may result in the following error:
Server Error in '/Application_Name' Application.A network-related or instance-specific error occurred while establishing a connection to SQL Server.
|
2017/10/18 05:49

c# combobox fill from database

To get only the unique /distinct/ data table records from any database connected to DataSet in C# project and to put them inside combobox.Fill the table adapter with the records from database.
this.myDBTable_TableAdapter.Fill(this.myDB_DataSet.myDBTable);
Create a view containing the database table
DataView view = new DataView(this.myDB_DataSet.myDBTable);
Copy only the distinct records to new data table.
|
2017/10/05 03:18

Get latest date record - SQL query

To return the latest record (one record only) from data base table where one of the fields is in Date/Time format:
SELECT TOP 1 * FROM table_name ORDER BY date_time_column_name DESC
Notes:
  • for ORACLE instead of TOP 1,  ROWNUM shall be used
  • the query may return NULL value - suitable exception handling must be provided
    |
    2017/10/05 03:02

c# combobox set selected item by item's text

C# Combobox defined with name my_combobox
private System.Windows.Forms.ComboBox my_combobox;
Define string variable with the text of the combobox item that has to be selected. For example, if the combobox items have text values "item_a" "item_b" "item_c" ... and the item which text is "item_b" has to be selected
| |
2017/10/05 02:52

C# - Get database connection string from Web.config file

To get the database connection string from IIS Web.config file using C# code, include System.Configuration directive and then create a string variable to assign the connection string like this:
using System.Configuration;
string connStr = ConfigurationManager.ConnectionStrings["connectionString_Name"].ConnectionString;
If “ConfigurationManager” is not recognized you will need to add reference to: "System.Configuration".See: C# - How to add reference to project

| |
2017/10/05 02:45

C# Textbox - enter only positive numbers on KeyPress event in Windows form application

To prevent users from entering incorrect data in Windows form application, set restriction to allow only specific characters to be entered in the text box.This example uses KeyPress event to monitor the users input and to apply the restriction required.To avoid code duplication in Windows multiform applications for example, a separate class file is created “CommonFunctions.cs”.
The function to allow only numbers in textbox control is named InputNumbers.
For each Windows form, create a single KeyPress event and call the InputNumbers function inside it.
| |
2017/10/05 02:27

c# passing data between forms

C# project with two forms
public partial class Form1 : Form // this is the main form

public partial class Form2 : Form // this is the second form
To pass variables (values, data) from Form1 to Form2
Case: public variables in Form2
public string input_variable; // put this in Form2 as global variable

public string output_variable; // put this in Form2 as global variable
in Form1 inside the function that run (call) Form2
|
2017/10/05 02:07

C# How to draw shapes – Circle, Rectangle, Arc, Pie, Polygon, Bezier, Text

In order to draw shapes in C# a start point and end point coordinates, and a Pen control must be defined first.
Coordinates in C#The value of x is the location of the point along the x-axis, the 0 is at the extreme left. The value of y is the location of the point along the y-axis, the 0 is at the extreme top.
| |
2017/10/05 01:56

C# - How to add reference to project

To add a reference to CSharp project do the following:
  1. Open “Solution Explorer”;
  2. Right click “References”;
  3. Choose “Add Reference”.


On the Reference Manager Window go to: “Assemblies” -> “Framework”.
|
2017/10/05 01:31

C# Custom control – TextBox accepting only numbers, decimal numbers, signed numbers

The creation of custom control using Visual Studio involves the following steps:
  1. Start Visual Studio;
  2. Create new “Windows Control Library” project;
  3. In the created project delete “User Control” file;
  4. Go to “Project” > “Add User Control” and give it a name;
  5. Change the inherited class to this that you want your control to be;
  6. Create you custom properties for the control;
  7. Compile the project: Go to “Build” > “Build Solution”;
    | |
    2017/10/05 00:51

Visual Studio – Add custom control DLL to toolbox

To add a custom control to VisualStudio project:1) Go to “Toolbox” tab;2) Right click on it and click “Choose Items…”;

3) In the “Choose Toolbox Items” window click on the “Browse…” button.Navigate to the location where your Dynamic Link Library (DLL) file is.Click “OK”. Now your custom control is included into the Visual Studio toolbox.

| |
2017/10/05 00:37

Handling CheckedListBox ItemCheck event in C#

Common problem handling ItemCheck event in CheckedListBox control is the fact that when the event is fired the object's state change is not finalized.If within the ItemCheck event handling function we try to get the CheckedListBox item's check state using the most common methods like getting collection of all checked items
foreach (object obj in checkedListBox1.CheckedItems) { // some code here ... }
only the previously checked items will be included in the collection.
|
2015/11/19 10:16

C# How to Modify DataSet Connection Strings at Runtime?

Detailed instructions by Paul Keister in his excellent article Adaptive Connection Strings for Windows Forms Applications
In brief
the connection string property is set read only in the Settings.Designer.cs class:
public string MyAppConnectionString {
   get {
            return ((string)(this["MyAppConnectionString"]));
         }
}
However, all application settings are accessible in read/write mode by using the indexer directly.  The following code demonstrates how to modify the connection string using this approach:
//change the application connection string
Properties.Settings.Default["MyAppConnectionString"] = strCnString;

//debug:verify that this really did work
Console.WriteLine(Properties.Settings
                        
|
2015/10/19 11:07

Create application log file in C# / Write to File example

Application log file example
private string logFile = "full path and file name (C:\\temp\\MyApplicationLog.txt)";
...
WriteToLogFile("your application message", logFile); // use any time to add record to the log file
...
public void WriteToLogFile(string strMessage, string outputFile)
{
   string line = DateTime.Now.ToString() + " | ";
   line += strMessage;
   FileStream fs = new FileStream(outputFile, FileMode.Append, FileAccess.Write, FileShare.None);
   StreamWriter swFromFileStream = new StreamWriter(fs);
   swFromFileStream.WriteLine(line);
   swFromFileStream.Flush();
   swFromFileStream.Close();
}



|
2014/10/19 08:55

C# Is the Logged User a member of specific Windows Domain Group?

C sharp code
using System.DirectoryServices.AccountManagement;
string userName = SystemInformation.UserName;
string UDN = SystemInformation.UserDomainName;
string domainUser = UDN + "\\" + userName;
PrincipalContext pc = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, domainUser);
GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, "Domain_Group_Name");
bool is_member = user.IsMemberOf(group);


|
2013/10/19 11:10

DataGridView add row - C#

Considering DataGridView with 3 text columns defined.
string[] row = new string[] { "column1_content", "column2_content", "column3_content" };
DataGridView1.Rows.Add(row);

|
2012/10/19 09:18