question

BenTam-3243 avatar image
0 Votes"
BenTam-3243 asked BenTam-3243 commented

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);
         }
     }
    
 }

121368-image.png


dotnet-csharp
image.png (54.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BenTam-3243 avatar image
0 Votes"
BenTam-3243 answered BenTam-3243 commented

@karenpayneoregon

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?
122688-form1.gif



form1.gif (20.6 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

dataGridView1 is a DataGridView, add it to the form from the toolbox.

0 Votes 0 ·

Hi Karen. I have solved the problem above. However when I built the program, it showed 3 errors as follows. Originally I typed <appSettings> in the app.config file, the errors still existed. Thus I changed the case to <AppSettings>. However the errors still not went away. Could you advise how to fix them? Might I know how to implement requires 1?

TIA

app.config

123037-appconfig.gif

Operatoins

123077-code.gif

Err

123078-err.gif



0 Votes 0 ·
appconfig.gif (7.6 KiB)
err.gif (11.3 KiB)
code.gif (14.6 KiB)

Did you add a reference to System.Configuration.ConfigurationManager to your project? If not added this reference.

0 Votes 0 ·
Show more comments
Castorix31 avatar image
1 Vote"
Castorix31 answered BenTam-3243 commented

You can define as static :

 private static void ConnectServer(SqlConnection cnn, string ServerName, string DatabaseName, string InitCatalog)

(but there are other errors (sevrer, ...)



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Castorix. Thanks for your replay.

0 Votes 0 ·
TonyHill-2279 avatar image
0 Votes"
TonyHill-2279 answered BenTam-3243 commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Tony. Thanks for your reply.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered

I think that it should be 'Program.ConnectServer(ref cnn, ...)' and 'private static void ConnectServer(ref SqlConnection cnn, ...)'.



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Viorel. Thanks for your reply.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered BenTam-3243 commented

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
121409-courses.png

In closing the repository mentioned above has many more examples

121384-figure1.png



courses.png (10.1 KiB)
figure1.png (24.7 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Karen. Thanks for your reply. It may take some time to try you samples.

0 Votes 0 ·

Hi Karen. Thanks for your reply. I'll try your examples later.

0 Votes 0 ·