Click here to Skip to main content
15,867,756 members
Home / Discussions / C#
   

C#

 
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
subeditorPete 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 
I'm building a simple windows form app which stores all of my DVD's (pet project, just for learning; I'm sure there are plenty already out there). I'm able to add items into the database (azure SQL Server) perfectly, even leaving some or most textboxes blank (the title column is setup as NOT NULL in my table) but I can't get it to retrieve any data.

Here is my OnClick method:

C#
private void searchButton_Click(object sender, EventArgs e)
{
	string qString = @"SELECT Title, Director, Genre, ReleaseYear, Length, NumberofDisks, Description FROM Base WHERE Title = '" + titleTextbox.Text + "' AND Director = '" + directorTextbox.Text + "' AND Genre = '" + genreCombobox.GetItemText(genreCombobox.SelectedItem) + "' AND ReleaseYear = '" + yearCombobox.GetItemText(yearCombobox.SelectedItem) + "' AND Length = '" + lengthTextbox.Text + "' AND NumberOfDisks = '" + numberOfDisksTextbox.Text + "' AND Description = '" + descriptionTextbox.Text + "';";
	
    string cString = @"Server=MyAzureServer,MyPortNumber;Database=MyDatabase;User ID=me@MyAzureServer;Password=MyPassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;";

	DataTable dTable = new DataTable();

	SqlConnection sConnection = new SqlConnection(cString);

	sConnection.Open();

	using(SqlCommand sCommand = new SqlCommand(qString, sConnection))
        {
        	try
		{
			SqlDataReader sReader = sCommand.ExecuteReader();
			dTable.Load(sReader);
		}
		catch(SqlException sEx)
		{
			MessageBox.Show(sEx.Message);
		}

		resultsDataGridView.DataSource = dTable;
	}

		sConnection.Close();
}


and here is my table:

SQL
CREATE TABLE [dbo].[Base]
(
	[MediaID] [int] IDENTITY(1,1) PRIMARY KEY,
	[Title] [nvarchar](50) NOT NULL,
	[Director] [nvarchar](50) NULL,
	[Genre] [nvarchar](50) NULL,
	[ReleaseYear] [date] NULL,
	[Length] [time] NULL,
	[NumberOfDisks] [int] NULL,
	[Description] [nvarchar](200) NULL
)


I absolutely realize this is open to SQL injection. My plan is to just 'get it working' and then I will go back over it and and change to parameterized queries (I have to read up on how to use them). The program compiles and runs fine. When I click the search button with all or any of the textboxes filled, it returns no results in the datagridview. The white grid shows up but it is empty. I can query the database directly and get the results I'm looking for, but not programatically. Do I need to build the query using logic? Since almost all of my columns accept null (except Title) I'm assuming leaving a field blank wouldn't be a problem, but even if I enter data into all of the available textboxes(exact matches including case just to be certain) I get no results. Am I missing something small and stupid?

Can anyone give me any help or hints? Please no laughing at my code or making fun of me Laugh | :laugh:
Thanks,
John

"Only a fool learns from his own mistakes, but a wise man learns from the mistakes of others." -Unknown

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 
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 
GeneralRe: an unexpected absence of fail ... using anonymous actions in a WinForm EventHandler Pin
Member 1113646119-Jul-15 10:01
Member 1113646119-Jul-15 10:01 
GeneralRe: an unexpected absence of fail ... using anonymous actions in a WinForm EventHandler Pin
BillWoodruff19-Jul-15 11:49
professionalBillWoodruff19-Jul-15 11:49 
AnswerRe: an unexpected absence of fail ... using anonymous actions in a WinForm EventHandler Pin
Richard Deeming20-Jul-15 6:43
mveRichard Deeming20-Jul-15 6:43 
QuestionApplication is Crashing Pin
Jassim Rahma19-Jul-15 1:10
Jassim Rahma19-Jul-15 1:10 
AnswerRe: Application is Crashing Pin
OriginalGriff19-Jul-15 1:36
mveOriginalGriff19-Jul-15 1:36 
AnswerRe: Application is Crashing Pin
Dave Kreskowiak19-Jul-15 3:54
mveDave Kreskowiak19-Jul-15 3:54 
QuestionHow to get google place predictions with latitude, longitude and address based on Textbox entry in c# Pin
sr15918-Jul-15 19:05
sr15918-Jul-15 19:05 
AnswerRe: How to get google place predictions with latitude, longitude and address based on Textbox entry in c# Pin
OriginalGriff18-Jul-15 20:17
mveOriginalGriff18-Jul-15 20:17 
QuestionCar tracker Pin
lolinga218-Jul-15 8:58
lolinga218-Jul-15 8:58 

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.