Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Articles
Search: 

Bonnie Berent's Tips
Bonnie DeWitt, April 1, 2008
Great tips from the .NET developer community compiled by Bonnie Berent.
Summary
Great tips from the .NET developer community compiled by Bonnie Berent.
Description

Interesting Clipboard Functionality
Did you ever need to copy an instantiated class to the clipboard, retrieve it and access a property or method on it? Well, here's how to do it:
Clipboard.SetData("MyClassFormat", new MyClass("42"));
if (Clipboard.ContainsData("MyClassFormat"))
{
	MyClass o = Clipboard.GetData("MyClassFormat") as MyClass;
	if (o != null)
	{
		// do stuff here, such as execute a method
		// o.MyMethod();
		// or set a property
		// o.MyProperty = "xyz";
	}
}
else
{
	MessageBox.Show("Ain't nothing there!!");
}

There's only one gotcha and that is that your class must have the [Serializable] attribute.

from a solution provided by Einar Kvandahl in Message #1135004

Determine new PK after an Insert
You have a couple of options to get the PK value of a newly inserted record. I am assuming SQL Server database in these examples.

First, if you are not using Stored Procs and simply sending an INSERT INTO statement, just add a "SELECT SCOPE_IDENTITY() at the end of your query and be sure to execute your query by reading the result back (into a datareader or dataset for example).

Your query string should look like this:

string Sql = "INSERT INTO MyTable (ColumnOne) VALUES ('Bob') SELECT SCOPE_IDENTITY() AS MyPK";
And, here's the code that shows how to execute and read the results back using a DataReader:
  int MyPK = 0;
  SqlCommand sc = new SqlCommand(Sql, new SqlConnection(this.TestConnection));
  sc.Connection.Open();
  SqlDataReader rd = sc.ExecuteReader(CommandBehavior.CloseConnection);

  while (rd.Read())
  {
	MyPK = Convert.ToInt32(rd["MyPK"]);
  }
Alternatively, if you're using a Stored Proc, you'd use an OUTPUT parameter in your Stored Proc, and a ParameterDirection.InputOutput in your code:
-- The Stored Proc
CREATE PROCEDURE MySP 
          @PK        int     = NULL OUTPUT,
          @ColumnOne char(8) = NULL,
          @ColumnTwo char(4) = NULL
AS
  INSERT MyTable (ColumnOne, ColumnTwo)
         SELECT @ColumnOne, @ColumnTwo

  SELECT @PK = SCOPE_IDENTITY()
And here's how you would call it in your code:
  Command.CommandText = "MySP";
  Command.Parameters.Add("@PK", 0);
  Command.Parameters["@PK"].Direction = ParameterDirection.InputOutput;
  Command.Parameters.Add("@ColumnOne", OneValue);
  Command.Parameters.Add("@ColumnTWo", TwoValue);

  Command.ExecuteNonQuery();

  MyNewPK = Command.Parameters["@PK"].Value;

from two solutions provided by Éric Moreau and Bonnie Berent in Messages #1090511 and #930339

GridView Differences in a WebForm (1.1 vs. 2.0)
In an ASP.NET 1.1 WebForm, one could capture the value of a column in a GridView that was not visible. A simple example:

Say you have a GridView that contains 3 columns: Employee ID, Employee Name, Select Link. The first column is marked as not visible because we don't want to display the Employee ID field.

When the user clicks the Select link in column3, we can capture the row and column as such:

e.Item.Cells[0].Text.Trim()
This would allow us to get the employee ID field when the Select was clicked even though the employee ID field was never visible.

Now ASP.NET 2.0 comes along and this same WebForm cannot be used as is. With the Employee ID column still invisible, a blank value is returned. The only way to make this work is to make the Employee ID visible again. Clearly not a viable workaround.

Here's a better idea:

There's a new GridView property in 2.0 called DataKeyNames. We can set that to our ID column name. Then, in the GridView's SelectedIndexChanged event (which fires if we have any link or command button with a command name of "SELECT"), we can do this:

int MyID = (int)this.MyGridView.SelectedDataKey.Values["MyIDColumn"];

from a solution provided by Kevin Goff in Message #1098765

Setting Focus to a Control at Form Load
Normally, the Focus() method of a control is used to move the focus to that control:
private void MyButton_Click(object sender, System.EventArgs e)
{
    // Move the Focus elsewhere
    this.MySpecialTextBox.Focus();
}
This works at any time, except during Form/Control Load. Then, you simply have to set the ActiveControl:
private void MyForm_Load(object sender, System.EventArgs e)
{
    // Start the Form with the Focus on a certain control
    this.ActiveControl = this.MySpecialTextBox;
}

from a solution provided by Kevin McNeish in Message #1080245

Bonnie DeWitt, Geneva Systems Group
Bonnie is currently one of the principals of Geneva Systems Group. Call her the Senior Software Engineer, or even call her the VP of Engineering. She has no official title at the moment. Bonnie has been writing software in various languages for about 30 years. Bonnie's current focus on C# .NET applications began in early 2002. She has been a Microsoft C# MVP since Oct 2003 and an active member of the .NET online community.
More articles from this author
Bonnie DeWitt, September 1, 2005
Great tips for .NET developers
Bonnie DeWitt, October 1, 2005
Great tips for .NET developers
Bonnie DeWitt, November 1, 2005
Great tips for .NET developers
Bonnie DeWitt, December 1, 2005
Great tips for .NET developers
Bonnie DeWitt, April 1, 2009
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, February 1, 2006
Great tips for .NET developers
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers
Bonnie DeWitt, April 1, 2006
Great tips for .NET developers
Bonnie DeWitt, May 1, 2006
Great tips for .NET developers
Bonnie DeWitt, June 1, 2006
Great tips for .NET developers
Bonnie DeWitt, July 1, 2006
Great tips for .NET developers
Bonnie DeWitt, August 1, 2006
Great tips for .NET developers
Bonnie DeWitt, September 1, 2006
Great tips for .NET developers
Bonnie DeWitt, October 1, 2006
Great tips for .NET developers
Bonnie DeWitt, November 1, 2006
Great tips for .NET developers
Bonnie DeWitt, December 1, 2006
Great tips for .NET developers
Bonnie DeWitt, January 1, 2007
Great tips for .NET developers
Bonnie DeWitt, February 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers.
Bonnie DeWitt, April 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2007
Good tips for .NET developers.
Bonnie DeWitt, May 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, June 1, 2007
Great tips and tricks for .NET developers.
Bonnie DeWitt, July 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, August 1, 2007
Great tips for .NET developers
Bonnie DeWitt, September 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, February 1, 2008
Great tips from the Universal Thread .NET community.
Bonnie DeWitt, March 1, 2008
Great tips for .NET developers selected from the community by Bonnie Berent.
Bonnie DeWitt, January 1, 2006
Great tips for .NET developers