Category Archives: Tutorials

blog2

HOW TO: Start a New Xcode Project From a Git Repo

I’m currently putting the finishing touches on the Swift edition of Beginning Xcode, when I thought it would be nice to take a break and do some blog writing instead. Specifically, I realised that Xcode doesn’t make it really clear how you go about turning a url for a Git repository, say https://github.com/MattKnott/Bokeh.git into a new project without downloading it.

It’s actually super easy to do this, and save yourself the hassle of managing the download, extracting files etc.

1. Start by choosing a Git repo that you want to work with, I’m going to use the one from my SpriteKit particle effects tutorial, https://github.com/MattKnott/Bokeh.git

2. Next, Open Xcode, and go to  Source Control > Check out

tutorial_xcode_git_1

 

 

 

3. Enter the url of your Git repo and click Next

tutorial_xcode_git_2

 

 

 

 

 

 

4. Finally, choose a location to create the project and click Check Out.

After a short while depending on the size of the project, Xcode will have your project set up, ready to go.

depositphotos_6221884-Database

HOW TO: Write an SQL Webpart

To start with, the tools used. I’m using visual studio 2005, and have the wsssdk installed, the sharepoint 2007 sdk, and the wss 3.0 extenders for Visual Studio 2005.

Start up Visual Studio and create a new webpart. It’ll be called webpart1 by default, lets leave it at that for now. First off, right click the project in the solution explorer, and click add reference. Scroll down and select System.Data. Once you’ve added this we’re ready to begin.

Now, make your code look like this:

using System;
using System.Runtime.InteropServices;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace WebPart1
{
[Guid("493c926d-f885-4965-b72d-ee18e0292630")]
public class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart
{
private string _sqlStatement = "SELECT * from tblTest";
[WebBrowsable(true),
Personalizable(PersonalizationScope.Shared),
WebDisplayName("SQL Statement"),
WebDescription("Query to return a set of data")]
public string SQLstatement
{
get { return _sqlStatement; }
set { _sqlStatement = value; }
}
protected override void CreateChildControls()
{
if (string.IsNullOrEmpty(SQLstatement) || !SQLstatement.ToUpper().TrimStart().StartsWith("SELECT")|| SQLstatement.Contains(";"))
{
Literal lit = new Literal();
lit.Text = "Only single SELECT statement allowed"; Controls.Add(lit); return;
}

DataGrid grid = new DataGrid();

//Attempt connection
using (SqlConnection conn = new SqlConnection("server=SP-TESTdatabasearea; Initial Catalog=spTest; User ID=sharepoint; Password=password; Trusted_connection=yes;"))
{
SqlCommand cmd = new SqlCommand(SQLstatement, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
grid.DataSource = reader;
grid.AutoGenerateColumns = true;
grid.DataBind();
reader.Close();
conn.Close();
}
Controls.Add(grid);
}
}
}

Now just build the control and your webpart should be working well. Lets take a look at some of the key parts.

private string _sqlStatement = "SELECT * from tblTest";
[WebBrowsable(true),
Personalizable(PersonalizationScope.Shared),
WebDisplayName("SQL Statement"),
WebDescription("Query to return a set of data")]
public string SQLstatement
{
get { return _sqlStatement; }
set { _sqlStatement = value; }
}

Here we have created a webpart property to contain the SQL statement. We set the title and description that a user will see when they edit the page in SharePoint and look at the webpart properties.

using (SqlConnection conn = new SqlConnection("server=SP-TESTdatabasearea; Initial Catalog=spTest; User ID=sharepoint; Password=password; Trusted_connection=yes;"))
{
SqlCommand cmd = new SqlCommand(SQLstatement, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
grid.DataSource = reader;
grid.AutoGenerateColumns = true;
grid.DataBind();
reader.Close();
conn.Close();
}
Controls.Add(grid);

Here we instantiate the connection to the SQL database, bind the results to a DataGrid and display the results on the page. Note the server name SP-TESTdatabasearea has two backslashes, this is to counter the escape effect of a backslash. Alternatively you can use @”SP-TESTdatabasearea.

Using a web.config connection string

On the previous blog, someone asked how to do this using a connection string held in a web.config file. Firstly, create your entry in the web.config like this:


Next, instantiate an instance of System.Configuration.ConnectionStringSettings. You will have to reference System.Configuration for this to work.

ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["MyDB"];

Finally modify your SqlConnection to use the new object.

using (SqlConnection conn = new SqlConnection(connectionStringSettings.ConnectionString))