Link to home
Start Free TrialLog in
Avatar of wilko100
wilko100

asked on

C# Databinding to SQL

Im new to C# and to databinding so any feedback would be useful, i have a frm with a txt, box a listbox and a btn and a SQL database that has a client table (ID,Name,Address etc), what i want the form to do is whenever the button is clicked it searchs for the Client name in the Client table 'LIKE' any chars entered in the txt box then display the ClientName/Names in the ListBox. I need some help to on how to take whats entered in a txt box, run a query then transfer results to the Listbox? and do i need to specify the connection string everytime i have to reference a db? I said i was new!!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace ClientDetailsApplication1
{
    public partial class frmSearchClient : Form
    {
        public frmSearchClient()
        {
            InitializeComponent();
        }        
        
        //Enter part or all Client Name
        private void txtSearchClient_TextChanged(object sender, EventArgs e)
        {
 
        }
 
        //Open Connection and run Query to find Client thats entered in TxtSearchClient Box
        private void btnFind_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
            conn.ConnectionString = "integrated security=SSPI;data source=InstanceName;" +
            "persist security info=False;initial catalog=<DatabaseName>";
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                cmd.Connection.Open();
                cmd.CommandText = ("SELECT ClientName FROM Client WHERE ClientName LIKE '%'");             
                //ListBox1 to display whatever the above result returns from txtSearchClient      
 
                
 
                SqlDataReader reader = cmd.ExecuteReader();             
 
                cmd.Connection.Close();
            }  
        }
        
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
 
        }
 
        
    }
       
        
    }

Open in new window

Avatar of RiteshShah
RiteshShah
Flag of India image

you don't need to give connecting string everytime, you can create one databinding class or in case of web application, give it in web.config
ASKER CERTIFIED SOLUTION
Avatar of RiteshShah
RiteshShah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sidkud
sidkud

As you are displaying the data, you can use the read only and faster datareader class.
Regarding the connection string, yes you need to specify it every time you open a connection. However, you can put it in the web.config, or in constants or even a custom data access class and then use at multiple places.

private void btnFind_Click(object sender, EventArgs e)
{
	SqlConnection MyConn = new SqlConnection(@"Data Source=home-pc;initial catalog=db_siddharth;Integrated Security=True;");
	SqlCommand MyComm    = new SqlCommand("select * from table1 where name like '" + MyTextBox.Text + "%'", MyConn);
	MyConn.Open();
	SqlDataReader dr = MyComm.ExecuteReader();
	MyListBox.DataSource = dr;
	MyListBox.DataTextField = "Name";
        MyListBox.DataValueField = "Col1";
 
	MyListBox.DataBind();
	MyConn.Close();
}

Open in new window

Avatar of wilko100

ASKER

Already got DB but good example, excellent again!!  Thanks!! works a treat,  I have also created a server connection in VS 2008 that has created me a dataset and appconfig file with server properties which i now refer to everytime i bind to a datasource.
Once again thanks