Click here to Skip to main content
15,909,091 members
Home / Discussions / C#
   

C#

 
GeneralRe: System.DirectoryServices.ActiveDirectory Missing Pin
Toni7821-Jul-15 23:48
Toni7821-Jul-15 23:48 
Questionvisual studio 2015 local database help Pin
jamesmc153520-Jul-15 21:38
jamesmc153520-Jul-15 21:38 
AnswerRe: visual studio 2015 local database help Pin
Eddy Vluggen21-Jul-15 0:26
professionalEddy Vluggen21-Jul-15 0:26 
GeneralRe: visual studio 2015 local database help Pin
jamesmc153521-Jul-15 0:29
jamesmc153521-Jul-15 0:29 
GeneralRe: visual studio 2015 local database help Pin
Eddy Vluggen21-Jul-15 1:15
professionalEddy Vluggen21-Jul-15 1:15 
GeneralRe: visual studio 2015 local database help Pin
jamesmc153521-Jul-15 1:17
jamesmc153521-Jul-15 1:17 
GeneralRe: visual studio 2015 local database help Pin
Eddy Vluggen21-Jul-15 8:10
professionalEddy Vluggen21-Jul-15 8:10 
AnswerRe: visual studio 2015 local database help Pin
Richard MacCutchan21-Jul-15 3:35
mveRichard MacCutchan21-Jul-15 3:35 
QuestionAssigning Tuple.Create to a Func ? ... an interesting code fragment Pin
BillWoodruff20-Jul-15 8:51
professionalBillWoodruff20-Jul-15 8:51 
AnswerRe: Assigning Tuple.Create to a Func ? ... an interesting code fragment Pin
Sascha Lefèvre23-Jul-15 12:57
professionalSascha Lefèvre23-Jul-15 12:57 
GeneralRe: Assigning Tuple.Create to a Func ? ... an interesting code fragment Pin
BillWoodruff23-Jul-15 21:52
professionalBillWoodruff23-Jul-15 21:52 
Questionimplement duplex wcf service in iis Pin
tehran135720-Jul-15 2:54
tehran135720-Jul-15 2:54 
AnswerRe: implement duplex wcf service in iis Pin
Pete O'Hanlon20-Jul-15 3:16
mvePete O'Hanlon20-Jul-15 3:16 
AnswerRe: implement duplex wcf service in iis Pin
Eddy Vluggen20-Jul-15 7:47
professionalEddy Vluggen20-Jul-15 7:47 
QuestionDeploy POS.NET files Pin
Jassim Rahma20-Jul-15 2:42
Jassim Rahma20-Jul-15 2:42 
AnswerRe: Deploy POS.NET files Pin
ZurdoDev20-Jul-15 8:47
professionalZurdoDev20-Jul-15 8:47 
QuestionCreating a SELECT Query Based on Textbox Data Pin
John L. DeVito19-Jul-15 4:59
professionalJohn L. DeVito19-Jul-15 4:59 
GeneralRe: Creating a SELECT Query Based on Textbox Data Pin
PIEBALDconsult19-Jul-15 5:06
mvePIEBALDconsult19-Jul-15 5:06 
AnswerRe: Creating a SELECT Query Based on Textbox Data Pin
Wendelius19-Jul-15 5:25
mentorWendelius19-Jul-15 5:25 
AnswerRe: Creating a SELECT Query Based on Textbox Data Pin
Dave Kreskowiak19-Jul-15 6:29
mveDave Kreskowiak19-Jul-15 6:29 
AnswerRe: Creating a SELECT Query Based on Textbox Data Pin
F-ES Sitecore19-Jul-15 22:29
professionalF-ES Sitecore19-Jul-15 22:29 
AnswerRe: Creating a SELECT Query Based on Textbox Data Pin
Richard Deeming20-Jul-15 6:58
mveRichard Deeming20-Jul-15 6:58 
John L. DeVito wrote:
My plan is to just 'get it working' and then I will go back over it and and change to parameterized queries

That's a very bad plan. There's a good chance that you'll miss something, or forget to do it, or run out of time, or lose interest and move on to the next project... Smile | :)

Plus, as others have pointed out, using string concatenation to build your query will introduce new problems that you'll have to fix, which wouldn't be the case if you used properly parameterized queries.

Parameterized queries aren't particularly hard, particularly as you're using ADO.NET and SQL Server:
C#
private void searchButton_Click(object sender, EventArgs e)
{
    // TODO: This should probably be in the configuration file:
    const string ConnectionString = @"Server=MyAzureServer,MyPortNumber;Database=MyDatabase;User ID=me@MyAzureServer;Password=MyPassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;";
    
    const string Query = @"SELECT 
    Title, 
    Director, 
    Genre, 
    ReleaseYear, 
    Length, 
    NumberofDisks, 
    Description 
FROM 
    Base 
WHERE 
    (NullIf(@Title, '') Is Null Or Title = @Title) 
AND 
    (NullIf(@Director, '') Is Null Or Director = @Director) 
AND 
    (NullIf(@Genre, '') Is Null Or Genre = @Genre) 
AND 
    (NullIf(@ReleaseYear, '') Is Null Or ReleaseYear = @ReleaseYear) 
AND 
    (NullIf(@Length, '') Is Null Or Length = @Length) 
AND 
    (NullIf(@NumberOfDisks, '') Is Null Or NumberOfDisks = @NumberOfDisks) 
AND
    (NullIf(@Description, '') Is Null Or Description = @Description)
;";

    DataTable dTable = new DataTable();

    using (SqlConnection connection = new SqlConnection(ConnectionString))
    using (SqlCommand command = new SqlCommand(Query, connection))
    {
        command.Parameters.AddWithValue("@Title", titleTextbox.Text);
        command.Parameters.AddWithValue("@Director", directorTextbox.Text);
        command.Parameters.AddWithValue("@Genre", genreCombobox.GetItemText(genreCombobox.SelectedItem));
        command.Parameters.AddWithValue("@ReleaseYear", yearCombobox.GetItemText(yearCombobox.SelectedItem));
        command.Parameters.AddWithValue("@Length", lengthTextbox.Text);
        command.Parameters.AddWithValue("@NumberOfDisks", numberOfDisksTextbox.Text);
        command.Parameters.AddWithValue("@Description", descriptionTextbox.Text);

        connection.Open();
        using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
        {
            dTable.Load(reader);
        }
    }

    resultsDataGridView.DataSource = dTable;
}




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


Questionan unexpected absence of fail ... using anonymous actions in a WinForm EventHandler Pin
BillWoodruff19-Jul-15 4:05
professionalBillWoodruff19-Jul-15 4:05 
AnswerRe: an unexpected absence of fail ... using anonymous actions in a WinForm EventHandler Pin
Alan N19-Jul-15 9:44
Alan N19-Jul-15 9:44 
GeneralRe: an unexpected absence of fail ... using anonymous actions in a WinForm EventHandler Pin
BillWoodruff19-Jul-15 11:46
professionalBillWoodruff19-Jul-15 11:46 

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.