Put the result of the SELECT query into WPF datagrid

The idea was to add window where user can find any student in database. However I've got an error that the column name is wrong. My method' code

string connStr = @"Data Source=.\SQLEXPRESS;
                        AttachDBFileName=C:\Users\Чак\Desktop\ботанизм\ООП\coursework.start\CourseWorkFinal\CourseWorkFinal\University11.mdf;
                        Integrated Security=True";
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        //idstudent={0} AND Name={1} AND Surname={2} AND Middlename={3} AND House={4} AND Street={5} AND Telephone={6}
        string BETA = string.Format("SELECT * FROM Students WHERE Name={0}", "Bob");//textbox1.Text 
        SqlDataAdapter a = new SqlDataAdapter(BETA, conn);
        DataTable x = new DataTable();
        a.Fill(x);//<-there is an exception
        this.dataGrid1.DataContext = x;
        conn.Close();

XAML code

         <DataGrid AutoGenerateColumns="False" Height="227" HorizontalAlignment="Left" Margin="198,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="443" ItemsSource="{Binding}"/> 

I've got such name in database. Why I've got such exception?

Upvotes: 1

Views: 2343

Answers (1)

competent_tech
competent_tech

Reputation: 44921

The problem is this line of code:

string BETA = string.Format("SELECT * FROM Students WHERE Name={0}", "Bob");//textbox1.Text 

This needs to be changed to:

string BETA = string.Format("SELECT * FROM Students WHERE Name='{0}'", "Bob");//textbox1.Text 

Upvotes: 2

Related Questions