none
Data from Database to textbox RRS feed

  • Question

  • Hi,

     

    I really would appreciate some help with c# and databases, i have tried all day to get data from my sdf database to a textbox in a form, I have included the code i used below, could anybody point me in the correct direction. Thanks.

     

    Code Snippet

    SqlCeEngine engine = new SqlCeEngine(Settings.Default.CompanyInformationConnectionString);

    SqlCeConnection connection = new SqlCeConnection(engine.LocalConnectionString);

    connection.Open();

    SqlCeCommand command = connection.CreateCommand();

    command.CommandText = "SELECT [CompanyName], [Address Line 1], [Address Line 2], [Address Line 3], [Tow" +

    "nCity], [County], [Postcode], [Phone Number], [Fax Number], [Website]" +

    ", [Email Address], [VAT Number] FROM [CompanyInfo] WHERE [CompanyName]='companyname'";

    this.CompanyNameData = "SELECT [CompanyName] FROM [CompanyInfo] WHERE [CompanyName] = 'companyname'";

    CompanyName.Text = CompanyNameData.ToString();

     

     

    Wednesday, April 9, 2008 3:49 PM

Answers

  • Try this:

     

    Code Snippet

    try {

    SqlCeEngine engine = new SqlCeEngine(Settings.Default.CompanyInformationConnectionString);

    SqlCeConnection connection = new SqlCeConnection(engine.LocalConnectionString);

    connection.Open();

    SqlCeCommand command = connection.CreateCommand();

    command.CommandText = "SELECT [CompanyName], [Address Line 1], [Address Line 2], [Address Line 3], [Tow" +

    "nCity], [County], [Postcode], [Phone Number], [Fax Number], [Website]" +

    ", [Email Address], [VAT Number] FROM [CompanyInfo] WHERE [CompanyName]='companyname'";

     

    SqlCeDataReader reader = command.ExecuteReader();

    if (reader.Read())

    {

    CompanyName.Text = reader["Company"].ToString();

    }

    finally

    { // be sure to dispose of your objects.

    if (connection != null)

    connection.Dispose();

    if (command != null)

    command.Dispose();

    if (reader != null)

    reader.Dispose();

    }

     

     

     

     

    Wednesday, April 9, 2008 4:17 PM

All replies

  • command.executeReader will give you a reader by which you can iterate through the results and update a textbox if you want to.

     

    Wednesday, April 9, 2008 3:59 PM
  • Try this:

     

    Code Snippet

    try {

    SqlCeEngine engine = new SqlCeEngine(Settings.Default.CompanyInformationConnectionString);

    SqlCeConnection connection = new SqlCeConnection(engine.LocalConnectionString);

    connection.Open();

    SqlCeCommand command = connection.CreateCommand();

    command.CommandText = "SELECT [CompanyName], [Address Line 1], [Address Line 2], [Address Line 3], [Tow" +

    "nCity], [County], [Postcode], [Phone Number], [Fax Number], [Website]" +

    ", [Email Address], [VAT Number] FROM [CompanyInfo] WHERE [CompanyName]='companyname'";

     

    SqlCeDataReader reader = command.ExecuteReader();

    if (reader.Read())

    {

    CompanyName.Text = reader["Company"].ToString();

    }

    finally

    { // be sure to dispose of your objects.

    if (connection != null)

    connection.Dispose();

    if (command != null)

    command.Dispose();

    if (reader != null)

    reader.Dispose();

    }

     

     

     

     

    Wednesday, April 9, 2008 4:17 PM
  •  

    Thanks David,

     

    It worked a treat and you have saved my day! I am now able to get on and do some nice coding - seeing as i now know what i'm doing.

     

    Matt.

    Wednesday, April 9, 2008 4:43 PM