none
how to retrieve data from sql local database(.mdf file) into a textbox RRS feed

  • Question

  • please i am creating an application, and will like to retrieve the value of a certain cell in my database into my textbox on my main form.

    the database is a local sql database (.mdf file) with a table call TOTAL MEMBERSHIP and five columns

    connectionstring for the database:Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="D:\LIBRARY\ACADEMIC & EDUCATION\PROJECTS\I.T\VISUAL BASIC\A.C.C.M.S\LOCAL DATABASE FOR ACCMS.mdf";Integrated Security=True

    1. ID

    2. TOTAL MEN

    3. TOTAL WOMEN

    4. TOTAL CHILDREN

    5. TOTAL CONGREGATION

    it is just one row table, i just want to value in the total congregation to be retrieve into my text when the main page loads.

    the name of the text box is TEXTBOX1

    Sunday, February 11, 2018 9:25 PM

All replies

  • Hi NANA POKU BREMPONG,

    Thanks for posting here.

    This forum is about the Visual Studio IDE, as your issue is more related to the ADO.NET DataSet, we help you move it to correct forum for better support.

    If you have some VS IDE questions, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.


    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.

    Monday, February 12, 2018 6:46 AM
  • Hi NANA POKU BREMPONG,

    We could achieve it via ado.net, here is a winform sample for your reference.

    using System;
    using System.Data.SqlClient;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }
    
            private void Form3_Load(object sender, EventArgs e)
            {
                string connString = @"Data Source=(localdb)\MSSQLLocalDB; AttachDbFilename=D:\LIBRARY\ACADEMIC & EDUCATION\PROJECTS\I.T\VISUAL BASIC\A.C.C.M.S\LOCAL DATABASE FOR ACCMS.mdf; Integrated Security=True;";
                using (var conn = new SqlConnection(connString))
                {
                    string sqlString = @"select TOTALMEN from TOTALMEMBERSHIP where ID =1";
                    using (var command = new SqlCommand(sqlString,conn))
                    {
                        conn.Open();
                        var result = command.ExecuteScalar();
                        textBox1.Text = result.ToString();
                    }
                }
                
            }
        }
    }
    

    Best regards,

    Zhanglong


    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.

    Monday, February 12, 2018 7:41 AM
    Moderator