Click here to Skip to main content
15,908,674 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            string str = "select * from (select tblNewAdmission.NAID as student_ID,tblNewAdmission.fname as full_name,tblNewAdmission.mname as Monther_Name,tblNewAdmission.gender as Gender,tblNewAdmission.dob as Date_of_Birth,tblNewAdmission.mobile as Mobile,tblNewAdmission.email as Email_ID,tblNewAdmission.semester,tblNewAdmission.prog as Programing_Language,tblNewAdmission.sname as school_name,tblNewAdmission.duration as Course_duration,tblNewAdmission.addres as Address,tblfee.fee as Fees from tblNewAdmission inner join tblfee on tblNewAdmission.NAID=tblfee.NAID) where NAID = '"+textBox1.Text+"'";
            SqlCommand cmd = new SqlCommand(str, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            //cmd.ExecuteNonQuery();
            con.Close();
        }

above code Iam writing in the button click event for searching students


Iam using the joins to get the result from the 2 tables but when iam searching specfic student from the above script inner join result, iam not geeting
can you please help me with script.

What I have tried:

C#
string str = "select * from (select tblNewAdmission.NAID as student_ID,tblNewAdmission.fname as full_name,tblNewAdmission.mname as Monther_Name,tblNewAdmission.gender as Gender,tblNewAdmission.dob as Date_of_Birth,tblNewAdmission.mobile as Mobile,tblNewAdmission.email as Email_ID,tblNewAdmission.semester,tblNewAdmission.prog as Programing_Language,tblNewAdmission.sname as school_name,tblNewAdmission.duration as Course_duration,tblNewAdmission.addres as Address,tblfee.fee as Fees from tblNewAdmission inner join tblfee on tblNewAdmission.NAID=tblfee.NAID) where NAID = '"+textBox1.Text+"'";
Posted
Updated 8-Mar-24 4:28am
v2
Comments
PIEBALDconsult 7-Mar-24 18:49pm    
Please! don't form an SQL statement that way.
Dave Kreskowiak 7-Mar-24 19:24pm    
What's with the SELECT * FROM (SELECT ...) garbage? It's completely uneccessary.

As Dave points out, you can simplify your query. Also, you do not pass parameters like that.

Try this:
C#
string str = "SELECT tblNewAdmission.NAID AS student_ID, tblNewAdmission.fname AS full_name, tblNewAdmission.mname AS Monther_Name, tblNewAdmission.gender AS Gender, tblNewAdmission.dob AS Date_of_Birth, tblNewAdmission.mobile AS Mobile, tblNewAdmission.email AS Email_ID, tblNewAdmission.semester, tblNewAdmission.prog AS Programing_Language, tblNewAdmission.sname AS school_name, tblNewAdmission.duration AS Course_duration, tblNewAdmission.addres AS Address, tblfee.fee AS Fees FROM tblNewAdmission INNER JOIN tblfee ON tblNewAdmission.NAID = tblfee.NAID WHERE tblNewAdmission.NAID = @NAID";

using (SqlCommand command = new SqlCommand(str, con))
{
    command.Parameters.AddWithValue("@NAID", textBox1.Text);

    // Execute your query and process the result
}
 
Share this answer
 
To add to what PIEBALDconsult and Graeme_Grant have said, there is a reason why you don't form SQL queries that way - it's called a "SQL Injection", and it leaves you wide open to accidental or deliberate attack which can destroy your entire database. Never concatenate strings to build a SQL command - always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Quote:
where NAID

Lets guess that the NAID is ambiguous in the query.

Quote:
where NAID = '"+textBox1.Text+"'";

Another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
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