Monday, 15 February 2010

Retrieving values from Web.Config

  VB.net
ConfigurationManager.AppSettings("configValue")

Web.Config





      


Friday, 12 February 2010

DevExpress Controls - Handling client side events

After much struggling with using the client side events of most of the DevExpress controls i've tried using, I came across this video from Mehul Harry.

It gives a brief run-through of how the client side events work, which for someone who is used to working on Server side events complete with Intelli-Sense, is a real help.

Thursday, 11 February 2010

Retrieving cell data from AspxGridView using focused rows

This is very useful for instances where there is a Child grid with a datasource that requires a value from the Parent grid.

In this example, we will begin with a Parent grid (gridParent) which is databound to a SQL Database. We are retrieving several values, but the most important for this demo is AppID

In the SettingsBehaviour properties we will need to set AllowFocusedRow to True. Also, we need to set ProcessFocusedRowChangedOnServer to True, as this stops the event being handled client side, and will process the event on the server side.

Next, we need to add the method to handle the FocusedRowChanged event. To get the AppID field we need to feed into the gridChild, we need to call the GetRowValues method, which requires a row index and a column ID.

To get the row index, we create a new aspxGridView instance from the Sender object arguement, which will give us the ability to call the FocusedRowIndex() method.

Then we need to give it the name of the column we require, in this case "AppID"

Wednesday, 10 February 2010

AspxGridview Custom buttons and actions

This post is to describe the process of creating a custom button in a Dev Express AspxGridview and to create a code behind event that will handle it.





The example I am going to use is a simple 'Admin' function which will return a list of Role IDs that the user has. We also want to be able to give our Admins the ability to remove one or more roles from the user's profile.





From a technical side, we will be using an ASPXGridview grid databound to a SQL database, as shown below








To Create the "Remove" button, you will need to add a CustomButton column, which inherits from the GridViewCommandColumn class. This can be done by switching to the ASPX Source view, and adding the following between your <column> tags


<dxwgv:GridViewCommandColumn VisibleIndex="0">

        <
CustomButtons>

            <
dxwgv:GridViewCommandColumnCustomButton   ID="Remove" Text="Remove">

            </
dxwgv:GridViewCommandColumnCustomButton>

        </
CustomButtons>

</
dxwgv:GridViewCommandColumn>

The entire code for the Grid is :

 

<dxwgv:ASPxGridView runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" ID="gridUserRoles" EnableCallBacks="False"><Columns>
    <dxwgv:GridViewCommandColumn VisibleIndex="0">
        <CustomButtons>
            <dxwgv:GridViewCommandColumnCustomButton ID="Remove" Text="Remove">
            </dxwgv:GridViewCommandColumnCustomButton>
        </CustomButtons>
    </dxwgv:GridViewCommandColumn>
    <dxwgv:GridViewDataTextColumn FieldName="RoleID" VisibleIndex="1">
    </dxwgv:GridViewDataTextColumn>
</Columns>
</dxwgv:ASPxGridView>

Next -


We need to define the Event in the code behind. Create a new CustomButtonCallBack Method, which will serve as a handler for any custom actions that fire.


Finally, we use the Event Args of the Gridview custom button, to find our newly created button. The Event args are referenced when then new CustomButtonCallBack method is created, so we dont even need to worry about creating that.


Protected Sub gridUserRoles_CustomButtonCallback(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridView.ASPxGridViewCustomButtonCallbackEventArgs) Handles gridUserRoles.CustomButtonCallback

       If e.ButtonID <> "Remove" Then
           Return

     Else

    ‘Code for removing users role


Saving Changes is not permitted (SQL 2008)

Ive recently upgraded to SQL 2008 and the first major difference i noticed was the inability to save changes to a table schema without dropping the contents of the table first (see dialog box below).
This is unacceptable in most cases, as I need to make changes to existing tables which contain large amounts of data.
After much googling I found a solution from Henry Cordes which did the trick. 

Goto Tools > Options



And then goto Designers > Table and Database Designers and deselect the option 'Prevent saving changes that require table re-creation'

Tuesday, 9 February 2010

Using Enterprise Library's Data Access Block to retrieve dataset

This is basic boilerplate code for the use of the Microsoft Enterprise Library Data Access Block (EntLib DAB) using VB.net to Retrieve data using a Stored procedure.


Imports Microsoft.Practices.EnterpriseLibrary.Data


Try
 'Declare Database - All you need to do is enter the connection string referenced in the  Web.Config. 
 Dim db As Database = DatabaseFactory.CreateDatabase("connectionString")


'Set up the database command and enter the name of the stored procedure you want to use
 Dim dbCmd As DbCommand = db.GetSqlStringCommand("usp_RemoveIndividualSystemID")


'It is important to set the "commandType" to StoredProcedure so it will look for a proc, rather than interpret the String as a SQL statement
dbCmd.CommandType = CommandType.StoredProcedure


'Add the parameters that are required for the proc, in this case, it expects a value called @param, and we are assinging as a type of 'String' and giving it a value of "Parameter Value"
db.AddInParameter(dbCmd, "@param", DbType.String, "Parameter Value")
'Declare a dataset and Execute the db.ExecuteDataSet command, which will fill ds with the data returned from the database
 Dim ds As DataSet = db.ExecuteDataSet(dbCmd)

Catch ex As Exception

'Exception Handling
End Try