Click here to Skip to main content
16,018,534 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 = con.CreateCommand(); 
da.SelectCommand.CommandText = "select sum(مدين) AS مدين,sum (دائن) AS دائن  from  اذن_قيد  where اسم_البيان='" + comboBox1.SelectedIndex + "'"; 
da.Fill(ds, "اذن_قيد"); 
dataGridView1.DataSource = ds; 
dataGridView1.DataMember = "اذن_قيد"; 


When i use this code, it return null data But When i use this command in sql server "Return Data"
Posted
Comments
Sergey Alexandrovich Kryukov 11-Mar-12 15:59pm    
How can I be sure the data set meta-data content matches expected data set you get from the command?
Your information is not enough.
--SA

Please see my comment to the question.
To make sure the data set obtained through the query in not empty, create a data reader and check it. Data reader is agnostic to the database schema, it can show you the complete result of the query.

For example:
C#
string queryText = //...

SqlCommand command = new SqlCommand(queryText);
SqlDataReader reader = command.ExecuteReader();
boo empty = !reader.HasRows;
//...
reader.Close();


The data reader is very convenient when you only start to work with some database and may want to investigate what's in it and validate the structure of data sets you are going to create. Please see:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx[^].

There is another problem, a bigger one.

You are composing your query as a string from the UI. Not only this is bad architecture, this is very bad and absolutely not acceptable from the security stand point. You system can be easily intruded using SQP Injection. Please see:
http://en.wikipedia.org/wiki/SQL_injection[^].

In the article referenced above, read about importance of parametrized statements, see also:
http://en.wikipedia.org/wiki/Prepared_statement[^].

Other parameters of the query (those written in Arabo-Persian script, if I'm not mistaken), are now hard-coded, but sooner or later you will have to parametrize them, and that will bring you to the same problem.

To do it in a safe, consistent and maintainable way, you should use parametrized commands, nothing else. Please see:
http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx[^].

—SA
 
Share this answer
 
Comments
Abhinav S 12-Mar-12 4:50am    
Detailed answer. My 5.
Sergey Alexandrovich Kryukov 12-Mar-12 4:55am    
Thank you, Abhinav.
--SA
Darsh_Basha 16-Mar-12 9:40am    
Thanks
Sergey Alexandrovich Kryukov 16-Mar-12 12:07pm    
If you agree that it makes sense, please accept the answer formally (green button) -- thanks.
--SA
Look at that comboBox1.SelectedIndex: that is a number, not a string. I guess you mixed it up with comboBox1.Text; "ism" means name of ..., doesn't it, and not number of ....
Edit: characterset incompatibilty, the copied arab word was changed into nonsense characters.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 12-Mar-12 4:58am    
I did not notice it, because the whole thing is so wrong. Good catch, my 5.

But then this is not a real code OP tested and get exception, because it would not compile.

@Member 7976020:
This is very bad to show wrong code, not that you used during run-time. How could anyone help you with the fix, if you mess it up?
--SA

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