stored procedure to display into a textBox

Post number 5 has been selected as best answer.

Daz66

Member
Joined
May 17, 2020
Messages
5
Programming Experience
Beginner
Hi all,

I'm new to the forum, and new to programming.

I've been following the Microsoft course book C# .NET for beginners an i'm about a month or so in (Lock down binge). I'm a 60's kid so i'm not looking for a career in programming but it's something I've always been interested in; so here we are.

I've managed to create myself a basic database manipulating the code from the exercises in the book to suite my own needs, and i think i'm doing quite well so far in making my spreadsheets redundant thanks to SQL and Windows forms. I'm having good fun.

I'm using visual studio, but coding in C#, so presumably i'm in the right place?, am i in the right place for a newb beginner? and do i simply copy and paste code straight into this text editor, or use Pastebin?

I would like some advice on trying to get a stored procedure (Sum of a column) to display into a textBox or a label, I have achieved this using dataGrid and can achieve it using an array, but trying to get it to work in a textbox is proving very difficult for me. I'm not looking for code, more a nudge in the right direction as i'm keen to learn and will be extremely grateful to anyone that can help, I will always search the forum before i ask.

Thanks in advance.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,227
Location
Virginia Beach, VA
Programming Experience
10+
Welcome.

To get the values inside an array to display in a textbox, you have to iterate over the elements of the array (or use some of .NET Framework library code do the iteration), and build a string that contains those values. The finally you assign that long string to the Text property of the textbox.
 

Daz66

Member
Joined
May 17, 2020
Messages
5
Programming Experience
Beginner
Thank you.
What i would like to do is have a stored procedure display to a textbox so the value of my column SUM updates in the text box as i add new jobs and values to the database.. As i'm understanding an array, i would need to input the value manually for each new job? I currently have that. I would like to point my stored procedure to a textbox. The following code shows the working AllRecords stored procedure and my attempt at getting results into a textbox with my SumTest stored procedure....go easy. :)

C#:
//!=======================================
//!GETS ALL RECORDS FROM DATABASE==WORKING
//!=======================================
BindingSource binder = new BindingSource();

private void btnGetAll_Click(object sender, EventArgs e)
{
    string conString;
    conString = Properties.Settings.Default.jobsConnectionString;

    SqlConnection con;
    con = new SqlConnection(conString);

    con.Open();

    SqlCommand cmd;
    cmd = new SqlCommand("AllRecords", con);
    cmd.CommandType = CommandType.StoredProcedure;

    SqlDataAdapter adaptor;
    adaptor = new SqlDataAdapter(cmd);

    DataTable dataTable;
    dataTable = new DataTable();

    adaptor.Fill(dataTable);

    dataGridView1.DataSource = binder;

    binder.DataSource = dataTable;
    con.Close();
}

//!=========================================================
//!ATTEMPT TO GET STORED PROCEDURE TO DISPLAY INTO A TEXTBOX
//!=========================================================
private void button1_Click(object sender, EventArgs e)
{
    string conString;
    conString = Properties.Settings.Default.jobsConnectionString;

    SqlConnection con;
    con = new SqlConnection(conString);

    con.Open();

    SqlCommand cmd;
    cmd = new SqlCommand("SumTest", con);
    cmd.CommandType = CommandType.StoredProcedure;

    SqlDataAdapter adaptor;
    adaptor = new SqlDataAdapter(cmd);

    SqlDataReader reader;
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        textBox1.Text = reader["SumTest"].ToString();
    }

    con.Close();
}
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,227
Location
Virginia Beach, VA
Programming Experience
10+
When you do an assignment to the textbox Text property, it will replace the old contents of the textbox. This is why you need to build a long string that contains all the values, and then assign that string to the Text property of the textbox.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,860
Location
Sydney, Australia
Programming Experience
10+
I didn't format my code in my last post so it was done for me, do i just use the toggle button when posting code so it displays correctly?
Like all formatting options, it is on the editor toolbar. There are a number of options under the ellipsis, including code. Paste your code in the dialogue provided and ensure that it is formatted correctly, e.g. no fat wads of leading whitespace. The dialogue does do some of the code formatting for you and you can use Ctrl+A followed by Shift+Tab to format the lot, as long as it's a valid and complete block. That will remove block-wide leading withespace.
 

Daz66

Member
Joined
May 17, 2020
Messages
5
Programming Experience
Beginner
Like all formatting options, it is on the editor toolbar. There are a number of options under the ellipsis, including code. Paste your code in the dialogue provided and ensure that it is formatted correctly, e.g. no fat wads of leading whitespace. The dialogue does do some of the code formatting for you and you can use Ctrl+A followed by Shift+Tab to format the lot, as long as it's a valid and complete block. That will remove block-wide leading withespace.
Much appreciated, thank you.
 

Daz66

Member
Joined
May 17, 2020
Messages
5
Programming Experience
Beginner
Sorted, with 1 line of code....and the removal of 6. :)

Many thanks for the nudge.

C#:
string conString;
conString = Properties.Settings.Default.jobsConnectionString;
SqlConnection con;
con = new SqlConnection(conString);
con.Open();
SqlCommand cmd;
cmd = new SqlCommand("SumTest", con);
cmd.CommandType = CommandType.StoredProcedure;
textBox1.Text = cmd.ExecuteScalar().ToString();
con.Close();
 
Top Bottom