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();
}
}
}
dataGridView1.Refresh();after assigning datasource