.NET Tip of The Day
Learn one new .NET trick every day
Быстрое пополнение счета телефона      Login or Join

.NET Tips & Tricks Community (ALPHA)

First 1 2 3 4 5  ... Last  RSS

Fill datset without open connection

There is no need 

Connection.Open

Before calling Method

Adapter.Fill(Dataset)


like

 public override DataSet getSMCommentedSubmission(string prodID, string salesID)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {

                SqlCommand cmd = new SqlCommand("sp_WB_Submission", cn);
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                DataSet dset = new DataSet();
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@SqType", SqlDbType.Int).Value = 48;
                cmd.Parameters.Add("@PubPlanID", SqlDbType.Int).Value = null;
                cmd.Parameters.Add("@TypeID", SqlDbType.Int).Value = null;
                cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = null;
                cmd.Parameters.Add("@CommentID", SqlDbType.Int).Value = null;
                cmd.Parameters.Add("@ThemeText", SqlDbType.VarChar).Value = null;
                cmd.Parameters.Add("@StorySynopsys", SqlDbType.VarChar).Value = null;
                cmd.Parameters.Add("@Status", SqlDbType.VarChar).Value = null;
                cmd.Parameters.Add("@Comments", SqlDbType.VarChar).Value = null;
                cmd.Parameters.Add("@TimeStamp", SqlDbType.SmallDateTime).Value = null;
                cmd.Parameters.Add("@PublisherID", SqlDbType.Int).Value = null;
                cmd.Parameters.Add("@PermissionType", SqlDbType.VarChar).Value = null;
                cmd.Parameters.Add("@Checked", SqlDbType.VarChar).Value = null;
                cmd.Parameters.Add("@SMStatus", SqlDbType.VarChar).Value = null;
                cmd.Parameters.Add("@PMStatus", SqlDbType.VarChar).Value = null;
                cmd.Parameters.Add("@ProductionManagerID", SqlDbType.VarChar).Value = prodID;
                cmd.Parameters.Add("@SalesManagerID", SqlDbType.VarChar).Value = salesID;
                cmd.Parameters.Add("@StoryID", SqlDbType.Int).Value = null;
                cmd.Parameters.Add("@Review", SqlDbType.Int).Value = null;
               

                adp.Fill(dset);
               
                return dset;

            }
        }

 

submitted by Ajay
6/11/2008

Never lock on a value type

When locking on a value type it gets boxed, meaning a new object instance is allocated and passed to the lock statement. Thus every time you lock on a value type, you're locking on a new object. Here is an example of bad code:

    private bool flag = false;

 

    public void Method1()

    {

        lock(flag)

        {

            ...

        }

    }

submitted by kostya.ly
6/4/2008

need to use either “using” or explicitly close the connection if we use the datareader

We need to use either “using” or explicitly close the connection if we use the datareader.

Otherwise, it will lead to Connection Pool error. See the underlined text below:

 

However, there are other cases where it is unclear when to close the connection. An example is the ExecuteReader method. This method returns an object that implements the IDataReader interface. The Database base class has a default implementation that returns a DbDataReader object. DbDataReader objects are designed to read specific portions of the data as needed, which requires an open connection. In other words, it is unknown when the application no longer needs the DbDataReader. If the Data Access Application Block methods close the connection before returning the DbDataReader, the DbDataReader becomes useless to the client code. Instead, the DbDataReader methods indicate to the underlying ADO.NET call to automatically close the connection when the DbDataReader is finished. In this situation, it is considered a best practice for the application to ensure that the DbDataReader is closed in a timely fashion, either by explicitly closing the reader with the DbDataReader.close method or by forcing the disposal of the DbDataReader, which results in the Close method being called.

The following code demonstrates a call to the ExecuteReader method. The using statement (Using in Visual Basic) ensures that the DbDataReader object is disposed, which closes the DbDataReader object.

[C#] 

 

Database db = DatabaseFactory.CreateDatabase();

 

DbCommand dbCommand = db.GetSqlStringCommand("Select Name, Address From Customers");

using (IDataReader dataReader = db.ExecuteReader(dbCommand))

{

// Process results

}

submitted by saisuchir
5/15/2008

BrowsableAttribute

when u are creating a control, and u decides that a property or a event should not be displayed in property window.

System.ComponentModel.BrowsableAttribute is there for you, to do the above.

Eg:

private string text;

[Browsable(false)]        //  True is default
 public string Text
      {
         get
         {
            return text;
         }
         set
         {
            text = value;
         }
  }

submitted by dhanaid
5/13/2008
First 1 2 3 4 5  ... Last  RSS
RSS .NET Tip of The Day
Subscribe to receive one tip from the .NET Tips and Tricks Community per day.
Previous Tips of The Day
The best of the .NET Tips & Tricks Community.
.NET Practitioners .NET Tips & Tricks Community
Every .NET practitioner has a trick up in their sleeve. This is the place to share it with other .NET people.
Submit a Tip
Discovered a new trick? Share it with others.
My Tips
Manage tips you authored.