In T-SQL:
IF EXISTS(SELECT * FROM dbo.Table1 WHERE Name = @Name AND FromDate = @FromDate AND ToDate = @ToDate)
RAISERROR (N'Values already exist', 10, 1)
ELSE
INSERT INTO dbo.Table1(Name, FromDate, ToDate)
VALUES(@Name, @FromDate, @ToDate)
And then call this from a parametrized query in your VB.NET code.
For details about RAISERROR, see the MSDN docs: http://msdn.microsoft.com/en-us/library/ms178592.aspx. The value "10" here is the severity, the "1" is the state. You can see a list of all available severities and states in the MSDN docs.
Or wrap the whole statement into a stored proc that deals with all of this, and just call that stored proc.
UPDATE:
Create a stored proc:
CREATE PROCEDURE dbo.InsertData(@Name VARCHAR(50), @FromDate DATETIME, @ToDate DAETIME)
AS BEGIN
BEGIN TRANSACTION
IF EXISTS(SELECT * FROM dbo.Table1 WHERE Name = @Name AND FromDate = @FromDate AND ToDate = @ToDate)
ROLLBACK TRANSACTION
RAISERROR (N'Values already exist', 10, 1)
ELSE
INSERT INTO dbo.Table1(Name, FromDate, ToDate)
VALUES(@Name, @FromDate, @ToDate)
COMMIT TRANSACTION
END
and then call that from your code (I'm not fluent in VB.NET - this is C#, but should be simple enough to translate):
using(SqlConnection _con = new SqlConnection('your connection string here'))
{
using(SqlCommand _cmd = new SqlCommand("InsertData", _con)
{
_cmd.CommandType = CommandType.StoredProcedure;
// add parameters as necessary
_cmd.Parameters.AddWithValue("@Name", "Your Name");
_cmd.Parameters.AddWithValue("@FromDate", "20100101");
_cmd.Parameters.AddWithValue("@ToDate", "20100331");
try
{
// open connection, execute stored proc, close connection
_con.Open();
_cmd.ExecuteNonQuery();
_con.Close();
}
catch(SqlException sqlexc)
{
// handle SQL exception
}
}
}
That nicely encapsulates everything into a single method - does that work for you??