Mohamed Elhabbab Mohamed Elhabbab - 3 months ago 5
ASP.NET (C#) Question

Error While Calling Code From other class in Asp.net

I'm developing an Asp.net website and i decided to do it by using oop concepts.

so my first thoughts is to encapsulate my code (the whole behind code )in external class then calling it in my Aspx page to be executed .

i made a new class then i derived it from the main class to be able to access main class controls like buttons,labels etc...

when i call the method which include the code it gives me an error


"Object reference not set to an instance of an object."


so as i think this means that my second class (derived one)
can't access to my main class objects/controls and run code depending on it's value.

the method should executes some code depending on DropDownList but it gives this error.

i really appreciate your help !

1-The behind code for main class which in (aspx page) method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


namespace power_CMS.Authentication
{

public partial class Permissions : System.Web.UI.Page
{


//connection string
//db name = cms
SqlConnection conn = new SqlConnection("data source=.\\sqlexpress ; initial catalog=cms ; integrated security = true");

protected void Page_Load(object sender, EventArgs e)
{

}



protected void btn_grant_Click(object sender, EventArgs e)
{ //grant permission to Members
Cms c = new Cms();
c.User_Role_Insert();

}


2-A part of designer code for my aspx page

protected global::System.Web.UI.WebControls.DropDownList drp_user;

/// <summary>
/// UserDataSource control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.


3-Here's the code for my derived class which i encapsulated my code in it:

namespace power_CMS
{

public class Cms :power_CMS.Authentication.Permissions
{

//sql connection
public static SqlConnection conn = new SqlConnection("data source=.\\sqlexpress ; initial catalog=*** ; integrated security = true");


public void User_Role_Insert()
{




if (drp_user.SelectedIndex == 0 && drp_permission.SelectedIndex == 0 || drp_user.SelectedIndex == 0 || drp_permission.SelectedIndex == 0)
{
//make sure that user will make a valid choise with both dropdownlists
//Invalid Choise Code

lbl_confirm.Text = "Please Make A Valid Choise !";
}

else
{
// Valid Choise Code

try
{
conn.Open();

// string str = "insert into aspnet_UsersInRoles(UserId,RoleId) values('"+c.drp_user.SelectedValue+"','"+drp_permission.SelectedValue+"')";

SqlCommand cmd = new SqlCommand("aspnet_UsersInRoles_Insert", conn);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("UserId", SqlDbType.UniqueIdentifier).Value = new Guid(drp_user.SelectedValue);
cmd.Parameters.Add("RoleId", SqlDbType.UniqueIdentifier).Value = new Guid(drp_permission.SelectedValue);

cmd.ExecuteNonQuery();
conn.Close();

lbl_confirm.Text = "Permission Granted ...!";


drp_user.SelectedIndex = 0;
drp_permission.SelectedIndex = 0;

}
catch (Exception)
{

lbl_confirm.Text = "Sorry <br> This User Already Have A Permission ! ";
}
}
}
}
}

Answer

Where your drp_user define ? It will be null if you not define this object

if you still want not define drp u must change in if

if (drp_user != null && (drp_user.SelectedIndex == 0 ....