Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello All,
I am getting this error for the following code to fill the dropdownlist 'ddlstuid' :

Error ocurredSystem.NullReferenceException: Object reference not set to an instance of an object. at IAP.Attendance.Page_Load(Object sender, EventArgs e).

pls tell me how to remove the above error.

code:
C#
SqlConnection myconn;
SqlCommand cmd;
string str = "Data Source=TIMSCDR\\SQLEXPRESS;Initial Catalog=IAP;Integrated Security=True";
SqlDataReader dr;

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        cmd = new SqlCommand("select stuid from attendance", myconn);
        myconn.Open();
        ddlstuid.Items.Clear();
        dr = cmd.ExecuteReader();
        if (dr != null)
        {
            while (dr.Read())
            {
                ddlstuid.Items.Add(dr["stuid"].ToString());
            }
        }
        myconn.Close();
    }
    catch (Exception e1)
    {
        Label45.Text = "Error ocurred" + e1;
    }


Thanks in Advance.
Posted
Updated 5-Jun-11 20:37pm
v2
Comments
cuteband 6-Jun-11 2:21am    
Is this a Web Based?
debo_india 6-Jun-11 2:34am    
yep

public void bind()
{
sqldataadapter da=new sqldataadapter("select * from tname",cn);
datatable dt=new datatable();
da.fill(dt);
ddlstuid.datasource=dt;
ddlstuid.datatextfield="colnale";
ddlstuid.datavaluefield="colname";
ddlstuid.databind();
}

in page load
{
if(!ispostback)
{
bind();
}
}
 
Share this answer
 
Comments
debo_india 6-Jun-11 2:34am    
in the line: ddlstuid.datatextfield="colnale";

what is 'colnale'? did u mean 'colname'?
LakshmiNarayana Nalluri 6-Jun-11 2:46am    
ya
janwel 6-Jun-11 4:46am    
nicely done
You don't appear to assign anything to "myconn". Try adding
C#
cmd = new SqlCommand("select stuid from attendance", myconn);
myconn = new SqlConnection(str); // --- Add this line
myconn.Open();
Don't forget to dispose it in a finally block.
 
Share this answer
 
Comments
debo_india 6-Jun-11 2:28am    
i tried this but its not working still
 
Share this answer
 
v2
Comments
debo_india 6-Jun-11 3:01am    
i tried this but its not working
Check 3 things......
1) you have a fields name stuid in your table or you misspelled it.
2) Add the solution suggested by OriginalGriff

myconn = new SqlConnection(str); // --- Add this line

3) Remove if(dr != null) rather add if(dr.hasRows)

Try out this.. If any problem Comment here..

Hope it helps
 
Share this answer
 
v2
Comments
debo_india 6-Jun-11 2:44am    
i tried out your solution. it is giving an error on this line:
dr = cmd.ExecuteReader();
Ra-one 6-Jun-11 2:52am    
It must not if you created SQLConnection before cmd.ExecuteReader()
C#
SqlConnection myconn;
      SqlCommand cmd;
      string str = "Data Source=TIMSCDR\\SQLEXPRESS;Initial Catalog=IAP;Integrated Security=True";
      SqlDataReader dr;

      protected void Page_Load(object sender, EventArgs e)
      {
         if(!Page.IsPostBack)
         {
          try
          {
              myconn = new SqlConnection(str); // --- Add this line
              cmd = new SqlCommand("select stuid from attendance", myconn);
              myconn.Open();
              ddlstuid.Items.Clear();
              dr = cmd.ExecuteReader();
              if (dr != null)
              {
                  while (dr.Read())
                  {
                      ddlstuid.Items.Add(dr["stuid"].ToString());
                  }
              }
              myconn.Close();
          }
          catch (Exception e1)
          {
              Label45.Text = "Error ocurred" + e1;
          }
         }
 
Share this answer
 
try this:
VB
Dim strSQL As String
Dim connection As SqlConnection = New SqlConnection("Data Source=TIMSCDR\\SQLEXPRESS;Initial Catalog=IAP;Integrated Security=True")
strSQL = "Select [stuid] from [attendance]"
connection.Open()
Dim command As New SqlCommand(strSQL, connection)
dddiscipline.DataSource = command.ExecuteReader
ddlstuid.DataTextField = "stuid"
ddlstuid.DataValueField = "stuid"
ddlstuid.DataBind()
connection.Dispose()
connection.Close()


it works for me
pm me if it doesnt work
 
Share this answer
 
Comments
debo_india 6-Jun-11 3:12am    
its giving error on the line : ddlstuid.DataSource = command.ExecuteReader;
janwel 6-Jun-11 4:14am    
what does it say?
janwel 6-Jun-11 4:16am    
plss put the exact error code
janwel 6-Jun-11 4:19am    
what language are you using?
janwel 6-Jun-11 4:20am    
LakshmiNarayana Nalluri has already have the answer plss use that code if your using C

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