none
Visual Studio 2017

    Question

  • i have a database table in sql server 2017. when i retrieve a record from sqldatareader it is possible to store that value in a string variable. But i cannot use it outside the dr.Read() function. i want to display resulting the value in the txt3 text box.

    con = new SqlConnection("Data Source=DESKTOP-GSJITB6\\MICROSOFT;Initial Catalog=AuditLog;Integrated Security=True");
                con.Open();
                cmd = new SqlCommand("Select top 1 id,prefix from Customer2 order by id desc", con);
                dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    string a = dr.GetInt32(0).ToString();
                    string b = dr.GetString(1);
                }
                txt3.Text = (a + b);
                dr.Close();
                con.Close();

    Monday, May 20, 2019 5:00 PM

Answers

  • Here is an example writing in a class, call it from a form e.g. var ops = new Class1(); then

    txt3.Text = ops.GetData();

    using System.Data.SqlClient;
    
    namespace YourNamespaceGoesHere
    {
        public class Class1
        {
            public string ConnectionString => 
                "Data Source=DESKTOP-GSJITB6\\MICROSOFT;Initial Catalog=AuditLog;Integrated Security=True"
            public string GetData()
            {
                var results = "";
                var selectQuery = "Select top 1 id,prefix from Customer2 order by id desc";
    
                using (var cn = new SqlConnection {ConnectionString = ConnectionString})
                {
                    using (var cmd = new SqlCommand() {Connection = cn, CommandText = selectQuery})
                    {
                        cn.Open();
    
                        var reader = cmd.ExecuteReader();
                        if (!reader.HasRows) return results;
                        reader.Read();
                        results = $"{reader.GetInt32(0)}{reader.GetString(1)}";
    
                    }
                }
    
                return results;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, May 20, 2019 5:49 PM
    Moderator
  • Hi Thushan,

    Thank you for posting here.

    For your question, you want to display the value in the txt3 textbox outside the dr.Read() function.

    You could try the following code.

                 string a = null;
                 string b = null;
                while (dr.Read())
                {
                    a = dr.GetInt32(0).ToString();
                    b = dr.GetString(1);
                }
                txt3.Text = (a + b);

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 21, 2019 3:09 AM

All replies

  • Here is an example writing in a class, call it from a form e.g. var ops = new Class1(); then

    txt3.Text = ops.GetData();

    using System.Data.SqlClient;
    
    namespace YourNamespaceGoesHere
    {
        public class Class1
        {
            public string ConnectionString => 
                "Data Source=DESKTOP-GSJITB6\\MICROSOFT;Initial Catalog=AuditLog;Integrated Security=True"
            public string GetData()
            {
                var results = "";
                var selectQuery = "Select top 1 id,prefix from Customer2 order by id desc";
    
                using (var cn = new SqlConnection {ConnectionString = ConnectionString})
                {
                    using (var cmd = new SqlCommand() {Connection = cn, CommandText = selectQuery})
                    {
                        cn.Open();
    
                        var reader = cmd.ExecuteReader();
                        if (!reader.HasRows) return results;
                        reader.Read();
                        results = $"{reader.GetInt32(0)}{reader.GetString(1)}";
    
                    }
                }
    
                return results;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, May 20, 2019 5:49 PM
    Moderator
  • Hi Thushan,

    Thank you for posting here.

    For your question, you want to display the value in the txt3 textbox outside the dr.Read() function.

    You could try the following code.

                 string a = null;
                 string b = null;
                while (dr.Read())
                {
                    a = dr.GetInt32(0).ToString();
                    b = dr.GetString(1);
                }
                txt3.Text = (a + b);

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 21, 2019 3:09 AM