Posted on Leave a comment

Sorting Gridview (Get it working quick!)

There are lots of references out there for getting a sortable gridview up, however I feel each of them are missing certain bits of information which make them incomplete. I ended up having to use three different references before I could get mine working!

Firstly, the stuff you are here for, the complete code to get a sortable gridview up and working! Below the code are the details you may/may not want to be bothered with.

.aspx.cs file

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
private string _connectionString;
private SqlConnection _conn;
private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvSortable.DataSource = getData();
gvSortable.DataBind();
}
}

private DataSet getData()
{
_connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
_conn = new SqlConnection(_connectionString);
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM tbl_user", _conn);

DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}

public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;

return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}

protected void sortGv(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;

if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}

private void SortGridView(string sortExpression, string direction)
{
DataTable dt = getData().Tables[0];
DataView dv = new DataView(dt);

dv.Sort = sortExpression + direction;
gvSortable.DataSource = dv;
gvSortable.DataBind();
}
}

.aspx file

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<title>Sorting Gridview</title>
</head>
<body>
<form id="form1" runat="server">

<asp:GridView ID="gvSortable" runat="server" Width="100"
AutoGenerateColumns="false"
AllowSorting="true" OnSorting="sortGv">
<Columns>
<asp:boundfield DataField="user_id" HeaderText="Id" SortExpression="user_id" />
<asp:boundfield DataField="name" HeaderText="Name" SortExpression="name" />
<asp:boundfield DataField="age" HeaderText="Age" SortExpression="age" />
</Columns>

</asp:GridView>

</form>
</body>
</html>

Add into the webconfig

<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Data Source=YOUR_SERVER_URL_HERE;Initial Catalog=YOUR_DB_NAME;Persist Security Info=True;User ID=YOUR_USER_NAME;Password=YOUR_PASSWORD;" providerName="System.Data.SqlClient"/>
</connectionStrings>

If you stick all of this code into your project and have a table (tbl_user) with user_id (int), name (varchar) and age (int) then your gridview will be up and working already!

You can choose to divert from the above and go with different options, but for seeing it working the code above is all that you need.

I’m going to revisit this post later to add in some options and comments however for now I have to go finish the project that I needed this sortable gridview for in the first place!

Posted on 2 Comments

Change database owner in SQL server
(single and multiple items)

Changing a single table owner:

Exec sp_changeobjectowner ‘dbo12323.my_table’,’dbo’

dbo12323 = make this whatever the table currently has as the db owner
dbo = the new db owner

Changing multiple items owners:

SELECT * from sysobjects where uid = user_id(‘UseNAme’)
declare @Return int
exec @Return = sp_configure ‘allow updates’, ‘1’
SELECT @Return as ‘Returned Code’
GO
reconfigure WITH OVERRIDE
GO
DECLARE @Rows int, @Error int
BEGIN TRANSACTION
update sysobjects set uid = user_id(‘dbo’) where uid = user_id(‘UseNAme’)
SELECT @Error = @@Error, @Rows = @@RowCount
SELECT @Rows as ‘#Rows’
IF @Rows > 0
BEGIN
SELECT @Rows AS ‘#Rows’
COMMIT TRANSACTION
END
else
BEGIN
SELECT @Error AS ‘Error #’
ROLLBACK TRANSACTION
END

exec sp_configure ‘allow updates’, ‘0’
reconfigure WITH OVERRIDE
go

Posted on 1 Comment

Babbel – Learn Italian easily in this flex based environment

I’ve ‘started’ to learn several different languages, all ending up in the same place that I didn’t want to spend the time or money taking classes at a school or a lot of money on a set of training-up cd’s. Recently I’ve taken to learning Italian and started by purchasing a rather in-expensive cd from Earworms (warning, the site is not high on the ‘pretty’ scale) whose method actually works really well. It combines the learning experience with a base of music, the same concept as putting the alphabet to a tune to help it stick in your mind.


As of yesterday however, I discovered Babbel.com, a flex based (+1 point) online language learning tool (+1 point) that is free!(+10 points!!) I registered and had a play through two of ‘packages’ and found the user-experience and ability to learn from it to be excellent. They start off by teaching you single words, which are each associated to an image, they then eventually go on to teach you to memorise them and learn to understand them when spoken within phrases. By the end I was very pleasantly surprised at how much I got through and how much was easily retained into memory. They also marked one of my finished exercise packages as needing a refresher in 4 days time, which I think is great to keep you interested and remembering previous lessons. So there’s my bit raving about how great it is for learning a new language, which isn’t just available for Italian either, but so far it also has German, French and Spanish.

I’m quite pleased to see it’s been created with Flex. It’s allowed them to create a fluid learning step-through environment, making it easy to learn the language. It’s also good to see that they’ve made it really simple for the community to contribute to it. On the home page, for example, there is an area of phrases that require images, and you as a user can click one of these phrases and choose to upload an image that you believe describes this phrase best. You can also mark images that others have uploaded as good/bad as you go through the exercises, although what effect that ultimately has on the image you’ve marked I’m not quite sure yet. All in all I still have to have a thorough play in it, but it is by far the best tool I have ever used for learning a language and I highly recommend you give it a try as well.

Ciao!