Share The Code Concept

315days since
Republic Day

SharePoint Utility

Create a sharepoint Utility to make the webservice deployment eazier.

C#.NET

Handling single quotes in C# .net

posted Oct 15, 2008 6:11 AM by Praveen Krishna R

While building some dynamic strings for queries or something you should keep in mind that the single quotes / double quotes will definitely make some problems. 

Code which results in Bug

drPosts = oMainDS.Tables["BlogPostslist"].Select("PostCategory = '" + CategoryName + "'");

In the above case we placed the quotes just around the String Value.
But what happens if it contains a single Quotes. It will Throw Exception, for sure.
The solution which I found on GOOGLE is given below.

Solution

drPosts = oMainDS.Tables["BlogPostslist"].Select("PostCategory = '" + CategoryName.Replace("'", "''") + "'");

Yes replace the lonely single quotes with a couple of single quotes. 
And it resolved my problem.



How to register Javascript programatically ?

posted Sep 2, 2008 3:53 AM by Praveen Krishna R   [ updated Sep 2, 2008 5:34 AM ]

.NET Webpart Example

protected override void CreateChildControls()
{
if (!this.Page.ClientScript.IsStartupScriptRegistered("PageContents" + this.UniqueID))
    {
    string JavsScriptVar = "<script type=\"text/javascript\" >" +
                                        "function GiveMessage ( ){" +
 "alert('hi');" +
                                          "}" +
                                          "</script>";

    this.Controls.Add(new LiteralControl(JavsScriptVar));
    }
}

ASP.NET Example

protected void Page_Load(object sender, EventArgs e)
{
string s = "<script type=\"text/javascript\" >" +
                "function PageContents ( ){" +
                "alert('hi');" +
                "}" +
                "</script>";
    if (!Page.ClientScript.IsStartupScriptRegistered("PageContents"))
    {
        this.Controls.Add(new LiteralControl(s));
    }
}

Verifying

To verify run the solution/project and view the content in a web-browser. Right click and take the source code, then search for the function name you have provided. You will find some other text is appended to your JS function name. This is the UniqueID if you are creating a webpart. Debug and find out if there is any difference between the UniqueID at debug time and while appearing in the browser. If there is difference then do the fix for it (Probably some string replace function will be enough).And you are ready to GO.!

How to Fill a DropDownList from Sharepoint Column in all of the Lists ?

posted Aug 21, 2008 11:41 PM by Praveen Krishna R   [ updated Aug 22, 2008 12:08 AM ]

internal static void FillDropDown(DropDownList ddlObject, string FieldName)
        {
            SPWeb oWeb = null;
            SPFieldChoice oFieldAccessReq = null;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    oWeb = SPContext.Current.Web;

                    foreach (SPList oList in oWeb.Lists)
                    {

                        oFieldAccessReq = (SPFieldChoice)oList.Fields[FieldName];

                        ddlObject.Items.Add(new ListItem(""));

                        foreach (String strChoice in oFieldAccessReq.Choices)
                        {
                            ddlObject.Items.Add(new ListItem(strChoice));
                        }
                    }
                   
                }
                catch (Exception ex)
                {
                   LogError(ex);// You have to write this function.
                }
                finally
                {
                    if (oWeb != null)
                        oWeb.Dispose();
                }
            });
        }

‹ Prev    1-3 of 3    Next ›