Monthly Archives: August 2011

Create a Popup Input Box with C#


So I decided I wanted to keep the interface of my Overseer project pretty clean, but wanted to get simple input before creating certain objects. For example, a tab name when a user clicks the new tab button.

Here is a sample of what I’m talking about.

Here is the function I created to do this. You’ll need to add a using statement to your project.

using System.Drawing.Drawing2D;


public DialogResult PopupInput(Control ctrl, int border, int length, ref string output)
{

//handle alignment
Point ctrlpt = this.PointToScreen(ctrl.Location);
ctrlpt.Y += 24;
ctrlpt.X += 4;

TextBox input = new TextBox { Height=20, Width=length, Top= border/2,Left=border/2};
input.BorderStyle = BorderStyle.FixedSingle;
//######## SetColor to your preference
input.BackColor = Color.Azure;

Button btnok = new Button {DialogResult = System.Windows.Forms.DialogResult.OK, Top=25};
Button btncn = new Button { DialogResult = System.Windows.Forms.DialogResult.Cancel, Top = 25 };

Form frm = new Form { ControlBox = false, AcceptButton = btnok, CancelButton = btncn, StartPosition = FormStartPosition.Manual, Location = ctrlpt };
frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//######## SetColor to your preference
frm.BackColor = Color.Navy;

RectangleF rec = new RectangleF(0, 0, (length + border), (20 + border));
GraphicsPath GP = new GraphicsPath(); //GetRoundedRect(rec, 4.0F);
float diameter = 8.0F;
SizeF sizef = new SizeF(diameter,diameter);
RectangleF arc = new RectangleF(rec.Location, sizef);
GP.AddArc(arc, 180, 90);
arc.X = rec.Right - diameter;
GP.AddArc(arc, 270, 90);
arc.Y = rec.Bottom - diameter;
GP.AddArc(arc, 0, 90);
arc.X = rec.Left;
GP.AddArc(arc,90,90);
GP.CloseFigure();

frm.Region = new Region(GP);
frm.Controls.AddRange(new Control[] {input,btncn,btnok});
DialogResult rst = frm.ShowDialog();
output = input.Text;
return rst;
}

If you’d like to make a sample app like the one above create a basic windows forms project, toss the above method in there with a button and a textbox on the form and add this code to the on click of the button.


string var="";

if (PopupInput(button1, 4, 75, ref var) == System.Windows.Forms.DialogResult.OK)
textBox1.Text = var;

NOTE: I’m not an awesome coder so any advice would be appreciated. I only post this because I wasn’t able to find something like it on the net.

EDIT: I forgot to add, as you can see by default the box aligns to the bottom of the passed in control. It would be easy to align it to another location, or to add a param that would let you pick. I wanted to keep the code as short as possible, but if anyone wants to see that, let me know.

SQL Backup File location and date time


So, you want to see when you’ve backuped up what/where and when. Certainly something that’s worth keeping an eye on, but how do you find it? Here’s how

select server_name,
    backupset.database_name,
    backupset.backup_start_date,
    backupset.backup_finish_date,
    backupset.backup_size,
    backupmediafamily.physical_device_name
from msdb.dbo.backupset
    inner join msdb.dbo.backupmediafamily
    on backupmediafamily.media_set_id = backupset.media_set_id
order by backupset.backup_finish_date desc

 

So, if you run your backups like I do (just to a named file and not so much media sets) you could use this to do a restore!

Identify your Powershell Script to SQL


I recent wrote a backup script that ran asynchronously and have had some weird problems with it rarely just never stopping. I’m no SQL expert so its hard for me to diagnose on that end. I’ve popped in to SQL Analyzer to try and see what DB its hung up on and just cant figure out what processes are from my job since there are just so many. I noticed there is an Application Name listed and there were some custom names so I got looking in to this and found I can name my connections to help identify which SQL processes are from my script.

For my backup script I was using an SMO Server object which could be constructed with a SQLConnectionInfo object which happened to allow me to set the application name, here is an example of that.

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SMO”) | Out-Null

$sqlapp = New-Object microsoft.sqlserver.management.common.sqlconnectioninfo DBSERVER
$sqlapp.ApplicationName = “Powershell Magic”

$sql = New-Object microsoft.sqlserver.management.smo.server $sqlapp

That’s all there is to it. If you still use the old style connection objects with a connection string you can specify the application name in the connection string.

Data Source=DBSERVER;Initial Catalog=TheDB;Application Name=Powershell Magic;

 

This certainly helps with debugging Powershell SQL scripts!