Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,

I'm very new to .NET (c#) so please help me to solve my issue, I build a simple student project. In this project I created a form to display data from sql database to dgPrintIDCard, I added on more column "cells[0]" for checkbox to dgPrintIDCard then I have a print card button to print the checked row in dgPrintIDCard. It works if I only checked one record but it doesn't work once I have multiple checked. It show multiple crystal report instead of one crystal report show and show all student ID Card regarding to checked rows. Please see my code below.

My purpose is: when I checked only one row, system will show only one card in crystal
When I checked 3 rows, system will show 3 cards in crystal

Your assistance is much appreciated.
Regards,
Cheng

What I have tried:

foreach (DataGridViewRow item in dgPrintIDCard.Rows)
                  {
                      if (item.Cells[0].Value != null)
                      {
                          var stID = item.Cells[1].Value;//Student ID is in cells[1]

                          con = new SqlConnection(cs.DBcon);
                          con.Open();
                          string query = "SELECT * FROM student WHERE s_id = '" + stID + "'"; 
                          cmd = new SqlCommand(query, con);
                          printPreviewGlobalVar.mReport = "prntStudentCard";
                          printPreviewGlobalVar.mSqlstr = query;
                          prntPreview prnt = new prntPreview();
                          prnt.Show(); 

                          this.Close();//close current form
                      }                    
                  }
Posted
Updated 1-Jul-19 2:39am
v2

Take out the Close at the end of the if - if you close the form, it will kill the form, and all it's controls - including the DataGridView you are trying to get information from...
 
Share this answer
 
Comments
chenglll 1-Jul-19 4:28am    
I have tried it but still got same result.
First of all, please, read carefully OriginalGriff's solution.

Second of all, your query is for one student at time:
C#
"SELECT * FROM student WHERE s_id = '" + stID + "'";

BTW: such of query is sql injection[^] vulnerable!
You have to create a query which accepts several id's to be able to display selected cards. See: IN (Transact-SQL) - SQL Server | Microsoft Docs[^]
C#
StringBuilder sb = new StringBuilder();
foreach (DataGridViewRow item in dgPrintIDCard.Rows)
{
    if (item.Cells[0].Value != null)
    {
        var stID = item.Cells[1].Value.ToString();//Student ID is in cells[1]
        sb.Append($"{stID},");
    }                    
}

string query = $"SELECT * FROM student WHERE s_id IN ({sb.ToString().TrimEnd(',')})"; 
con = new SqlConnection(cs.DBcon);
con.Open();
cmd = new SqlCommand(query, con);
printPreviewGlobalVar.mReport = "prntStudentCard";
printPreviewGlobalVar.mSqlstr = query;
prntPreview prnt = new prntPreview();
prnt.Show(); 
this.Close();//close current form


Note:
I'm using StringBuilder class, because string object is immutable! See: Strings - C# Programming Guide | Microsoft Docs[^]

Links:
StringBuilder Class (System.Text) | Microsoft Docs[^]
String.TrimEnd Method (System) | Microsoft Docs[^]
 
Share this answer
 
Comments
chenglll 1-Jul-19 23:52pm    
Thank you very much Maciej Los.

I really appreciated for your support, it's working for me now. :)
Maciej Los 2-Jul-19 0:07am    
You're very welcome.
Put a conter for each row selected, then you will be able to see each card or print whatever you want to print(it works for me at least)
The way you have that it appears to me that only the first row selected will be printed out
 
Share this answer
 
v2

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