Click here to Skip to main content
15,894,539 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to able to have the information of the particular image button clicked? Pin
Richard Deeming22-Mar-21 22:33
mveRichard Deeming22-Mar-21 22:33 
Member 15084749 wrote:
String SQL = "Select * from Book_List where Book_category='" + Label1.Text + "'";
Don't do it like that!

Although this specific case is OK since you're using a constant value, using string concatenation to build a SQL query can and will leave you vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
C#
const string SQL = "Select * from Book_List where Book_category = @BookCategory";
SqlDataAdapter da = new SqlDataAdapter(SQL, con);
da.SelectCommand.Parameters.AddWithValue("@BookCategory", Label1.Text);
Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

Also, don't store the database connection in a class-level field. Instead, create it where necessary, and wrap it in a using block to ensure that it's always disposed of properly.
C#
private static SqlConnection CreateConnection()
{
    return new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
}

...

DataSet ds = new DataSet();
using (SqlConnection con = CreateConnection())
{
    const string SQL = "Select * from Book_List where Book_category = @BookCategory";
    SqlDataAdapter da = new SqlDataAdapter(SQL, con);
    da.SelectCommand.Parameters.AddWithValue("@BookCategory", Label1.Text);
    da.Fill(ds);
}

DataTable dt = ds.Tables[0];
...

And do yourself a favour and give your controls meaningful IDs, rather than accepting the default values suggested by the Visual Studio designer. Sure, you might remember what Label1 refers to now, but when you come back to your code in three weeks, you'll have forgotten.

As to your question, Page_Load is too late to initialize your dynamic controls. Do it in Page_Init instead.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

AnswerRe: How to able to have the information of the particular image button clicked? Pin
DerekT-P23-Mar-21 0:46
professionalDerekT-P23-Mar-21 0:46 
QuestionC# Excel test specific cells in a column Pin
Steve Rudd22-Mar-21 5:59
Steve Rudd22-Mar-21 5:59 
AnswerRe: C# Excel test specific cells in a column Pin
Gerry Schmitz22-Mar-21 7:40
mveGerry Schmitz22-Mar-21 7:40 
GeneralRe: C# Excel test specific cells in a column Pin
Steve Rudd22-Mar-21 9:31
Steve Rudd22-Mar-21 9:31 
GeneralRe: C# Excel test specific cells in a column Pin
Richard MacCutchan22-Mar-21 22:56
mveRichard MacCutchan22-Mar-21 22:56 
GeneralRe: C# Excel test specific cells in a column Pin
Steve Rudd23-Mar-21 0:13
Steve Rudd23-Mar-21 0:13 
GeneralRe: C# Excel test specific cells in a column Pin
Steve Rudd23-Mar-21 1:32
Steve Rudd23-Mar-21 1:32 
GeneralRe: C# Excel test specific cells in a column Pin
Richard MacCutchan23-Mar-21 2:09
mveRichard MacCutchan23-Mar-21 2:09 
QuestionMike`s Gradiator Pin
Alejandro01122-Mar-21 2:22
Alejandro01122-Mar-21 2:22 
AnswerRe: Mike`s Gradiator Pin
Richard Deeming22-Mar-21 2:41
mveRichard Deeming22-Mar-21 2:41 
GeneralRe: Mike`s Gradiator Pin
Alejandro01122-Mar-21 2:48
Alejandro01122-Mar-21 2:48 
AnswerRe: Mike`s Gradiator Pin
Gerry Schmitz22-Mar-21 7:49
mveGerry Schmitz22-Mar-21 7:49 
GeneralRe: Mike`s Gradiator Pin
Alejandro01122-Mar-21 10:16
Alejandro01122-Mar-21 10:16 
GeneralRe: Mike`s Gradiator Pin
Alejandro01123-Mar-21 14:53
Alejandro01123-Mar-21 14:53 
QuestionHow to write to Form1.texBox1 from another class. Pin
Member 1406872419-Mar-21 11:17
Member 1406872419-Mar-21 11:17 
AnswerRe: How to write to Form1.texBox1 from another class. Pin
OriginalGriff19-Mar-21 12:24
mveOriginalGriff19-Mar-21 12:24 
QuestionCombobox Text Pin
Ismael Oliveira 202119-Mar-21 8:01
Ismael Oliveira 202119-Mar-21 8:01 
AnswerRe: Combobox Text Pin
OriginalGriff19-Mar-21 9:44
mveOriginalGriff19-Mar-21 9:44 
GeneralRe: Combobox Text Pin
Ismael Oliveira 202119-Mar-21 10:14
Ismael Oliveira 202119-Mar-21 10:14 
AnswerRe: Combobox Text Pin
BillWoodruff20-Mar-21 6:15
professionalBillWoodruff20-Mar-21 6:15 
QuestionUnhandle System.AccessViolationException from FFMediaToolkit Pin
huycaothu18-Mar-21 23:05
huycaothu18-Mar-21 23:05 
AnswerRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
OriginalGriff18-Mar-21 23:19
mveOriginalGriff18-Mar-21 23:19 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
huycaothu19-Mar-21 2:43
huycaothu19-Mar-21 2:43 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
OriginalGriff19-Mar-21 2:56
mveOriginalGriff19-Mar-21 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.