Solved

retrieving data from sql into c#

Posted on 2010-11-23
2
272 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

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

820 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