Solved

retrieving data from sql into c#

Posted on 2010-11-23
2
270 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:dekempeneer
2 Comments
 
LVL 5

Accepted Solution

by:
Tompa99 earned 500 total points
ID: 34195905
Hi,

results.Add(reader.GetString(0));
This is only reading out the string from first column.
if you would change that to (1) you could get the city I think.

Could do something like this
string address = reader["Address"].ToString();
string city = reader["City"].ToString();
string zip =  reader["Zip"].ToString();

since your data is in 3 columns you need to specify what you want to read out

or like this for each of the columns.
reader[0].tostring();
reader[1].tostring();
reader[2].tostring();

Regards Tompa
0
 

Author Closing Comment

by:dekempeneer
ID: 34196091
thank you,

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

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now