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:
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:
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