Solved

retrieving data from sql into c#

Posted on 2010-11-23
2
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 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