Monday 27 October 2008

Modal Popup in IE

To have a web page popup in a modal page IE has a javascript function

window.showModalDialog('page url', 'page title', 'options');

The syntax for the options is slightly different from window.open

To specify height and width we would do:

'dialogHeight:300px; dialogWidth:300px'

To make sure the code works cross browser we can check for the showModalDialog function and if it is not available we can you use window.open.

eg.

if (window.showModalDialog)
{
window.showModalDialog('page.html','MyPage','dialogWidth:300px;dialogHeight:3000px');
}else{
window.open('page.html','MyPage','width=300, height=300');
}

Thursday 23 October 2008

email the blog

Hi Guys

Did you know that you can email posts directly to the blog!

When you log in to the blog, go to settings and create an email address.

You can chose to save the posts as a draft or to publish the post straight away.

Happy Email blogging!

 

Glasgow - Proud Host City of the 2014 Commonwealth Games

 

----------------------------------------------------------------------------

Disclaimer:

This message is intended only for use of the addressee. If this message

was sent to you in error, please notify the sender and delete this message.

Glasgow City Council cannot accept responsibility for viruses, so please

scan attachments. Views expressed in this message do not necessarily reflect

those of the Council who will not necessarily be bound by its contents.

----------------------------------------------------------------------------

 

& or && that is the question!

Using && instead of &.

if (phContent.Controls[0].ID == "ucSwJobs" & ((SWJobs)phContent.Controls[0]).CheckForCheckedJobs())
{
ShowMessage("You still have selected jobs in SW Jobs!");
}


If we used & in the above if statement we would get an error because the second evaluation relies on the first one being true. If we used && the first statement would fail and the second statement would throw an error.
By using a double && we stop the second statement being evealuated if the first fails!

Add a confirm popup in asp .net

To add a confirm popup when a button is clicked in asp .net do the following:

In the buttons OnClientClick add the following javascript:

if(confirm('Are you sure?'))
{
return true;
}else{
return false;
}

The confirm method returns a boolean.

The popup displays the message, an Ok button and a canel button.

If Ok is clicked return true allows the button to cause a post back.
Clicking cancel returns false cancel the submit action of the button.

Tuesday 21 October 2008

ConText - Software Developer Text Editor Tool

ConTEXT is a small, fast and powerful freeware text editor, developed to serve as a secondary tool for software developers.

Check out link for features and download.

http://www.contexteditor.org/

Wednesday 15 October 2008

Get your Application to run at windows start-up

Hi,

Creating a registry key to get your application to run at start-up. I put it in the Local machine folder to get it starting for all users. The example shows for local user, not sure if this will run for every user.

Click me

Enjoy!

This was used in the Flexi (MyTime) system.

Tuesday 14 October 2008

Uploading Spread into ASp.Net and inserting Data in a spread sheet into Data BAse

Uploading a file using FileUploader


String Name = FileUpload1.FileName.ToString();
if (FileUpload1.PostedFile != null)
{
fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
SaveLocation = Server.MapPath(fn);
FileUpload1.PostedFile.SaveAs(SaveLocation);
}

Getting connection to Spread Sheet and SQL to get the data from spread sheet and insert into SQl TABLE(Table NAme: Photo Finding)


String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath(fn) + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection con = new OleDbConnection(sConnectionString);
SqlConnection con1 = new SqlConnection("data source=DCSQL2;initial catalog=gGilesDW;integrated security=SSPI;persist security info=False;Trusted_Connection=Yes");
OleDbDataReader oledbrdr = null;
try
{
con.Open();
// Create OleDbCommand object and select data from worksheet Sheet1
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", con);
oledbrdr = cmd.ExecuteReader();
con1.Open();
while (oledbrdr.Read())
{
String Status = Convert.ToString(oledbrdr["Status"]);
String id = Convert.ToString(oledbrdr["ID"]);
query = "INSERT INTO PhotoFinding" + "(Status,[ID]) " + " VALUES ('" + Status + "','" + id + "')";
SqlCommand sqlcmd = new SqlCommand(query, con1);
i = sqlcmd.ExecuteNonQuery();
}
if (i == 1)
{
Label2.Text = "Hai Data Base is Updated Sucessfully";
}
else
{
Label2.Text = "The Process of Updating Failed";
}
}

Monday 13 October 2008

LINQ in Visual Studio 2008

To use LINQ in a project add a 'LINQ to SQL Classes' template.

this opens a .dbml file that you can drag tables onto to make classes and stored procedures to make methods.
When you drag tables across there relationships are detected.

To use linq you need to create a Data Context to connect to your classes and methods.
This is usually called 'Whatever you named your LINQ to SQL Classes' + DataContext, eg.

PDPDataContext db = new PDPDataContext();

Get Data

To get all users you might do somehting like this:

var users: = from o in db.Users
select o;

This would select every record and field from the users table.

You can then bind this like you would a dataset or datatable.

datasource = users;

To insert a record using linq we can do the following:

Create an instance of the user class created by the LINQ to SQL Classes.

User u = new User();

Set the properties:

u.Name = "Jim";

Add User:

db.Users.InsertOnSubmit(u);

Save to Database:

db.SubmitChanges();


Update a record

Get a single record:

User u = (from o in db.User
where o.userid = 2456
select o).Single();

You set the properties as before and save to the database, there is no need to InsertOnSubmit.

db.SubmitChanges();

Examples
You can group and order and use aggregate functions using LINQ, for examples go here

Friday 10 October 2008

Put your application in the system tray

Found this blog. East step by step guide on how to setup your application to run in the System Tray.

http://www.developer.com/net/csharp/article.php/3336751

Enjoy!

Thursday 9 October 2008

Run a DTS package through an application

To run a dts package through code:

Using:
using System.Threading;
using DTS;

Declare:
private Thread dts;

Method:
private void CallDTS()
{
dts = new Thread(new ThreadStart(RunDTSPackage));
dts.Start
}

private void RunDTSPackage()
{
try
{
object obj = new object();

PackageClass csDTS = new PackageClass();
csDTS.LoadFromSQLServer("Server", null, null, DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, null, null, null, "PackageName", ref obj);
csDTS.Execute();
csDTS.UnInitialize();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

You might need to wait until the package is finished running before you carry on and call other methods so check for:
while (dts.IsAlive)
{
}
DoSomething();

Thats it.

Check if an instance of another app is open

The following method checks to see if an instance of a specific application is running.....

using System.Diagnostics;

private bool CheckIfPPlusOpen()
{
Process[] pApps = Process.GetProcesses();
bool bPPlus = false;
foreach (Process p in pApps)
{
if (p.ProcessName.Equals("P+Test"))
{
bPPlus = true;
}
}
if (!bPPlus)
{
GetPPlusAbsenceData();
}
else
{
MessageBox.Show("Please close P+ before refreshing the P+ data!", "Close P+", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
return bPPlus;
}

Stop multiple instances of your windows app running

I needed to stop users from starting up more than one copy of the MyTime app running and I found this link to help.

I changed where the code actually runs, I think its a bit better this way.

1. Open up the Program.cs file.

2. Add System.Threading into the using list.

3. Paste this in the class declaration
private static Mutex mutex;
private static string mutexName = "MyTime.Mutex";

4. Paste this in the main() method before any other code runs.
try
{
mutex = Mutex.OpenExisting(mutexName);

//since it hasn’t thrown an exception, then we already have one copy of the app open.
MessageBox.Show("A copy of MyTime is already open.",
"MyTime", MessageBoxButtons.OK, MessageBoxIcon.Information);

//use this command as we haven't called the run method of the application
Environment.Exit(0);
}
catch
{
//since we didn’t find a mutex with that name, create one
mutex = new Mutex(true, mutexName);
}

//Hook up the Application Exit event to release the mutex so that the app can re-open correctly.
Application.ApplicationExit += new EventHandler(delegate { mutex.ReleaseMutex(); });


5. Thats it. Test it!


Don't think there is much else to this. Any questions let me know.
Free Hit Counter