"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Sunday, February 27, 2011

Binding Grid Using Store procedure

Hi,
Last day i had given 1 hour class to freshers.On the half of the way i gone through this topic also,anyway i'm not describing what i teach there.Just displaying the code how to achieve it,with a mind will help somebody.



   try
    {
     SqlCommand cmd = new SqlCommand("[SP Name]", new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"));
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add("CustomerID", txtCustID.Text);
      cmd.Connection.Open();
      // Grid Name: Grid
      Grid.DataSource = cmd.ExecuteReader();
      Grid.DataBind();
      cmd.Connection.Close();
      cmd.Connection.Dispose();
    }
    catch (Exception ex)
    {
     lblStatus.Text = ex.Message;
    }


Have a nice day... 'N happy Coding :)

Sunday, February 20, 2011

Rounding Number in C#,[System.Math.Floor , System.Math.Ceiling, System.Math.Round]

 Hi,
i know this is an old topic but still some people searching and frequent question in Forums also.Anyway lets moves forward.
We can approach this requirement by 3 way, depending upon need because all are giving different result. :)
        1.System.Math.Floor 
        2.System.Math.Ceiling
        3.System.Math.Round
 
1.System.Math.Floor
System.Console.WriteLine("Floor "+ System.Math.Floor(2.1));
System.Console.WriteLine("Floor " System.Math.Floor(2.5));

Answers:
Floor 2
Floor 2
 
2.System.Math.Ceiling
System.Console.WriteLine("Ceiling " + System.Math.Ceiling(2.1));
System.Console.WriteLine("Ceiling " + System.Math.Ceiling(2.5));

Answer:
Ceiling 3
Ceiling 3
3.System.Math.Round
System.Console.WriteLine("Round " + System.Math.Round(1.1));
System.Console.WriteLine("Round " + System.Math.Round(2.5));

Answer:
Round 1
Round 3
 
 

Have a nice day... 'N happy Coding :)

Monday, February 14, 2011

Coalescing operator in C#

 Coalescing operator in C#
 The Coalescing operator is short cut instead of IF condition.

Working

Here I am just going to show how coalescing operator replace ternary operator. i.e not comparing ternary operator with coalescing operator.

Ternary Operator

Syntax
Type variable = booleanCondition ? exp1 : exp2;
Ternary operator assign exp1 to variable if the booleanCondition return true or exp2 if booleanCondition return false.Consider Case were I am coding with the nullable type using ternary opertor.
 Example
Nullable<int> a = null;
Nullable<int> b = 10;
int c = a==null ? b.Value : a;

Coalescing operator

Coalescing operator work some what similar to ternary operator but it works only with the Nullable types only. so its sort hand operator to deal with Nullable types only.
Syntax
Type variable = nullalbeTypeVariable1.Value ?? nullableTypeVariable2.Value;
Operator its check value of nullalbeTypeVariable1 if its as real value rater tan null it assin value to variable else assign value of nullableTypeVariable2.
 Example
 Nullable<int> a = null;
Nullable<int> b = 10;
int c = a.Value ? b.Value : a;

Summary 

Coalescing operator deals with Nullable type and its sort for long ternary operator expression.

 Have a nice day... 'N happy Coding :)

Saturday, February 12, 2011

Error: A network-related or instance-specific error occurred while establishing a connection to SQL Server.

 Error: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

If you encounter above error, while running your VS.Net, than first thing you should do is to check your web.config file to make sure that connection string is correct.

This problem might occur if you have recently
a.    Check your PC Name
b.    Installed new sql server instance but have not updated your web.config file with correct instance name.


Example:
Earlier you were using instance name "PCName\SQLExpress" but due to new sql server instance installation your instance name changed to "PCName\NewInstanceName". 

Below is sample connection string, where text in red is cause of problem.
Data Source=PCName\SQLInstance;Initial Catalog=DBName;Integrated Security=True
 

SQL Optimization Tips

 SQL Optimization Tips 

• Use views and stored procedures instead of heavy-duty queries.
This can reduce network traffic, because your client will send to
server only stored procedure or view name (perhaps with some
parameters) instead of large heavy-duty queries text. This can be used
to facilitate permission management also, because you can restrict
user access to table columns they should not see.

• Try to use constraints instead of triggers, whenever possible.
Constraints are much more efficient than triggers and can boost
performance. So, you should use constraints instead of triggers,
whenever possible.

• Use table variables instead of temporary tables.
Table variables require less locking and logging resources than
temporary tables, so table variables should be used whenever possible.
The table variables are available in SQL Server 2000 only.

• Try to use UNION ALL statement instead of UNION, whenever possible.
The UNION ALL statement is much faster than UNION, because UNION ALL
statement does not look for duplicate rows, and UNION statement does
look for duplicate rows, whether or not they exist.

• Try to avoid using the DISTINCT clause, whenever possible.
Because using the DISTINCT clause will result in some performance
degradation, you should use this clause only when it is necessary.

• Try to avoid using SQL Server cursors, whenever possible.
SQL Server cursors can result in some performance degradation in
comparison with select statements. Try to use correlated sub-query or
derived tables, if you need to perform row-by-row operations.

• Try to avoid the HAVING clause, whenever possible.
The HAVING clause is used to restrict the result set returned by the
GROUP BY clause. When you use GROUP BY with the HAVING clause, the
GROUP BY clause divides the rows into sets of grouped rows and
aggregates their values, and then the HAVING clause eliminates
undesired aggregated groups. In many cases, you can write your select
statement so, that it will contain only WHERE and GROUP BY clauses
without HAVING clause. This can improve the performance of your query.

• If you need to return the total table's row count, you can use
alternative way instead of SELECT COUNT(*) statement.
Because SELECT COUNT(*) statement make a full table scan to return the
total table's row count, it can take very many time for the large
table. There is another way to determine the total row count in a
table. You can use sysindexes system table, in this case. There is
ROWS column in the sysindexes table. This column contains the total
row count for each table in your database. So, you can use the
following select statement instead of SELECT COUNT(*): SELECT rows
FROM sysindexes WHERE id = OBJECT_ID('table_name') AND indid < 2 So,
you can improve the speed of such queries in several times.

• Include SET NOCOUNT ON statement into your stored procedures to stop
the message indicating the number of rows affected by a T-SQL statement.
This can reduce network traffic, because your client will not receive
the message indicating the number of rows affected by a T-SQL statement.

• Try to restrict the queries result set by using the WHERE clause.
This can results in good performance benefits, because SQL Server will
return to client only particular rows, not all rows from the table(s).
This can reduce network traffic and boost the overall performance of
the query.

• Use the select statements with TOP keyword or the SET ROWCOUNT
statement, if you need to return only the first n rows.
This can improve performance of your queries, because the smaller
result set will be returned. This can also reduce the traffic between
the server and the clients.

• Try to restrict the queries result set by returning only the
particular columns from the table, not all table's columns.
This can results in good performance benefits, because SQL Server will
return to client only particular columns, not all table's columns.
This can reduce network traffic and boost the overall performance of
the query.
1.Indexes
2.avoid more number of triggers on the table
3.unnecessary complicated joins
4.correct use of Group by clause with the select list
5 In worst cases Denormalization


Index Optimization tips

• Every index increases the time in takes to perform INSERTS, UPDATES
and DELETES, so the number of indexes should not be very much. Try to
use maximum 4-5 indexes on one table, not more. If you have read-only
table, then the number of indexes may be increased.

• Keep your indexes as narrow as possible. This reduces the size of
the index and reduces the number of reads required to read the index.

• Try to create indexes on columns that have integer values rather
than character values.

• If you create a composite (multi-column) index, the order of the
columns in the key are very important. Try to order the columns in the
key as to enhance selectivity, with the most selective columns to the
leftmost of the key.

• If you want to join several tables, try to create surrogate integer
keys for this purpose and create indexes on their columns.

• Create surrogate integer primary key (identity for example) if your
table will not have many insert operations.

• Clustered indexes are more preferable than nonclustered, if you need
to select by a range of values or you need to sort results set with
GROUP BY or ORDER BY.

• If your application will be performing the same query over and over
on the same table, consider creating a covering index on the table.

• You can use the SQL Server Profiler Create Trace Wizard with
"Identify Scans of Large Tables" trace to determine which tables in
your database may need indexes. This trace will show which tables are
being scanned by queries instead of using an index.

• You can use sp_MSforeachtable undocumented stored procedure to
rebuild all indexes in your database. Try to schedule it to execute
during CPU idle time and slow production periods.
sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?')"



Have a nice day... 'N happy Coding :)