Wednesday, April 27, 2011

SQL Server: Add a Read-Only Account using SQL authentication

I need to create a read-only account using SQL authentication to login SQL Server. There are several steps are needed to prepared and they are shown below:

1. Ensure the authentication of SQL Server

    (1) Right click the SQL Server and select "Properties"

    (2) Under page "Security", click "SQL Server and Windows Authentication mode" and then click button "OK"

2. Create a read-only account

    (1) Right click folder "Security" and select create a new login

    (2) Under page "General", type login name, password for SQL authentication and default database


    (3) Under page "User Mapping", click the designated database and set db_datareader in its role membership

    (4) Click button "OK"

Tuesday, April 19, 2011

ASP.NET: DropDownList do not fire onselectdindexchanged

I add a DropDownList in a web page and the setting is shown below:
<asp:DropDownList ID="DropDownList1" runat="server" Height="35px"             onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="184px"              style="margin-right: 38px">
When I change the value in the DropDownList, it do not trigger the onselectedindexchange method. After some searching, i need to add attribute AutoPostBack
AutoPostBack="true"

Monday, April 18, 2011

Event Handler: Enable Error Message

In SharePoint, event handler is triggered before or after a specific event, A cancel update (properties.Cancel) is an unhandled exception for SharePoint and error message is shown as below:


The following code will be triggered before adding a field and an error message will be shown as below.

public override void FieldAdding(SPListEventProperties properties)
        {
            properties.ErrorMessage = "error”;
            properties.Cancel = true;
        }

By default, the error message will be shown as below when the attributue of CallStack is true in web.config (a xml document defines configuration of the web application). The error message will show the exception detail when admin enables the CallStack attribute


If I update the attribute of CallStack to “false”, the information for debugging will be totally hidden.


References:

Tuesday, April 5, 2011

Excel Service REST: authentication in C#

The service reference of excel service in SharePoint Server 2010 is

http://sp2010/_vti_bin/ExcelService.asmx

I add above service reference in a console application. The code in the application is shown below:

ExcelServiceSoapClient client = new ExcelServiceSoapClient();
client.ClientCredentials.Windows.AllowedImpersonationLevel =           System.Security.Principal.TokenImpersonationLevel.Impersonation;
String url = "http://sp2010/excelFiles/Book1.xlsx";
Status[] status;
String session = client.OpenWorkbook(url, "en-US", "en-US", out status);
When I runs the above code, method OpenWorkbook() throws an MessageSecurityException as below:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate, NTLM'.
The message indicates the problem is related to the authentication method. Go to app.config and change the security settings.

<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" proxyCredentialType="Ntlm"  realm="" />
      <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
 If you do not want to use current window authentication, you can specify a username account.

client.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(username, password, domain);
                client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;    

Monday, April 4, 2011

C#: GridView

I create a GridView in an asp.net web apge. When it runs method DataBind(), an HttpException is shown as below:

The data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.


When assigning a list of custom object to the data source of GridView,  .Net can read the property names of collection objects automatically. 

The above problem can be solved by the following code:

 public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            Car c1 = new Car() { Add = "1", Name = "2"};
            list.Add(c1);
            //data grid
            GridView1.AutoGenerateColumns = true;
            GridView1.DataSource = list;
            GridView1.DataBind();
        }
    }
    class Car
    {
        public String Name { get; set;}
        public String Add {get;  set;}
    }
For more detail about GridView in C#: http://www.dotnetperls.com/datagridview-tips

Sunday, April 3, 2011

C#: Get Data from Clipboard

I write a application read data from clipboard using System.Windows.Froms. Then I discover the method Clipboard.getDataObject() always returns null.

Based on the description from msdn,

The Clipboard class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

The sample code is shown below:

class Program
    {
        static void Main(string[] args)
        {
            Thread thread = new Thread(new ThreadStart(CopyToClipboard));
            thread.TrySetApartmentState(ApartmentState.STA);
            thread.Start();
        }
        static void CopyToClipboard()
        {
            IDataObject clip = Clipboard.GetDataObject();
            if (clip.GetDataPresent(DataFormats.Text))
            {
                Console.WriteLine("" + clip.GetData(DataFormats.Text));
            }
        }
    }

Sunday, March 27, 2011

SPSite: FileNotFoundException was unhandled

I create a console project in Visual Studio 2010.

After adding a reference to Miscrosoft.SharePoint, I add following code in the project:



When it opens a SPSite, FileNotFoundException is thrown as below:


Since the console project set the Platfrom target is X86 as default and I run the project under X64 environment. Therefore, I need to update the Platform target to x64.

1. Right click the project
2. Click Properties
3. Click Build and update the value in Platfrom target