Category Archives: C# Snippets

Manage IIS Security from a C# Assembly

I have discovered over the past couple of days that it is possible to control many aspects of IIS behaviour from a .NET assembly (in this case I will be using C# as the underlying language), by using the System.DirectoryServices … Continue reading

Posted in C# Snippets | Comments Off

Exclude named child nodes in XPath

To select all subnodes of a record, except for named exclusions, use the following XPath syntax: /rootNodeName/*[not(self::excludeChildName1)] or for multiple exclusions : /rootNodeName/*[not(self::excludeChildName1)][not(self::excludeChildName2)]

Posted in C# Snippets | Comments Off

Adding a “Please wait” message to long form submissions

I have a couple of forms on the company SharePoint Portal site that take a few seconds to submit. In the back end, they link into our financial packages, and rely on the external application to do some of the … Continue reading

Posted in C# Snippets | Comments Off

Hiding Columns in a dynamic DataGrid

resultDataGrid is populated from resultDataSet, which is populated directly from SQL. private void formatDataGridColumns() { for(int i=0;i<resultdataset .Tables[0].Columns.Count;i++) { BoundColumn objbc = new BoundColumn(); objbc.DataField = _ resultDataSet.Tables[0].Columns[i].ColumnName; objbc.HeaderText = _ resultDataSet.Tables[0].Columns[i].ColumnName; resultDataGrid.Columns.Add(objbc); objbc.Visible = false; if (objbc.HeaderText == _ … Continue reading

Posted in C# Snippets | Comments Off

Check if a file exists on a remote web server (by http)

using System.Net; WebRequest wrq = WebRequest.Create_ ("Http://www.example.com/fileToTest.htm"); bool fileExists = false; try { WebResponse wrs = wrq.GetResponse(); fileExists = true; } catch { }

Posted in C# Snippets | Comments Off

Get SharePoints Database names and connection strings.

C Sharp source code for grabbing database details in sharepoint Using Sharepoint’s internal mechanisms, it is possible to retrieve the database name and connection string for the content databases that power a site. The code in the attached file uses … Continue reading

Posted in C# Snippets | Comments Off

Conditional methods

To create methods that will only be called in a debug build of your application, you can use the following syntax : [Conditional( "DEBUG" )] private void SetDebugVariables() { connect ionString = “datasource=developmentDB;SSPI=true;initial catalog=development”; Trace.WriteLine(“Called in debug build”); } One … Continue reading

Posted in C# Snippets | Comments Off

Splitting a string to an array

string delimiters = " .,;:"; char [] delimiter = delimiters.ToCharArray(); string [] GotValues = Returned.Split(delimiter,2);

Posted in C# Snippets | Comments Off

Using XPath to narrow results from an XML file

// These two lines read config setting from the app.config file. filename = ConfigurationSettings.AppSettings["inputfile"]; filter = Configu rationSettings.AppSettings["filter1"]; XmlDataDocument myDocument = new XmlDataDocument(); myDocument.Load(filename); XmlNodeList myList = myDocument.SelectNodes(“//item[@class='" + filter + "']/*”); for (int i=0; i < myList.Count; i++) { … Continue reading

Posted in C# Snippets | Comments Off

Create a DataSet from a Stored Procedure

SqlConnection myConn = new SqlConnection(ConnectionString); SqlDataAdapter da = new SqlDataAdapter(); ds.SelectCommand = new SqlCommand(“GetByOffice”,myConn); da.SelectCommand.CommandType=CommandType.StoredProcedure; SqlParameter myParam = new SqlParameter(“@Office”,SqlDbType.VarChar,50); myParam.Direction=ParameterDirection.Input; myParam.Value = textBox1.Text; da.SelectCommand.Parameters.Add(myParam); DataSet ds = new DataSet(); da.Fill(ds,”results”); dataGrid1.DataSource = ds.Tables["Results"]; ds.WriteXml(“result.xml”,XmlWriteMode.WriteSchema);

Posted in C# Snippets | Comments Off