Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Dear All,
I am new to programming,

I am getting the Error message on the command Text:
 
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:
 
Data Source=RMM-PC-343\SQLEXPRESS1;Initial Catalog=Student_Result;Integrated Security=True;
 
What do I need to correct this?

Thanks,
Zaid Qureshi
Posted 28-Sep-12 20:52pm
Edited 28-Sep-12 20:57pm
v2
Comments
Karthik Harve at 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 at 29-Sep-12 2:57am
   
provide complete code. Beginning from sqlconnection object creation.
zaid Qureshi at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

cmd is probably null check you have the following line before using cmd:
cmd = new SqlCommand(...); // with appropriate parameters
  Permalink  
Comments
zaid Qureshi at 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 at 29-Sep-12 3:38am
   
Run your program in a debugger and see where you are getting a null value.
zaid Qureshi at 29-Sep-12 3:46am
   
i am getting null values in all the fields.
Mehdi Gholam at 29-Sep-12 3:54am
   
TxtStudentname is null ??
zaid Qureshi at 29-Sep-12 3:57am
   
yes
Mehdi Gholam at 29-Sep-12 4:03am
   
Make sure you are creating/initializing them then.

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

  Print Answers RSS
0 OriginalGriff 324
1 Sergey Alexandrovich Kryukov 182
2 Maciej Los 159
3 Hard_Rockz 143
4 Richard MacCutchan 125
0 OriginalGriff 5,374
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,270


Advertise | Privacy | Mobile
Web01 | 2.8.140718.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid