Folk!
How to programattically check if null value exists in database table (using stored procedure)?
I know it's possble in the Query Analyzer (see last SQL query batch statements)?
But how can I pass null value as parameter to the database stored procedure programattically using C#?
Although I can check for empty column (the following code passes string.Empty as parameter but how to pass null value?), I cannot check for null value in the following code snippet:
SQL Queries: -
USE [master]
-
GO
-
-
IF EXISTS (SELECT name FROM sys.databases WHERE name = 'ExampleDatabase')
-
DROP DATABASE [ExampleDatabase];
-
GO
-
-
CREATE DATABASE [ExampleDatabase];
-
GO
-
-
USE [ExampleDatabase];
-
GO
-
-
IF EXISTS (SELECT NAME FROM SYS.TABLES WHERE NAME = 'ExampleTable')
-
DROP TABLE dbo.ExampleTable;
-
GO
-
-
CREATE TABLE
-
dbo.ExampleTable
-
(
-
ID INT IDENTITY(1,1) NOT NULL,
-
UserID INT NULL,
-
Name NVARCHAR(50) NULL,
-
DateOfBirth DATETIME,
-
IsActive BIT,
-
Phone NVARCHAR(50) NULL,
-
Fax NVARCHAR(50) NULL,
-
CONSTRAINT PK_ID PRIMARY KEY(ID),
-
CONSTRAINT UNIQUE_Phone UNIQUE(Phone),
-
CONSTRAINT FK_UserID FOREIGN KEY(UserID) REFERENCES ExampleTable(ID),
-
CONSTRAINT FK_Fax FOREIGN KEY(Fax) REFERENCES ExampleTable(Phone)
-
);
-
GO
-
-
INSERT INTO dbo.ExampleTable
-
(
-
UserID,
-
Name,
-
DateOfBirth,
-
IsActive,
-
Phone,
-
Fax
-
)
-
VALUES
-
(
-
1,
-
'Bill',
-
'12-31-2000',
-
'False',
-
'12345678',
-
'12345678'
-
);
-
GO
-
-
INSERT INTO dbo.ExampleTable
-
(
-
UserID,
-
Name,
-
DateOfBirth,
-
IsActive
-
)
-
VALUES
-
(
-
2,
-
'Larry',
-
'12-31-2005',
-
'True'
-
);
-
GO
-
-
SELECT * FROM ExampleTable;
-
GO
-
-
-
IF EXISTS(SELECT NAME FROM SYS.PROCEDURES WHERE NAME = N'CheckForeignKeyFax')
-
DROP PROCEDURE dbo.CheckForeignKeyFax;
-
GO
-
-
CREATE PROCEDURE dbo.CheckForeignKeyFax
-
(
-
@Fax NVARCHAR(50)
-
)
-
AS
-
DECLARE @ResultFax INT
-
IF EXISTS
-
(
-
SELECT
-
NULL
-
FROM
-
dbo.ExampleTable WITH (UPDLOCK)
-
WHERE
-
ISNULL(Phone, 'NULL') = ISNULL(@Fax, 'NULL')
-
--(Phone IS NULL AND @Fax IS NULL) OR (@Fax = Phone)
-
)
-
BEGIN SELECT @ResultFax = 0 END
-
ELSE BEGIN SELECT @ResultFax = -1 END
-
RETURN @ResultFax
-
GO
-
-
DECLARE @ReturnValue INT
-
EXEC @ReturnValue = CheckForeignKeyFax @Fax = '12345678'
-
SELECT ReturnValue=@ReturnValue;
-
GO
-
-
DECLARE @ReturnValue INT
-
EXEC @ReturnValue = CheckForeignKeyFax @Fax = NULL
-
SELECT ReturnValue=@ReturnValue;
-
GO
-
C# Code: -
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
-
using System.Data.SqlClient;
-
-
namespace WindowsFormsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
private string ConnectionString
-
{
-
get
-
{
-
return @"Persist Security Info=False;Data Source=.\SQLEXPRESS;User ID=sa;Password=asdfglkjh;Initial Catalog=master;";
-
}
-
}
-
-
private int CheckFax(string fax)
-
{
-
int Result = -1;
-
-
try
-
{
-
using (SqlConnection ConnectionSql = new SqlConnection(ConnectionString))
-
{
-
using (SqlCommand CommandSql = new SqlCommand("ExampleDatabase.dbo.CheckForeignKeyFax"))
-
{
-
CommandSql.CommandType = CommandType.StoredProcedure;
-
CommandSql.Parameters.Add(new SqlParameter("@Fax", fax));
-
-
SqlParameter ParameterSql = new SqlParameter("@ReturnValue", DbType.Int32);
-
ParameterSql.Direction = ParameterDirection.ReturnValue;
-
-
CommandSql.Parameters.Add(ParameterSql);
-
-
ConnectionSql.Open();
-
CommandSql.Connection = ConnectionSql;
-
CommandSql.ExecuteScalar();
-
Result = Int32.Parse(CommandSql.Parameters["@ReturnValue"].Value.ToString());
-
ConnectionSql.Close();
-
}
-
}
-
}
-
catch (Exception ex)
-
{
-
MessageBox.Show(ex.ToString());
-
}
-
return Result;
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
MessageBox.Show(CheckFax(textBox1.Text).ToString());
-
}
-
}
-
}
-
Please help!
Check out the DBNull.Value object for passing in a null (or checking against a null value in a DataSet)
In SQL you can use the "is null" to check if a field is null
2 14632 Plater 7,872
Recognized Expert Expert
Check out the DBNull.Value object for passing in a null (or checking against a null value in a DataSet)
In SQL you can use the "is null" to check if a field is null
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Matt |
last post by:
I could use some help dealing with null blobs. I'm
returning a transaction from an Image BLOB field in SQL
Server 2000 using C#. If the transaction exists the value
is returned with out trouble,...
|
by: craigkenisston |
last post by:
I have a generic function that receives a couple of datetime values to
work with. They can or cannot have a value, therefore I wanted to use
null. This function will call a database stored...
|
by: John Hoge |
last post by:
Is it possible to pass a null value to a stored procedure in .net?
I have a search Sproc that can take one of two numbers to search on,
but not both. I use the code below to pass a null value to...
|
by: David Lozzi |
last post by:
Here is the proc:
CREATE PROCEDURE .
@CID as int,
@Netname as nvarchar(25),
@Return as int OUTPUT
AS
IF EXISTS (SELECT DISTINCT netname FROM computers WHERE CompanyID = @CID AND...
|
by: JIM.H. |
last post by:
Hello,
I am calling a stored procedure to update my table.
If one of the date on the screen left empty, I need to send date as null.
Since MyDate=”” gives error in asp.net, how should I do...
|
by: Carl Fenley |
last post by:
I am successfully adding stored procedures to an Access database. However,
I need to be able to check if the stored procedure of the same name already
exists.
Is there a way to do this other...
|
by: vovan |
last post by:
I'm creating DataSet, then 2 DataTables, then DataRelation between those
DataTables.
I populate DataTables with DataAdapters. Data from each table is displayed
in Grids.
For display everything...
|
by: Dan |
last post by:
Hello,
we have an intranet application using Windows Integrated Authentification.
When an user starts the application, he gets a form for inputting data. The
first time he does that, the...
|
by: qwedster |
last post by:
Howdy folks!
I am using stored procedure to see if a value exists in database table and return 0 if exists or else -1, in the following SQL queries.
However how to check if a value (that is...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |