Link to home
Start Free TrialLog in
Avatar of dekempeneer
dekempeneerFlag for Belgium

asked on

retrieving data from sql into c#

I want to retrieve data from sql and put into a word field.
Thing is it all works partially.
It returns in the textbox the street, but not the zip or city as mentionned in the sql query, allthough if I run this query in sql it works fine.
Can anyone help ?

void GetAddresFromExternal_DoWork(object sender, DoWorkEventArgs e)
        {
            string LanguageMode = (string)e.Argument;
            string connectionString = "Data Source=xxxxxx\\xxxxxx;Initial Catalog=Contacts;user id=xxxxxxx;password=xxxxxx; Asynchronous Processing=true";
            string ToExternal = Convert.ToString(lstToExternal.Items[0]);
            string query = "SELECT dbo.ADDRESS_SQL.Address, dbo.ADDRESS_SQL.City, dbo.ADDRESS_SQL.Zip FROM dbo.ADDRESS_SQL INNER JOIN dbo.NAME ON dbo.ADDRESS_SQL.Name_Rec = dbo.NAME.Name_Rec WHERE (dbo.NAME.Last_Name + ' ' + dbo.NAME.First_Name = '" + ToExternal + "')";
            e.Result = FetchDataForExternal(connectionString, query);
        }
        void GetAddresFromExternal_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            FillTextBox(Globals.ThisDocument.txtTo, e.Result as StringCollection);
        }

        private void FillTextBox(Microsoft.Office.Tools.Word.PlainTextContentControl txtTo , StringCollection items)
        {
            foreach (string item in items)
            {
                Globals.ThisDocument.txtTo.Text += "\n" + item;
            }
        }
		private StringCollection FetchDataForExternal(string connectionString, string query)
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;
            StringCollection results = new StringCollection();
            try
            {

                connection = new SqlConnection(connectionString);
                command = new SqlCommand(query, connection);
                command.CommandTimeout = 0;
                connection.Open();
                reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    try
                    {
                        results.Add(reader.GetString(0));
                
                    }
                    catch (Exception ex) { }
                
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                command = null;
                if (reader != null) reader.Close();
                if ((connection != null) && (connection.State != ConnectionState.Closed))
                    connection.Close();
                reader = null;
                connection = null;
            }
            return results;
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Tompa99
Tompa99
Flag of Sweden 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 dekempeneer

ASKER

thank you,

solved it by doing :
results.Add(reader.GetString(0));
                        results.Add(reader.GetString(1));
                        results.Add(reader.GetString(2));