0

UPDATE: This great answer from Steve https://stackoverflow.com/a/57692761/5758150 plus needing to include: Integrated Security=SSPI in my connection string has resolved this issue. Many thanks to all for their assistance.

I am a C# newb and this is my first project. The code is designed to query a sql server table and return the results into a datagridviewer on a windows Form. The form compiles with no errors but simply shows a blank datagridviewer.

Can anyone advise what I am doing wrong please? I have checked the server name db name etc and they are all correct.

using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System;
using System.Data.SqlClient; //For SQL Connection


namespace Reference_Table_Updater
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


    void GetList()
    {

        String strConnection = "server;" +
                   "Database='Scratchpad';";

        SqlConnection con = new SqlConnection(strConnection);

        SqlCommand sqlCmd = new SqlCommand();
        sqlCmd.Connection = con;
        sqlCmd.CommandType = CommandType.Text;
        sqlCmd.CommandText = "Select * from dbo.UPDATE_Test";
        SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

        DataTable dtRecord = new DataTable();
        sqlDataAdap.Fill(dtRecord);
        dataGridView1.DataSource = dtRecord;

    }

    private void Form1_Load()
    {
        GetList();
    }




}



}
7
  • Have you tried calling dataGridView1.Refresh(); after assigning datasource Commented Aug 28, 2019 at 12:32
  • The code seems to be correct. However that connectionstring seems to be a fake, In that form It shouldn't work but you should have an error when executing the Fill method. What is the real connectionstring? Commented Aug 28, 2019 at 12:38
  • Yeah it makes no difference. Commented Aug 28, 2019 at 12:39
  • String strConnection = "server= uk-DEV-dtwh-01\\WH02;" + "Database='Scratchpad';"; Commented Aug 28, 2019 at 12:41
  • I escaped the \ in the server name with \\ Commented Aug 28, 2019 at 12:42

2 Answers 2

1

That Form_Load is not correct. Probably you have added it manually and it is never called. Of course the grid is not filled.

To have your Form_Load called you need to bind it to the Load event

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Load += Form_Load;    
    }
    private void Form_Load(object sender, EventArgs e)
    {
        GetList();
    }

Of course, this binding of event handlers to events is usually done using the Form Designer, double clicking on the event that you want to handle. (and thus is not required to write the binding in your form constructor)

Now, your GetList should be called and if there are errors there they should be raised with an exception. The GetList code seems to be correct, unless there is a problem with the connectionstring or the database table.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok so this has got somewhere now but is throwing Additional information: Login failed for user ''. I am using windows auth so a bit confused.
If you use Windows Authentication then you shouldn't provide a username and a password.
"Integrated Security=SSPI";
Yes but no username and password.
ConnectionStrings.com or create a UDL file and use it to create a connection string, then read the UDL file as a text file.
0

Here you need to use user and password String strConnection = "server;" + "Database='Scratchpad';"; Like this example: "SERVER=localhost; user id=root; password=; database=Scratchpad"; Or are you using windows autentication?

2 Comments

windows auth all the way mate
Yes, please see the update at the top of the original post. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.