Solved

retrieving data from sql into c#

Posted on 2010-11-23
2
269 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
Comment Utility
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
Comment Utility
thank you,

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

18 Experts available now in Live!

Get 1:1 Help Now