Using Custom Configuration Section and Configuration Manager class in .NET – 1


At times instead of using appSettings section we would like to define our own custom section within configuration file.

 

Suppose we  want to create a custom config section in this following manner

 

<SimplConfigSection id=myID name=myName />

 

For implementing the above custom section we need to first define a class inheriting from ConfigurationSection class

 

Add reference to System.Configuration dll.

 

Create a new config section class in the following manner

 

// inherit the class ConfigurationSection

    class SimpleConfigSection : ConfigurationSection

    {

        public SimpleConfigSection()

        {

        }

 

        // using ConfigurationProperty attribute to define the attribute

        [ConfigurationProperty(“id”,IsRequired = true)]

        public String ID

        {

            get

            { return (String)this[“id”]; }

            set

            { this[“id”] = value; }

        }

        // using ConfigurationProperty attribute to define the attribute

        [ConfigurationProperty(“name”, IsRequired = true)]

        public String Name

        {

            get

            { return (String)this[“name”]; }

            set

            { this[“name”] = value; }

        }

    }

 

 

Now to register your custom section do the following

 

<configSections>

<section name=SimplConfigSection            type=Namespace.SimpleConfigSection, Namespace />

</configSections>

 

 

And to use it within the application

 

SimpleConfigSection simpleConfig = (SimpleConfigSection)ConfigurationManager.GetSection(“SimplConfigSection”);

            string id = simpleConfig.ID;

            string name = simpleConfig.Name;

 

 

That’s it …

Logging off from Oracle\Siebel CRM On Demand Web Service within a .NET application


There are two ways of logging off from an active session within CRM On Demand

 

// pass the url in that case no need to set the header

// only set sessionid           

string logoffUrlString = “servername/Services/Integration;jsessionid=” + SessionID + “?command=logoff”;

HttpWebRequest req = HttpWebRequest)WebRequest.Create(logoffUrlString);         

// make the HTTP call

HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 

 

Or else

 

string logoffUrlString = “servername/Services/Integration?command=logoff”;

HttpWebRequest req = HttpWebRequest)WebRequest.Create(logoffUrlString);         

// set the session id in header

req.Headers[“JSESSIONID”] = SessionID;

// make the HTTP call

HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 

 

Within Web Services Utilization List of Admin Tab in the CRM On Demand application we can see the login and logout info.

 

That’s it ..

“An unexpected error has occurred” error message in SharePoint.


Normally within SharePoint if an error occurs we would be receiving quite an unhelpful error message i.e. “An unexpected error has occurred”. It is because by default the detailed error messages are turned off for security reasons.

 

Here are the steps for getting the full error messages

 

1)      Go to the MOSS site for which you would like to enable it.

Most likely to be found at c:inetpubwwwrootwssVirtualDirectories and a sub directory with your web application’s port number.

 

2)      Locate and open the web.config file for editing.

 

3)      Find out the following entry

<SafeMode MaxControls=200 CallStack=false DirectFileDependencies=10 TotalFileDependencies=50 AllowPageLevelTrace=false>

 

And make following changes to it

 

<SafeMode MaxControls=200 CallStack=true DirectFileDependencies=10 TotalFileDependencies=50 AllowPageLevelTrace=true>

 

4)      And

<customErrors mode=On />

To

<customErrors mode=Off />

 

5)      Save and close web.config.

 

 

That’s it …

Using InfoPath form within SharePoint Workflow


I was trying to implement the InfoPath form for initiation while writing workflow for SharePoint. I must confess it wasn’t easy as i faced so many issues while doing the same. So here i am attaching a document which gives step by step description of how to do that. I hope no one else faces the same issues that i faced  🙂

https://nishantrana.me/wp-content/uploads/2009/02/infopathsharepoint.doc

Bye …

Using SPGridView to bound to list data in SharePoint


These are the few points we need to remember while using SPGridView

With SPGridView we would inherit the same look and feel as the rest of SharePoint site because it makes use of the same CSS classes that the other grids in SharePoint use.

We need to set AutoGenerateColumns=false and explicitly bind the columns.

Create a new asp.net page

Put the following directive to use SPGridView

<%@ Register TagPrefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls”

Assembly=”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %

Declare the control

<SharePoint:SPGridView runat=”server” ID=”grdView”

  AutoGenerateColumns=”false” /> 

Declaring class level variable

public partial class _Default : System.Web.UI.Page

{

    // refer to your site collection

    SPSite mySite = new SPSite(@”http://d-1246:100&#8243;);

    // create class level spweb and splist object

    SPWeb myWeb;

    SPList myList;

Code for Page_Load EventHandler

protected void Page_Load(object sender, EventArgs e)

    {

        myWeb = mySite.OpenWeb();

        myList = myWeb.Lists[“ListName”];

        if (!Page.IsPostBack)

        {

            BindToGrid(myList, grdPropertyValues);

        }

    }

Code for BindToGrid method

private void BindToGrid(SPList myList, SPGridView gridView)

    {

        //grdView.Columns.Clear();

        // get all the listitem

        SPListItemCollection results = myList.Items;

        // create the datatable object

        DataTable table;

        table = new DataTable();

        table.Columns.Add(“Type”, typeof(string));

        table.Columns.Add(“Name”, typeof(string));

        table.Columns.Add(“Created”, typeof(string));    

        // Create rows for each splistitem

        DataRow row;       

        foreach (SPListItem result in results)

        {

            row = table.Rows.Add();

            row[“Type”] = result[“Type”].ToString();

            row[“Name”] = result[“Name”].ToString();

            row[“Created”] = result[“Created”].ToString();   

        }

        // create the bound fields

        SPBoundField boundField;

        boundField = new SPBoundField();

        boundField.HeaderText = “Type”;

        boundField.DataField = “Type”;

        boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center;

        boundField.ItemStyle.Wrap = false;

        gridView.Columns.Add(boundField);

 

 

        boundField = new SPBoundField();

        boundField.HeaderText = “Name”;

        boundField.DataField = “Name”;

        gridView.Columns.Add(boundField);

 

        boundField = new SPBoundField();

        boundField.HeaderText = “Created”;

        boundField.DataField = “Created”;

        gridView.Columns.Add(boundField);

       

        gridView.AutoGenerateColumns = false;

        gridView.DataSource = table.DefaultView;

        gridView.DataBind();  

    }

 

That’s it …

Gridview SelectedIndexChanged Showing no Data


It could happen if we are dynamically creating DataTable and bounding it to our GridView. Searching for it if found the following information on one of the blog

 

The GridView (and actually, all our data controls) does not save data items across postbacks.  This reduces ViewState (if the objects are even serializable) and enables garbage collection to happen and clean up your objects.  So, when you click the button to post back, the GridView has not called DataBind and therefore your data item isn’t there.  This is what you’ve discovered.

Selection is handled slightly differently than choosing an edit item, sorting, paging, or other functions: because those other functions require a change in the control tree or a query to the data source to get different data, they cause a DataBind to happen automatically at some point before the control renders again.  You can handle RowDataBound and get your data item.  Selection, however, doesn’t require new data or a different control tree (all that usually changes is the style properties on the row), so it will never call DataBind.  This makes selection very fast.  It also means you won’t get a DataBound or RowDataBound event unless you call DataBind yourself.  So you can’t use that event to get your data item unless you call it yourself.  If you need the data item, you can call DataBind yourself directly to get it, then handle OnRowDataBound, check the row index against the SelectedIndex, and get the data item.

However, this is kind of the shotgun method- it will get you what you want, but the performance has decreased because you’ve called DataBind again.  Also, if the data in the data store has been modified between your calls to databind, the row index that was the selected item may now point to a different row or just different data.  What data are you really looking for from the selected data item?  Is it just one or two fields?  If so, consider using DataKeyNames to store these fields, and then you can get to them via the DataKeys property.  If you don’t want to suffer the hit to ViewState from DataKeyNames, consider getting the data directly from the controls in the row of the GridView.

So I followed the suggestion highlighted in yellow and was able to retrieve the value for the selected row.

 

  protected void grdPropertyValues_SelectedIndexChanged(object sender, EventArgs e)

    {       

        GridViewRow row = grdPropertyValues.SelectedRow;

        // Get the selected row index

        iRownum = grdPropertyValues.SelectedIndex;

        // call the method that dynamically creates the DataTable and

        // bind the gridview to it

        BindConferenceDayToGrid(myList, grdPropertyValues);

    }

 

 

protected void grdPropertyValues_RowDataBound1(object sender, GridViewRowEventArgs e)

    {

        // getting the GridViewRow

        GridViewRow row = e.Row;

        // if RowType is DataRow and RowIndex is our selected row’s index

        if (e.Row.RowType == DataControlRowType.DataRow && row.RowIndex == iRownum)

        {       

                DataRowView drv = (DataRowView)e.Row.DataItem;

                Response.Write(“The value we need “+drv.Row[1].ToString());          

        }      

    }

 

That’s it ..

 

 

 

Nishant Rana's Weblog

Everything related to Microsoft .NET Technology

Skip to content ↓