Monday, February 20, 2012

Substring Function

For any function there arises three basic question--:
  1. What does it mean?
  2. Why is this?
  3. How it works?
What does it mean?
As we see SubString is a combination of two words "Sub" and "String".
String-: In Computer Science String  means "A set of consecutive characters".
Sub   -: Sub is basically a Latin prefix which has several meaning but the best suitable meaning for here is "part of".
So, Substring means "Part of a string".

Why is this?
So, from definition its clear that the work of substring is to extract a part of string.

How it works? 
Syntax : Substring(index,length)
            Here index denotes the postion from where you want to extract the string and length denotes up to how much character you need to extract.Both contains integer value.
Example :
        int ind = Convert.ToInt32(txtIndex.Text);
        int len = Convert.ToInt32(txtLength.Text);
        Literal1.Text = TextBox1.Text.Substring(ind,len); 


You can download Sample Code from Here 
 



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.