Click here to Skip to main content
16,018,417 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
SqlConnection con = new SqlConnection("Data Source=MOSTAFA;Initial Catalog=mohasba;Integrated Security=True");
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand.CommandText ="select sum(مدين),sum (دائن)  from  اذن_قيد  where اسم_البيان='" + comboBox1.SelectedIndex + "'";
da.Fill(ds, "اذن_قيد");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "اذن_قيد";


When I run this code
get this erorr

Object reference not set to an instance of an object.
Posted
Updated 8-Mar-12 8:36am
v2

Since you didn't tell us what LINE OF THE CODE was generating the exception, I can only guess...

ds is null because you passed a negative value in with the query (comboBox1.SelectedItem), resulting in a null dataset.

OR

You failed to pass the con variable to the SqlDataAdapter.

My advise is to put a try/catch block around the whole thing, and run your code under the debugger.
 
Share this answer
 
v3
Comments
Darsh_Basha 8-Mar-12 16:08pm    
The line that generate this exception

da.SelectCommand.CommandText ="select sum(مدين),sum (دائن) from اذن_قيد where اسم_البيان='" + comboBox1.SelectedIndex + "'";
#realJSOP 8-Mar-12 18:05pm    
So which item is null? da or comboBox1?
This is not a valid use of DataSet. You can fill an instance of DataSet only if it is filled with meta-data reflecting the database schema you expect to use through the query. In other words, you should add some data tables, columns, etc.

Formally, you can use the command the way you, do, but in real life… there is no cases where you hard-code the data in your query like you do. And if you want to compose the query through string concatenation, this is also very bad. First, strings are immutable, so multiple use of the concatenation operation "+" is bad in terms of performance, you could use string.Format of System.Text.StringBuilder, but for queries this is not really acceptable.

If you want to compose your query string out of user-input data, it's just too dangerous. Please read about the danger of SQL Injection and importance of parametrized statements:
http://en.wikipedia.org/wiki/SQL_injection[^].

So, you need to use parametrized commands. Please see:
http://msdn.microsoft.com/en-us/library/ms254953.aspx[^].

For introductory article on ADO.NET, I recommend this one:
Using ADO.NET for beginners[^].

—SA
 
Share this answer
 
My Dear friend...

There is very minor mistake you are doing. You must call SQLCommand command class, otherwise it will give you a message "Object refrense set to null".
Now i have put some code for you, you can just change as per my code it will work.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
//Retrive connection string form web.config file.
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["DemoJVM"].ToString());
//Create dataset object.
DataSet ds = new DataSet();
//Creater sqlcommand object.
SqlCommand cmd = new SqlCommand("", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.CommandText = "select * from JVN where JV_No=1";
da.Fill(ds, "non");
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch(Exception ex)
{

Response.Write("Source=" + ex.Source);
}
}
}



I am giving you example of code. you should change as per you requirement.

Thank You.

Patel Azharuddin.
Jr. Software Developer.
azhar_patel_4u@yahoo.co.in
India.
 
Share this answer
 

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