Currently I am working on a project regarding C# and SQL and I have a problem regarding the SELECT function and I cannot find any solutions on-line.
The scenario is regard searching query from C# through SQL server and display the results in a Data Grid View at C#.
I'm using Visual Studio 2008 and SQL Server Studio 2008.
Before starting the actual project I just did a quick Windows form from Visual studio and just did a datagridview, 2 text boxes and a Search Button.
*And the problems started ! *
At SQL Server I have a a database with a table DVD and I want to search, from this Windows form with the DVD ID and Name.
I Started with the DVD ID and implemented this code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace Project1
{
public partial class Form1 : Form
{
SqlConnection c = new SqlConnection(@"Data Source=GILBERTB-PC\SQLEXPRESS;Initial Catalog=DVDandGameBooking;Integrated Security=True");
public Form1()
{
InitializeComponent();
}
private void btnView_Click(object sender, EventArgs e)
{
DataTable t = new DataTable();
string sqlString = "SELECT * From DVD where Id ='" + txtID.Text+ "'";
SqlDataAdapter dt = new SqlDataAdapter(sqlString, c);
dt.Fill(t);
dtgv1.DataSource = t;
}
}
}
and it worked :)
Then I Changed the code to `string sqlString = "SELECT * From DVD where Name ='" + txtName.Text+ "'";
so that I can search with Name of the DVD but when I started the program and searched with the Name it just showed a blank database :
Is there a reason ?
-----** Also is there any way that I can change the code so that I can either search with the ID or with the Name ?**
Thanks for your help and time