1

i have a admin users.. when once admin supervisor added by admin and set designation supervisor and department as computer science .. then when again admin add account and try to add admin and set his designation supervisor and department computer science then here i want to show error " designation already exist" ..because every department has one supervisor , one manager and also one senior manager ....not multiple supervisors ,managers i try this sp and code.. sp

ALTER procedure [dbo].[spadmindesig]
@DesigID int
as
if exists(select * from Designation where DesigID=@DesigID)
        return -1
 else
        return 1

and for admin signup

ALTER procedure [dbo].[spadminreg]
@UserName nvarchar(50),
@Password nvarchar(50),

@UserTypeID int,
@DepID int,
@DesigID int,
@emailaddress nvarchar(50),
@PhoneNumber nvarchar(50)

as
if EXISTS(SELECT 1  from Designation where DesigID=@DepID)
begin
select @DesigID as 'SuperVisor'
end
else if EXISTS (select 2 from Designation where DesigID=@DesigID)
begin
select @DesigID as 'Manager'
end
 else if EXISTS (select 3 from Designation where DesigID=@DesigID)
begin
select @DesigID as 'Senior Manager'
end
 else
if exists(select * from Designation where DesigID=@DesigID)
        return -1
 else
        return 1

insert into [Userss](UserName,Password,UserTypeID,DepID,CreateDate,DesigID,Email ,PhoneNumber)
values
(@UserName,@Password,@UserTypeID,@DepID,GETDATE(),@DesigID,@emailaddress,@PhoneNumber)

code

public void AdminSignUp(string Username, string Password, int UserTypeID, int DepID, int desigid, string emailaddress, string PhoneNumber)
{

    db.ExecuteScalar("spadminreg", new object[] { Username, Password, UserTypeID, DepID, desigid, emailaddress, PhoneNumber });

}

public string Admindes( int desigid)
{

     string val=db.ExecuteScalar("spadmindesig", new object[] {  desigid }).ToString();
     return val;
}

button code

protected void Btn_SignUp_Click(object sender, EventArgs e)
{


    try
    {
        //test
       //value = adminsignup.Admindes(Convert.ToInt32(DropDownList2.SelectedValue));
         string val= adminsignup.Admindes(Convert.ToInt32(DropDownList2.SelectedValue));

        if(val=="1")
        {
        adminsignup.AdminSignUp(nametxt.Value, passtxt.Value, Convert.ToInt32(DropDownList1.SelectedValue), Convert.ToInt32(DropDownList2.SelectedValue), Convert.ToInt32(DropDownList3.SelectedValue), mailtxt.Value, numbtxt.Value);
        //GridView1.DataSource=ca.viewadmin();
        Lbe6.Visible = true;
        Lbe6.Text = ("Designation Already Exists.");
        //  GridView1.DataBind();
        }
        else
        {
            lbe5.Visible = true;

             lbe5.Text = ("");

         }
    }
    catch
    {
        lbe5.Visible = true;

       lbe5.Text = ("SIGNUP FAILED.PLEASE TRY AGAIN");

    }
    nametxt.Value = "";
    passtxt.Value = "";

    mailtxt.Value = "";
    numbtxt.Value = "";

}

supervisor account exist in table and when i add supervisor account and set same designation and department then it show me error "signup failed " and i want to display error " designation already exist" and when i delete existing supervisor account from table and then again i add supervisor account then it shows me error ..

object reference not set to an instance of an object
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3265377
  • 47
  • 1
  • 2
  • 10
  • most common cause of this is some object or array index is not there, try to set debug point so that you know what is happening in step-by-step (set debug point, use F10 or F11 key to move step by step) – Usman Waheed Feb 17 '14 at 06:46
  • 2
    Please include the full stack trace including line numbers - and indicate which lines in your code those lines refer to. – Jon Skeet Feb 17 '14 at 06:50
  • Why convert the result of `ExecuteScalar` to a string then compare that value to `"1"` - just return it as an integer or better yet return the comparison of the result to `1` and return the boolean result of the comparison. – tvanfosson Feb 17 '14 at 06:54
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Feb 17 '14 at 07:22

2 Answers2

2

Put a break point and debug.You can find error exactly.

Reason for this :

  • Value is assigning as null for any of the variable.

Chech your line of code where exactly error is coming

Example : DropdownList1.SelectedItem is null ,So this erros is coming

enter image description here

Revan
  • 1,104
  • 12
  • 27
0

I think your drop-down values are not convertible in Int32

if(DropDownList2.SelectedIndex > 0)
{
    string val= adminsignup.Admindes(Convert.ToInt32(DropDownList2.SelectedValue.ToString()));
}
Devraj Gadhavi
  • 3,541
  • 3
  • 38
  • 67
Mehul Bhalala
  • 780
  • 7
  • 16