How To Fix "Endpoint not found" Error When Browsing Restful WCF Service In Visual Studio



TODO:

You try to view your Restful WCF service in a browser from Visual Studio and get the "Endpoint not found" error.

 

SOLUTION:

Right click on your .svc file.  Look for "Factory="= "System.ServiceModel.Activation.WebServiceHostFactory" and delete it.  You can now Right click your .svc file and choose "View in browser"

 

NOTES:

There are no notes on this topic

How To Call Stored Procedure In C# That Has Output Parameters



TODO:

Have you ever wanted to call a stored procedure in C# that has output parameters?

 

SOLUTIONS:

System.Data.Objects.ObjectParameter param1 = new ObjectParameter("paraminsp1", typeof(bool));
System.Data.Objects.ObjectParameter param2 = new ObjectParameter("paraminsp2", 0.000);           //default so EF works

using (MyEntities MyDatabase = new MyEntities())
{
     MyDatabase.sp_name(x, 2, param1, param2);
}

 

NOTES:

There are no notes on this topic

How To Pass A DataTable To A Stored Procedure Using C# and SQL Server



TODO:

Have you ever wanted to pass a DataTable from your C# application to a Stored Procedure in SQL Server?

 

SOLUTION:

Step 1:  Create the User-Definied Table Type in SQL Server

CREATE TYPE [dbo].[MyUserDefinedType] AS TABLE(
	[Id] [int] NULL,
	[FieldOne] [int] NULL,
	[FieldTwo] [int] NULL
)

 

Step 2:  Create the DataTable and call the Stored Procedure

using (DataTable myDataTable = new DataTable())
{
     myDataTable.Columns.Add(“FieldOne”, System.Type.GetType("System.Int32"));
     myDataTable.Columns.Add(“FieldTwo, System.Type.GetType("System.Int32"));
     myDataTable.Columns.Add(“FieldThree, System.Type.GetType("System.Int32"));
                
     DataRow dataRow = null;
                
     for (int i = 0; i < 100; i++)
     {
          dataRow = myDataTable.NewRow();  
          dataRow[“FieldOne] = 1;
          dataRow[“FieldTwo”] = 2;
          dataRow[“FieldThree] = 3;
          myDataTable.Rows.Add(dataRow);           
     }

     using (SqlConnection connection = new SqlConnection(“connectionstring”))
     {
          
          using (SqlCommand command = connection.CreateCommand())
          {
               connection.Open();
               command.CommandType = CommandType.StoredProcedure;
               command.CommandTimeout = 90;
               command.CommandText = “MyStoredProcedureName;
               command.Parameters.AddWithValue(“@IncomingDataTable”, myDataTable);
               command.ExecuteNonQuery();
          }
     }
}


Step 3:  Create the Table

CREATE TABLE MyTable(FieldOne int NULL, FieldTwo NULL, FieldThree NULL)

 

Step 4:  Create the Stored Procedure

CREATE PROCEDURE [dbo].[MyStoredProcedure]
@MyUserDefinedType MyUserDefinedType READONLY

AS
BEGIN 

    INSERT INTO dbo.MyTable
	([FieldOne], [FieldTwo], [FieldThree])
	
	SELECT [FieldOne], [FieldTwo], [FieldThree] FROM @MyUserDefinedType

END

 

 

NOTES:

Be sure that the DataTable columns are added in the SAME order as they are created in the SQL Server User Type.

How To Configure Log4Net In A C# Application



TODO:

Have you ever wanted to use log4net in your C# application?

 

SOLUTION:

To do so, you need to do 2 things:

 

Step 1:  Add the following entry to the bottom of your AssemblyInfo.cs file

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

 

Step 2:  Add this entry to your web or app.config file

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
 
  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="c:\YOUR DIRECTORY HERE\your log file name.log" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyyMMdd" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="30MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d %-5level %logger{1} : %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>

 

NOTES:

If you already have a "configSections" entry, just add the new key to that section

How To Round A Decimal Down To The Nearest Whole Number Using C#



TODO:

Have you ever wanted to round a number down to the nearest whole number using C#.  Example:  14.789 rounded down to 14

 

SOLUTION:

Math.Truncate(14.789)  //produces 14

 

NOTES:

There are no notes on this topic.

How To Download A File From A Document Library Using The SharePoint Client Object Model



TODO:

Have you ever wanted to download a document from a document library using the SharePoint client object model?

 

SOLUTION:

using SP = Microsoft.SharePoint.Client;

//class and code go here

string saveLocation = "C:\\Temp\\";

//SET YOUR ITEM HERE!!!!!!
SP.ListItem item = null;

using (SP.FileInformation fileInformation = SP.File.OpenBinaryDirect(put your context here, (string)item["FileRef"]))
{
     string fileName = (string)item["FileRef"];
     fileName = string.Concat(saveLocation, fileName.Substring(fileName.LastIndexOf("/")+1)); //get the name of the file based on last '/'

     //now save it
     using (System.IO.FileStream outPutFile = System.IO.File.OpenWrite(fileName))
     {
          fileInformation.Stream.CopyTo(outPutFile);
          fileInformation.Stream.Close();
     }
}

 

NOTES:

There are no notes on this topic

How To Zip A Directory Using C#



TODO:

Have you ever wanted to zip an entire directory using C#?

 

SOLUTION:

using System.IO.Compression;

//class and other code here...

//zip the entire directory
//parameter 1 is the directory to zip
//parameter 2 is the resulting zip file
ZipFile.CreateFromDirectory(directoryToZip, zipFilePath);

 

NOTES:

There are no notes on this topic

How To Open Explorer To A Specific Directory Using C#



TODO:

Have you ever wanted to launch explorer from your C# application to a specified directory?

 

SOLUTION:

using System.Diagnostics;

//class and other code goes here...

//set your location here
string explorerLocation = "C:\\Temp\\MyLocation";

//open the location
System.Diagnostics.Process.Start(explorerLocation);

 

NOTES:

There are no notes on this topic

How To Use Excel.Application.GetSaveAsFilename() Method



TODO:

Have you ever wanted to use the Excel.Application.GetSaveAsFilename method to save your files?


SOLUTION:

Excel.Application excel = new Excel.Application();

FileInfo fileInfo = new FileInfo(txtInputFile.Text.Trim());

//so show the save as, and then filter by current extension only....reason is, that SaveAs() will save in current format, so format changes cannot happen here.
object fileName = excel.GetSaveAsFilename(fileInfo.Name, string.Format("Excel files (*{0}), *{0}", fileInfo.Extension), 1, "Save File Location");

 

NOTES:

This example will display the Save As dialog box.  I limit the box to the current file type, as calling excel.SaveAs(filename) will not change the file type.

How To Add A Custom Pre-Requisite To A Click-Once Application



TODO:

Have you ever wanted to add your own prerequisites to a Click-Once application?

 

SOLUTION:

1.  Follow the instructions here to create a package manifest.

2.  Follow the instructions here to create a product manifest.

3.  Last restart Visual Studio and you will see your prerequisites in the list.

 

NOTES:

There are no notes on this topic.