Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,I'm getting error like
error:System.NullReferenceException: Object reference not set to an instance of an object. at student._Default.clicked(Object sender, EventArgs e)
don't know why?is there anything wrong in my code?
please help...

thnks...



C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace student
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void clicked(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["stdConnectionStrings"].ConnectionString);
                con.Open();
                string user = "select count(*)from the stdtable where std_name='" + TextBox1.Text + "'";
                SqlCommand com = new SqlCommand(user, con);
                int a = Convert.ToInt32(com.ExecuteScalar());
                if (a == 1)
                {
                    Response.Write("user already exist");
                }
                string insertQuery = "insert into stdtable (std_name,roll,std_address) values (@std_name,@roll,@std_address)";
                com.Parameters.AddWithValue("@std_name", TextBox1.Text);
                com.Parameters.AddWithValue("roll", TextBox2.Text);
                com.Parameters.AddWithValue("@std_address", TextBox3.Text);
                com.ExecuteNonQuery();
                Response.Redirect("stdadmin.aspx");
                Response.Write("registration succescesfull...");
                con.Close();
            }
            catch (Exception ex)
            {
                Response.Write("error: " + ex.ToString());
            }
        }
    }
}
Posted
Updated 21-May-14 18:28pm
v2
Comments
Prasad Avunoori 22-May-14 0:27am    
Make sure TextBox1.Text, TextBox2.Text, TextBox3.Text are not empty/null.
dan!sh 22-May-14 0:29am    
Which line? Just debug your code.

ConfigurationManager.ConnectionStrings["stdConnectionStrings"].ConnectionString
Check if this contains a value.

If you debug your code you will be able to get to the exact line throwing the error.
 
Share this answer
 
Comments
Debanjan Mondal 22-May-14 2:06am    
I debugged, but don't know what's happening ...I changed my code little bit ...
Debanjan Mondal 22-May-14 2:13am    
namespace student
{
public partial class _Default :
System.Web.UI.Page
{
protected void Page_Load
( object sender, EventArgs e)
{
}
protected void clicked
( object sender, EventArgs e)
{
try
{
SqlConnection con =
new SqlConnection
(ConfigurationManager.ConnectionStrings
[ "stdConnectionStrings" ].ConnectionString);
con.Open();
string insertQuery =
"insert into stdtable
(std_name,roll,std_address) values
(@std_name,@roll,@std_address)" ;
com.Parameters.AddWithValue
( "@std_name" , TextBox1.Text);
com.Parameters.AddWithValue( "roll" ,
TextBox2.Text);
com.Parameters.AddWithValue
( "@std_address" , TextBox3.Text);
com.ExecuteNonQuery
();
Response.Redirect
( "stdadmin.aspx" );
Response.Write
( "registration succescesfull..." );
con.Close();
}
catch (Exception ex)
{
Response.Write
( "error: " + ex.ToString());
}
}
}
}
Check if your connection string name is correct in your config file. If you have your connection string name incorrect, you will get this error.
 
Share this answer
 
Comments
Debanjan Mondal 22-May-14 2:45am    
Thanks ..found it ...it's happening for a single 's'.
it was my mistake ...
what does it mean?

SqlConnection con =
new SqlConnection
(ConfigurationManager.ConnectionStrings
[ "stdConnectionStrings" ].ConnectionString);
con.Open();
 
Share this answer
 
Comments
kumar9avinash 22-May-14 2:21am    
its connection string for your database, [ "stdConnectionStrings" ] this is the name of the coonection string which you have in web config file
Debanjan Mondal 22-May-14 2:45am    
Thnks a looot..it's working finally
Debanjan Mondal 22-May-14 2:46am    
Still I want to know something ...
What is postback?

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