Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have the following stored procedure which tests if table has primary key or no
SQL
ALTER Procedure [dbo].[HasPrimaryKey]
@TableName nvarchar(255)
as
select  case when  name<>'' then   1 else 0 end
FROM   sysobjects
WHERE    xtype = 'PK'
AND parent_obj = OBJECT_ID(@TableName )


i call this procedure in Ado.net as follow:

C#
public bool HasPrimaryKey(string TableName)
       {
           int result =0;
           using (SqlConnection connection = new SqlConnection(connection_string))
           {
               connection.Open();
               SqlCommand command = new SqlCommand(HasPrimaryKey, connection);
               command.CommandType = CommandType.StoredProcedure;
               command.Parameters.Add(@TableName, SqlDbType.VarChar).Value = TableName;
               result = (int)command.ExecuteScalar();
               connection.Close();

           }
           if (result == 1)
               return true;
           else

               return false;
       }


my question is when the procedure returns 1 and select returns value every thing goes ok but when select return no value i got the exceptionObject reference not set to an instance of an object.
what should i do?
Posted
Updated 3-Jan-15 2:28am
v5
Comments
Arjsrya 30-Dec-14 4:21am    
What is happening when the table has no primary key?

1.If the table has primary key it will return 1
2.If the table has no primary key then it will return 0

If you don't pass the correct table name to the store procedure then obviously it will throw exception or return null so how are you handling this problem in your code?
oula alsheikh 30-Dec-14 5:51am    
iam sure that the table name is correct its not my problem
i got the exception maybe when there is no select
available am i right?
[no name] 30-Dec-14 6:53am    
If your table is not having any value then also you can return some customized value like 0. In this way your issue will be resolved.

Hi,

just change your stored procedure to return value when the table doesn't existe :)

SQL
alter procedure [dbo].[HasPrimaryKey] @TableName nvarchar(255)
as
begin
    if exists(
        select  top 1 1
        from   sysobjects
        where    xtype = 'D'
        and parent_obj = object_id(@TableName))

        return 1
    else
        return 0
end
 
Share this answer
 
Instead of stored procedure you can use function.
you can return value from function.

SQL
CREATE FUNCTION dbo.HasPrimaryKey (@TableName nvarchar(255))
RETURNS INT
AS BEGIN
    DECLARE @result int
    SET  @result = 0 
    select @result = 1 
        from   sysobjects
        where    xtype = 'D';
        and parent_obj = object_id(@TableName)
    RETURN @result
END
 
Share this answer
 
v2
Comments
oula alsheikh 3-Jan-15 8:35am    
i tried both solutions and the problem still appears
Sumit Jawale 4-Jan-15 23:44pm    
You can try one more thing, instead of taking int variable for result try it with string.

public bool HasPrimaryKey(string TableName)
{
string result = "";
using (SqlConnection connection = new SqlConnection(connection_string))
{
connection.Open();
SqlCommand command = new SqlCommand(HasPrimaryKey, connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(@TableName, SqlDbType.VarChar).Value = TableName;
result = convert.ToString(command.ExecuteScalar());
connection.Close();

}
if (result == "1")
return true;
else
return false;
}
oula alsheikh 5-Jan-15 7:30am    
Thanks very much Sumit i think it is going to be unsolved
what confused me that i have another function that is similar to that function and it works in normal way but this didn't what is the reason ?
Sumit Jawale 5-Jan-15 7:39am    
In your other Function, are you calling same procedure "HasPrimaryKey" ??
oula alsheikh 6-Jan-15 7:09am    
No iam using another procedure but it is written in same way and it works fine

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900