I followed your examples. When I opened Form1.cs and copied the code to the editor, I found the and error at line 27 (see attached figure). Could your advise how to fix it?
How to remove the red line

Dear All,
Could anyone please tell me how to remove the red line? The code window is for you to reproduce the issue. The red line is shown in the attached figure.
TIA
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NewTIMS
{
public class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
SqlConnection cnn;
Program.ConnectServer(cnn, "(local)", "WelkinDB", "Student");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Student_Form());
}
private void ConnectServer(SqlConnection cnn, string ServerName, string DatabaseName, string InitCatalog)
{
String connectionString;
connectionString = @"Sevrer=" + ServerName + ";Data Source=" + DatabaseName + "; Initial Catalog = " + InitCatalog + "; trusted_connection = yes;)";
cnn = new SqlConnection(connectionString);
}
}
}
Developer technologies | C#
-
Ben Tam 216 Reputation points
2021-08-12T14:38:06.31+00:00
4 additional answers
Sort by: Most helpful
-
Castorix31 90,686 Reputation points
2021-08-08T09:27:06.8+00:00 You can define as static :
private static void ConnectServer(SqlConnection cnn, string ServerName, string DatabaseName, string InitCatalog)
(but there are other errors (sevrer, ...)
-
Tony Hill 1 Reputation point
2021-08-08T09:34:31.177+00:00 Try changing the ConnectServer signature to
private static void ConnectServer(SqlConnection cnn, string ServerName, string DatabaseName, string InitCatalog)
the change to call to it from
Program.ConnectServer(cnn, "(local)", "WelkinDB", "STudent");
to
ConnectServer(cnn, "(local)", "WelkinDB", "STudent");
That should solve that problem but I fear there may be other problems but without knowing what you are trying to achieve I cannot advise further.
-
Viorel 122.6K Reputation points
2021-08-08T10:26:20.27+00:00 I think that it should be 'Program.ConnectServer(ref cnn, ...)' and 'private static void ConnectServer(ref SqlConnection cnn, ...)'.
-
Karen Payne MVP 35,586 Reputation points Volunteer Moderator
2021-08-08T11:28:48.757+00:00 The proper way is to do a connection string.
Full source found in this repository with data script.
- Place your connection string in app.config (or appsettings.json), the following will use app.config
- Create a class for data operations
App.config sample
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> </startup> <appSettings> <add key="ConnectionString" value="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=School;Integrated Security=True;Connect Timeout=30;" /> </appSettings> </configuration>
Class for working with data, here is only one method to show it will work. Of course if using say a DataAdapter you would adapt to a DataAdapter or if a TableAdapter the sample.
using System; using System.Data; using System.Data.SqlClient; using static System.Configuration.ConfigurationManager; namespace WindowsFormsConventional.Classes { public class Operations { /// <summary> /// Test connection /// </summary> /// <returns></returns> public static (bool success, Exception exception) TestConnection() { try { using (var cn = new SqlConnection() { ConnectionString = AppSettings["ConnectionString"] }) { cn.Open(); return (true, null); } } catch (Exception exception) { return (false, exception); } } /// <summary> /// Simple read joined tables /// </summary> /// <returns></returns> public static (DataTable dataTable, Exception exception) GetCourses() { DataTable table = new DataTable(); var selectStatement = "SELECT C.CourseID, C.Title, C.Credits, C.DepartmentID, D.Name FROM Course AS C " + "INNER JOIN Department AS D ON C.DepartmentID = D.DepartmentID"; using (var cn = new SqlConnection() {ConnectionString = AppSettings["ConnectionString"]}) { using (var cmd = new SqlCommand() { Connection = cn, CommandText = selectStatement }) { cn.Open(); try { table.Load(cmd.ExecuteReader()); table.Columns["CourseID"].ColumnMapping = MappingType.Hidden; table.Columns["DepartmentID"].ColumnMapping = MappingType.Hidden; return (table, null); } catch (Exception exception) { return (table, exception); } } } } } }
Form code
using System; using System.Windows.Forms; using WindowsFormsConventional.Classes; namespace WindowsFormsConventional { public partial class Form1 : Form { private readonly BindingSource _bindingSource = new BindingSource(); public Form1() { InitializeComponent(); Shown += OnShown; } private void OnShown(object sender, EventArgs e) { var (success, exception) = Operations.TestConnection(); if (success) { var (dataTable, _) = Operations.GetCourses(); _bindingSource.DataSource = dataTable; dataGridView1.DataSource = _bindingSource; } else { MessageBox.Show(exception.Message); } } } }
Screenshot
In closing the repository mentioned above has many more examples