0

I new to C#, and trying to make a winform app, i am having problem to search a Database depending upon the text entered in a textbox.

I have added the gridview, when adding a new query, what should be the WHERE clause like to be able to use data from Winform controls?

Also do i need to change the fill table call ?

And is there any Good books that may cover a good amount of similar topic,i.e,working with databases in c# winforms?

Thank you all, in advance..

TarunG
  • 602
  • 5
  • 21

2 Answers2

1

You should look into making a stored procedure for your query and then you supply the value from your textbox.Text field into your stored procedure.

This other question might help you with the C# syntax: How to execute a stored procedure within C# program

And assuming you're trying out SQL Server Express, this should help you setup the query as a stored procedure: http://msdn.microsoft.com/en-us/library/ms345415.aspx

Otherwise if you're going for basic SQL in the winForm, you likely want to run the query based on the user pressing a button rather than the "TextChanged" event on the TextBox:

String queryStr = "SELECT * "+
    "FROM my_table "+
    String.Format("WHERE my_value = '{0}'", TextBox1.Text);
Community
  • 1
  • 1
Seph
  • 8,472
  • 10
  • 63
  • 94
  • i am a newbie(wrt to databases) so pls bear with it, can you pls tell me how should i execute this queryStr, and then populate the gridView with the result?? (i am using SQl Server) Also pls suggest some book where i may find how to work with DB in c# – TarunG Jul 02 '11 at 09:24
0

It may be worth looking into linqto sql or linq to entities. Then you could do it somewhat like

  var q = from s in recs.Record
          where s == txtBox.Text 
          select s;

There are plenty of online resources for linq and it sort of creates a data access layer for you.

Daniel Casserly
  • 3,552
  • 2
  • 29
  • 60