Friday, February 17, 2012

An error has occurred during report processing. Exception has been thrown by the target of an invocation. Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

Yesterday I was using RDLC to create a matrix report and encountered by this problem
 "  An error has occurred during report processing.
        Exception has been thrown by the target of an invocation.
            Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."
Basically this problem arises if a primary or foreign key defined in Table Adapter of DataSet which is used as Data Source for Report.
To resolve this just go to the relevent dataset and search for the primary key and just delete that.


Hope this will help you.

Monday, February 13, 2012

How To get Column Value From DataSet


This example shows how to get particular column value from a dataset. Even  We can get all values of that particular field using loop construct.

query = "SELECT [EnrollmentNo] FROM [vw_NewAttenInfo]";
SqlDataAdapter adp = new SqlDataAdapter(query,con);
DataSet ds = new DataSet();
adp.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count;i++)
{
…………
…………
string st = ds.Tables[0].Rows[i]["EnrollmentNo"].ToString();
……
……
}

Monday, February 6, 2012

Restore failed for Server - RESTORE HEADERONLY is terminating abnormally

Error Detail..
Very often you will encountered by this error.This will happen due to many reasons.
Before searching for any other solution you must be sure about these things.

1. Backup copy of Database is good.
    -Try to restore the backup in test database in the same server from where  it has been taken.If its restored   successfully then backup copy is good.If you feel backup is corrupted then take a fresh backup.

2. Backup copy doesn't get corrupted during download.
    -Backup may get corrupted during downloading from FTP etc. If you are using FTP then use it in binary mode to copy the backup file into local drive
3. Backup was taken on a higher version of SQL Server and restoration is being done
   on the lower version.
To know your Sql Server version run "Select @@Version" command.

If the versions are different then update the SQL Server of system on which you are trying to restore the backup

Thursday, February 2, 2012

LPAD or RPAD in Sql Server


Before proceeding we will go through the definition of LPAD and RPAD.
Defintion--:
LPAD(column|expression,n,’string’) --> Pads the character value right-justified to a total width of n character positions.
RPAD(column|expression,n,’string’) ) --> Pads the character value left-justified to a total width of n character positions.

To implement these functions we have no any direct method in sql server but we can implement it with the combination of LEFT or Right and REPLICATE function.

Here's the solution --:
DECLARE @test VARCHAR(3)
SET @test = '143'
SELECT
RIGHT(REPLICATE('*',10) + @test ,10) AS RPAD,
LEFT(@test + REPLICATE('*',10) ,10) AS LPAD 

Your output will be look like this,

ISNULL() in LINQ

Various times we need to implement the functionality of ISNULL() in LINQ. There is no any direct method which implement this but we can do it very easily by using DefaultEmpty method and ternary operator.

In this example,we will focus on Reg_No which is an auto increment field.To implement auto increment we can just write "Reg_No = data.Registrations.Max(re => re.Reg_id) + 1,"  . But this will give error when table is blank or the field returns null. To solve this we need ISNULL functionality.
So, here is the solution for u --:
Registration reg = new Registration()

{

Reg_No = data.Registrations.DefaultIfEmpty().Max(re => (re.Reg_id == null ? 0 : re.Reg_id)) + 1,

 Reg_date = TxtDate.Value,

Reg_Prog_id = Convert.ToInt32(ddlprogram.SelectedValue),

Reg_Course_id = Convert.ToInt32(ddlCourse.SelectedValue),

  };

Still face problem then ask me.Till Enjoy Coding.

Wednesday, February 1, 2012

Creating Schema

What is Schema ?
A database schema is a way to logically group objects such as tables, views, stored procedures etc. Think of a schema as a container of objects.
You can assign a user login permissions to a single schema so that the user can only access the objects they are authorized to access.
Schemas can be created and altered in a database, and users can be granted access to a schema. A schema can be owned by any user, and schema ownership is transferable.

Create New Schema
Goto Security option of desired database -->Right Click --> New  --> Schema


Now in opened window write your schema name and click OK.
To check your created schema goto Security ---> Schema.

Now its Time to Add schema to the table.For this goto the properties window of desired table, in schema option change the schema. By default schema is dbo.
To see the effect Refresh Tables the view the result.