Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am Create User define Methode in asp.net in C#
i am geting error:Object reference not set to an instance of an object.
error dialog show here-
String query = "insert into saveCart(sno,BkName,BkLanguage,BkStd,BkQty,BkPrice,TotalPrice) values(" + sno + ",'" + Session["Name"].ToString() + "','" + BkName + "','" + BkLanguage + "','" + BkStd + "','" + BkQty + "','" + BkPrice + "','" + TotalPrice + "')";


My Database Table is
sno    int Checked
Name   nvarchar(10)    Checked
BkName nvarchar(100)   Unchecked
BkLanguage nvarchar(100)   Checked
BkStd  nvarchar(100)   Checked
BkQty  nvarchar(100)   Checked
BkPrice    nvarchar(100)   Checked
TotalPrice nchar(10)   Checked
       Unchecked


What I have tried:

private void saveCart(int sno,  String BkName, String BkLanguage, String BkStd, String BkQty,String BkPrice, String TotalPrice)
      {
          String query = "insert into saveCart(sno,BkName,BkLanguage,BkStd,BkQty,BkPrice,TotalPrice) values(" + sno + ",'" + Session["Name"].ToString() + "','" + BkName + "','" + BkLanguage + "','" + BkStd + "','" + BkQty + "','" + BkPrice + "','" + TotalPrice + "')";
          String mycon = "Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=NoveltySystem;Integrated Security=True;Pooling=False";
          SqlConnection con = new SqlConnection(mycon);
          con.Open();
          SqlCommand cmd = new SqlCommand();
          cmd.CommandText = query;
          cmd.Connection = con;
          cmd.ExecuteNonQuery();
      }
Posted
Updated 1-Feb-19 0:57am
Comments
Richard Deeming 28-Jan-19 12:46pm    
The most likely cause of the exception is that Session["Name"] is null. Debug your code to find out why.
Member 14083059 31-Jan-19 2:55am    
the error shows again an again here-String query = "insert into saveCart(sno,BkName,BkLanguage,BkStd,BkQty,BkPrice,TotalPrice) values(" + sno + ",'" + Session["Name"].ToString() + "','" + BkName + "','" + BkLanguage + "','" + BkStd + "','" + BkQty + "','" + BkPrice + "','" + TotalPrice + "')";

it shows the "Object reference not set to an instance of an objec"

You have too much fields for BkName, remove:
Session["Name"].ToString()
or add the Name field.

Also it is better to use parameterized queries to avoid the risk of SQL injection.
 
Share this answer
 
v2
To fix the SQL Injection vulnerability:
C#
private void saveCart(int sno,  String BkName, String BkLanguage, String BkStd, String BkQty,String BkPrice, String TotalPrice)
{
    // TODO: Load this from your config file:
    const string mycon = "Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=NoveltySystem;Integrated Security=True;Pooling=False";
    
    const string query = "insert into saveCart (sno, BkName, BkLanguage, BkStd, BkQty, BkPrice, TotalPrice) values (@sno, @BkName, @BkLanguage, @BkStd, @BkQty, @BkPrice, @TotalPrice)";
    
    using (SqlConnection con = new SqlConnection(mycon))
    using (SqlCommand cmd = new SqlCommand(query, con))
    {
        cmd.Parameters.AddWithValue("@sno", sno);
        cmd.Parameters.AddWithValue("@BkName", BkName);
        cmd.Parameters.AddWithValue("@BkLanguage", BkLanguage);
        cmd.Parameters.AddWithValue("@BkStd", BkStd);
        cmd.Parameters.AddWithValue("@BkQty", BkQty);
        cmd.Parameters.AddWithValue("@BkPrice", BkPrice);
        cmd.Parameters.AddWithValue("@TotalPrice", TotalPrice);
        
        // TODO: Which column are you inserting Session["Name"] into?
        
        con.Open();
        cmd.ExecuteNonQuery();
    }
}

It then becomes obvious that you're specifying more values in the VALUES clause than columns in the INSERT INTO clause - as already mentioned in Solution #1.

You also need to wrap your connection and command objects in using blocks, so that they are cleaned up properly. With your current code, you will eventually run out of connections, and start getting errors when you try to open a new connection.
using statement - C# Reference | Microsoft Docs[^]

You should also look at loading your connection string from your application's configuration file, rather than hard-coding it throughout your code.
Connection Strings and Configuration Files | Microsoft Docs[^]
 
Share this answer
 
v2
your query is as below:->

 String query = "insert into saveCart(sno,BkName,BkLanguage,BkStd,BkQty,BkPrice,TotalPrice) values(" + sno + ",'" + Session["Name"].ToString() + "','" + BkName + "','" + BkLanguage + "','" + BkStd + "','" + BkQty + "','" + BkPrice + "','" + TotalPrice + "')";

Remove :->  Session["Name"].ToString() 

your query should be like below:->

 String query = "insert into saveCart(sno,BkName,BkLanguage,BkStd,BkQty,BkPrice,TotalPrice) values('" + sno + "','" + BkName + "','" + BkLanguage + "','" + BkStd + "','" + BkQty + "','" + BkPrice + "','" + TotalPrice + "')";


But i think the error is due to Session["Name"] having null value.
It is always better to use Convert.toString(Session["Name"]) instead of Session["Name"].toString().
 
Share this answer
 
Comments
Member 14083059 31-Jan-19 2:54am    
sir can you give some example
Prasad Nikumbh 30-Jan-19 5:33am    
Yes I know abt SQL injection.But if the project is old one.sometimes in old project they might have use this everywhere.so i have provided this solution.
But paramterized you can use.Its better way.you have to change it in eveywhere where u have used sql queries.
And the another thing i have just provided the root cause of the issue.
Member 14083059 31-Jan-19 2:53am    
thank you for giving your sloution. because i used session["Name"] i want store data session wise user login.
Richard Deeming 1-Feb-19 6:45am    
Not fixing a critical security vulnerability because it would be too much work is not a good excuse.

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