Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
C#
public List<Students> function(string a,string b)
{
  using (SqlConnection con = new SqlConnection(conStr))
            {
                
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "select name,class,address from students where name='"+a+"' and class='"+b+"'";
                               
                cmd.Connection = con;
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();

                List<Students> DataObject = new List<Students>();

                while (dr.Read())
                {
                    Students o = new Students();
                  
                    o.Name= dr["Name"].ToString();
                    o.Class= dr["Class"].ToString();
                    o.Address= dr["Address"].ToString();
                    DataObject .Add(o);
                }

                return DataObject;
  }



I need to use this function like,

function(a,b); it should get the specific student.

function(null,null) it should get all students.
Posted
Comments
Sergey Alexandrovich Kryukov 28-Dec-15 2:55am    
You did not explain what to do if only a == null, or only b == null.
What's the problem?
—SA
BillWoodruff 28-Dec-15 7:37am    
Sergey and Kornfeld give you the information/advice you really need here, but do keep in mind that you can define methods with the same name, but different parameter "signatures" to logically separate in your code different distinct cases. This often makes code more maintainable in the future, clearer, self-documenting.

As I see, your problem is how to built an SQL statement that has complex WHERE clause...
You SQL - at the end - should look somethng like this:
SQL
SELECT NAME, CLASS, ADDRESS FROM STUDENTS WHERE (@A IS NULL OR NAME = @A) AND (@B IS NULL OR CLASS = @B)

To execute such query you need to learn how to use parameterized queries in C# (and never use string concatenation to create that!!!)
SqlCommand.Parameters Property (System.Data.SqlClient)[^]
 
Share this answer
 
Comments
BillWoodruff 28-Dec-15 4:39am    
+5
Kornfeld Eliyahu Peter 28-Dec-15 4:46am    
Thank you...
Your approach is wrong from the very beginning. The query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327.

Are you getting the idea? The string taken from a control can be anything, including… a fragment of SQL code.

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.

As to your original question, I have no idea what could possibly cause any confusion. Please see my comment to the question: you did not explain what to do if student or class is null, and the other argument is not null. Besides, any of those parameters can be empty string or anything not related to any class.

All you need is to formulate the rule precisely and make different queries for different cases.

—SA
 
Share this answer
 
Comments
BillWoodruff 28-Dec-15 4:10am    
+5 unwarranted negative down-vote countered
Kornfeld Eliyahu Peter 28-Dec-15 4:28am    
I wonder about that 1 vote...
+5...
C#
public List<students> function(string a,string b)
{
	List<students> DataObject = new List<students>();
	using (SqlConnection con = new SqlConnection(conStr))
	{
		SqlCommand cmd = new SqlCommand();
		if(string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b))
			cmd.CommandText = "select name,class,address from students";
		else
			cmd.CommandText = "select name,class,address from students where name='"+a+"' and class='"+b+"'";
                               
		cmd.Connection = con;
		con.Open();
		SqlDataReader dr = cmd.ExecuteReader();
 
		while (dr.Read())
		{
			Students o = new Students();
			
			o.Name= dr["Name"].ToString();
			o.Class= dr["Class"].ToString();
			o.Address= dr["Address"].ToString();
			DataObject.Add(o);
		}
	}
	return DataObject;
}</students></students></students>


But really you should use parameters instead of
name='"+a+"' and class='"+b+"'
 
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