Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
I am new to programming,

I am getting the Error message on the command Text:

C#
cmd.CommandText = "update StudentResult set '" + TxtStudentname.Text + "','" + TxtRollno.Text + "','" + TxtC.Text + "','" + TxtCplusplus.Text + "','" + TxtCsharp.Text + "','" + TxtAspdotnet.Text + "','" + Txtmarksobtained.Text + "','" + Txttotalmarks.Text + "','" + TxtPercentage.Text + "' where Rollno='" + TxtRollno.Text + "'";

Complete Error message is:

Object reference not set to an instance of an object.


This is with 2010 Express edition of ASP.NET using C#.

The SQL connection string is:

C#
Data Source=RMM-PC-343\SQLEXPRESS1;Initial Catalog=Student_Result;Integrated Security=True;


What do I need to correct this?

Thanks,
Zaid Qureshi
Posted
Updated 28-Sep-12 20:57pm
v2
Comments
Karthik Harve 29-Sep-12 2:56am    
This code is not sufficient to findout the issue. tell us where eaxtly you are creating the objects for cmd, and other controls.
Abhijit Parab 29-Sep-12 2:57am    
provide complete code. Beginning from sqlconnection object creation.
zaid Qureshi 29-Sep-12 3:06am    
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.Data.SqlClient;





//public class fields
//{
// public string _StudentName { get; set; }
// public int _RollNo { get; set; }
// public int _C { get; set; }
// public int _CPlusplus { get; set; }
// public int _Csharp { get; set; }
// public int _ASP { get; set; }
// int _TotalMarks { get; set; }
// double _Percentage { get; set; }
// float num3 { get; set; }
// float num8 { get; set; }

//}

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


protected void Page_Load(object sender, EventArgs e)
{

SqlConnection con = new SqlConnection(@"Data Source=RMM-PC-343\SQLEXPRESS1;Initial Catalog=Student_Result;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select * from StudentResult", con);

cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

if (!IsPostBack)
{
BindStudentResult();
}
}

private void BindStudentResult()
{

SqlConnection con = new SqlConnection(@"Data Source=RMM-PC-343\SQLEXPRESS1;Initial Catalog=Student_Result;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from StudentResult", con);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();


}






//private void bindgrid()
//{
// SqlConnection con = new SqlConnection(@"Data Source=RMM-PC-343\SQLEXPRESS1;Initial Catalog=Student_Result;Integrated Security=True");


// ds.Clear();
// con.ConnectionString//("data source=.\SQLEXPRESS;Integrated Security=SSPI");
// cmd.Connection=con;
// con.Open();

// string insert="insert into StudentResult(Studentname,Rollno,Cmarks,C++marks,Asp.netmarks,Marksobtained,totalmarks,Percentage)values("'@Studentname'",@Rollno,@Cmarks,@C++marks,@Asp.netmarks,@marksobtained,@totalmarks,@Percentage)";
// cmd=new SqlCommand(insert,con);
// cmd.ExecuteNonQuery();
// con.Close();

//}
DataTable dt;



public void BtnSubmit_Click(object sender, EventArgs e)


{

SqlConnection con = new SqlConnection(@"Data Source=RMM-PC-343\SQLEXPRESS1;Initial Catalog=Student_Result;Integrated Security=True");

con.Open();
calculatetotalmarks();
calculatepercentage();
string insrt = "insert into StudentResult values('" + TxtStudentname.Text + "','" + TxtRollno.Text + "','" + TxtC.Text + "','" + TxtCplusplus.Text + "','" + TxtCsharp.Text + "','" + TxtAspdotnet.Text + "','" + Txtmarksobtained.Text + "','" + Txttotalmarks.Text + "','" + TxtPercentage.Text + "')";
SqlCommand cmd = new SqlCommand(insrt,con);
cmd.CommandType = CommandType.Text;
// //SqlDataAdapter da = new SqlDataAdapter();
// DataSet ds = new DataSet();
//// da.Fill(ds);
// GridView1.DataSource = dt;
// GridView1.DataBind();

//SqlDataAdapter da = new SqlDataAdapter();
// DataSet ds = new DataSet();
// //da.Fill(ds);
//GridView1.DataSource = dt;
//GridView1.DataBind();
cmd.ExecuteNonQuery();
con.Close();

//DataSet ds= new DataSet();
//SqlDataAdapter da= new SqlDataAdapter ();
// da.Fill(ds);
//con.Close();


//con.open();
//cmd.parameters.AddWithValue("StudentName",TxtStudentname.Text);
//cmd.parameters.AddWithValue("RollNo",TxtRollno.Text);
//cmd.parameters.AddWithValue("Cmarks

It the line came direct from your code, an is the line causing the error, then one of your text boxes has not been initialized, and contains null (the error message is pretty clear about this). Which one? We can't tell - you need to either debug, or use a log to find out.

More importantly, you should not do that at all!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. For more information, google "Bobby Tables", and do not assume what you find is just a joke!
 
Share this answer
 
cmd is probably null check you have the following line before using cmd:
C#
cmd = new SqlCommand(...); // with appropriate parameters
 
Share this answer
 
Comments
zaid Qureshi 29-Sep-12 3:17am    
@mehdi gholam: I had tried that also but still the same Error what i am getting..Please help me...
Mehdi Gholam 29-Sep-12 3:38am    
Run your program in a debugger and see where you are getting a null value.
zaid Qureshi 29-Sep-12 3:46am    
i am getting null values in all the fields.
Mehdi Gholam 29-Sep-12 3:54am    
TxtStudentname is null ??
zaid Qureshi 29-Sep-12 3:57am    
yes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900