Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a adim users.. when once adim 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 " degiantion already exist" ..beacuse every department has one supervsior , one manager and also one senior manager ....not multilpe supervsiors ,managers i try this sp and code.. sp

SQL
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
C#
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

C#
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 = "";

}


supervsior account exist in table and when i add supevsior account and set same desginaation and deprtment then it show me error "signup failed " and i want to display error " desgination already exist" and when i delete existing supervsior account from table and then again i add supervsior account then it shows me error ..

object reference not set to an instance of an object
Posted
Comments
s#@!k 17-Feb-14 2:43am    
can you specify in which line your are getting the exception.Your error says some object your going to use was killed before using.
Sergey Alexandrovich Kryukov 17-Feb-14 2:48am    
In what line?
—SA
Diya Ayesa 17-Feb-14 6:31am    
in catch line
Sergey Alexandrovich Kryukov 17-Feb-14 11:06am    
No. Not even a nice try.
—SA
JoCodes 17-Feb-14 10:47am    
Which line the exception occurs and whats the Complete error description. Debug and see before it reaches the Catch block.

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 
v2
Comments
Maciej Los 17-Feb-14 18:19pm    
+5
Sergey Alexandrovich Kryukov 17-Feb-14 18:31pm    
Thank you, Maciej.
—SA
Try the following code. I guess you misplaced the error message.
And You gotta validate input values.

C#
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 DESIGNATION ALREADY EXIST 
        if(val=="-1")
        {
           Lbe6.Visible = true;
           Lbe6.Text = ("Designation Already Exists.");
        }
        //IF DESIGNATION DOESN'T EXIST
        else if(val=="1")
        {
          if(DropDownList2.SelectedValue!=null &&  
             DropDownList2.SelectedValue!=null &&
             DropDownList3.SelectedValue!=null )
            { 
               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();
               //  GridView1.DataBind();
            }
           else
          {
             //Show validation message :: Not all the required valued were provided 
          }
        }
        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 = "";
 
}
 
Share this answer
 
Comments
Diya Ayesa 17-Feb-14 6:43am    
i try ur code and it shows me same error in catch
Sergey Alexandrovich Kryukov 17-Feb-14 11:14am    
Do you think unvalidated data from UI can be null?
—SA

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