loads of useful information, examples and tutorials pertaining to web development utilizing asp.net, c#, vb, css, xhtml, javascript, sql, xml, ajax and everything else...

 

C-Sharpener.com - Programming is Easy!  Learn Asp.Net & C# in just days, Guaranteed!

Change column name in a MSSQL table

by naspinski 12/25/2008 5:16:00 AM

For some reason, MS does not have a built in command for this?


Not sure why this is as competeing technologies like Oracle and MySQL do, but there is a stored procedure that will get you where you are going:
EXEC sp_rename 'TABLENAME.OLD_COLUMNNAME', 'NEW_COLUMNAME', 'COLUMN';

Just replace TABLENAME.OLD_COLUMNNAME and NEW_COLUMNNAME with your new values.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

sql

A simple way to add a 'View All' sort option to your DataSource in Asp.Net

by naspinski 12/17/2008 7:58:00 AM

Just use a little SQL trick to simplify and add usability to your DataSource

Often times I have integer fields in my database that serve as categories. These categories are very helpful when sorting for the users, butit is sometimes tough to write a simple, one-line query to output ALL of them; I figured an extremely simple way to do this. This may be a little hard to explain, but it is very useful.

For this example, I am going to use a LINQDataSource

<asp:LinqDataSource ID="ldsCases" runat="server" ContextTypeName="dbDataContext" TableName="Cases" Where="Category == @Category">

   <WhereParameters>

     <asp:SessionParameter Name="Category" SessionField="Cat" Type="Int32" />

   </WhereParameters>

</asp:LinqDataSource>


This is using a Session variable to work with the DataSource which will be set the the integer value that corresponds to the category I want to find. Now this will work just fine if we are trying to just look at a single category, but what if we want to look at all of them? It is really quite simple, now, just add one more OR (||) statement to our SQL and set the Session variable to 0 any time we want to call everything:

<asp:LinqDataSource ID="ldsCases" runat="server" ContextTypeName="dbDataContext" TableName="Cases" Where="(Category == @Category) || ((Category * @Category) == 0)">

   <WhereParameters>

     <asp:SessionParameter Name="Category" SessionField="Cat" Type="Int32" />

   </WhereParameters>

</asp:LinqDataSource>

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

asp.net | sql

An error occured: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.

by naspinski 11/15/2008 2:58:00 PM

One simple solution to this error I often face

More often than not, I think everything should be working when this happens. I check to make sure that connections are allowed, I make sure my sql user has proper permissions and is mapped to the correct database and all that good stuff.

But one thing this fails to tell you is when you do not have sql server login enabled (windows only).

Simply go in to SQL Server Management Studio and right click on your instance and choose properties then click on security in the right side and there you will see your Server Authentication select the proper one and apply.

Now remember, many things can cause this error, this won't fix them all. I just thought I would share as this is not something you may think of when reading that error message.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

sql

Incorrect syntax near 'something' in SQL 2005 while doing a 'CREATE USER' or 'CREATE LOGIN'

by naspinski 10/23/2008 7:03:00 AM

Annoying little problem and the simple fix

I was entering the following into my SQL Server Management Express window:
CREATE LOGIN 160thUser
  WITH PASSWORD = 'some_password';
USE name_of_db_to_use;
CREATE USER 160thUser FOR LOGIN 160thUser;

And I kept getting the error:
Incorrect syntax near '160'

Now I often use the same LOGIN and USER, so that was not the problem. What I found it out to be is that SQL 2005 does not allow LOGIN and USER obejcts to start with a number... sooo I just flipped them around to:
CREATE LOGIN User160th
  WITH PASSWORD = 'some_password';
USE name_of_db_to_use;
CREATE USER User160th FOR LOGIN User160th;

And everything worked.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

sql