Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

retrieving data from sql into c#

Posted on 2010-11-23
2
Medium Priority
?
278 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Progress

715 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