Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everybody, I always got the error while I debug my program and the error is that "DataBaseProject.exe does not contain a static 'Main' method suitable for an entry point" and even I change its properties from Console Application to Class Library, then its build successfully but it does not work well and nothing show on the console window.So please help me to solve my this issue.Here's below is the code:


class Program
   {
       static void Main(string[] args)
       {
           int choice = 0;
           do
           {
               Console.WriteLine(" 1)Enter 1 to add a student.");
               Console.WriteLine(" 2)Enter 2 to view the all student.");
               Console.WriteLine(" 3)enter 3 to delete a student.");
               Console.WriteLine(" 4)enter 4 to exit");
               Console.Write("pleaswe enter your choice :");
               choice = Convert.ToInt32(Console.ReadLine());
               switch (choice)
               {
                   case 1:
                      Addstudent();
                       break;
                   case 2:
                      viewstudent();
                       break;
                   case 3:
                      deletestudent();
                       break;
               }
           } while (choice != 4 );
       }


         static void Addstudent()
          {
           Console.Write("enter the student name");
           string  name = Console.ReadLine();
           System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection();
           connection.ConnectionString = @"Data Source=ps201\sqlexpress;Initial Catalog=Info_studednt;Integrated Security=True;Pooling=False";
           System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
           command.Connection = connection;
           connection.Open();
           command.CommandText = "insert into student (name) values (' " + name + " ')";
           int rowAffected = command.ExecuteNonQuery();
           if (rowAffected != 0)
           {
               Console.WriteLine("Error:student not inserted..");
           }
           else
           {
               Console.WriteLine("Sucess:student insert successfully..");
           }

        }
            static void viewstudent()
            {
                System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection();
                connection.ConnectionString = @"Data Source=ps201\sqlexpress;Initial Catalog=Info_studednt;Integrated Security=True;Pooling=False";
                System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
                command.Connection = connection;
                command.CommandText = "selsct * from student";
                System.Data.SqlClient.SqlDataReader dr = command.ExecuteReader();
                Console.WriteLine("rollno \t name");
                while (dr.Read())
                {
                    Console.Write(dr["name"] + ":" + dr["rollno"]);
                }
            }

          static void deletestudent()
          {
              Console.Write("enter the student rollno");
              string rollno = Console.ReadLine();
              System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection();
              connection.ConnectionString = @"Data Source=ps201\sqlexpress;Initial Catalog=Info_studednt;Integrated Security=True;Pooling=False";
              System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
              command.Connection = connection;
              command.CommandText = "delete from student where =(' " + rollno + " ')";
              int rowAffected = command.ExecuteNonQuery();
              if (rowAffected != 0)
              {
                  Console.WriteLine("Error:student not deleted..");
              }
              else
              {
                  Console.WriteLine("Sucess:student deeleted  successfully..");
              }

          }
Posted
Updated 7-Sep-14 22:25pm
v2
Comments
Prasad Avunoori 8-Sep-14 4:28am    
Take new Console application and try the above code.

1 solution

That should work as a console app - with the exception that I'd normally expect a Namespace as well as a class.
Try making sure that you have this at the top of the file:
C#
using System;

namespace TCFConsole
    {
And a matching "}" at the end.

And BTW: SELECT has only one "s":
C#
command.CommandText = "selsct * from student";
                          ^



"Okey, can you please give me the brief example of parameterised query and how I need to write its code and what I need to mention there, because I am new to C#."

C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
        {
        cmd.Parameters.AddWithValue("@C1", myValueForColumn1);
        cmd.Parameters.AddWithValue("@C2", myValueForColumn2);
        cmd.ExecuteNonQuery();
        }
    }
 
Share this answer
 
v2
Comments
AnoopGharu 8-Sep-14 5:13am    
Sir I did all the above work which you mentioned but in actual I also getting confuse while I try to write the insert query into the AddStudent function and when I try to Write the the "values(value1, value2)" then i didn't understand what I need to write there I just wrote there "name" variable which I'd already declare in AddStudent and when I write it, then it could lead to an error.So can you please tell me what I need to mention there and also about my problem.
OriginalGriff 8-Sep-14 5:23am    
No, that isn't a good idea - it leads to SQL Injection which can destroy your database.
You should be using a parameterised query instead.
AnoopGharu 8-Sep-14 5:29am    
Okey, can you please give me the brief example of parameterised query and how I need to write its code and what I need to mention there, because I am new to C#.
OriginalGriff 8-Sep-14 5:42am    
Answer updated
AnoopGharu 8-Sep-14 6:18am    
Thank you Sir, here's in this ViewStudent function I got the one more error is "Cannot implicitly convert type 'int' to 'System.Data.SqlClient.SqlDataReader'" during the execution of "ExecuteNonQuery".Why always I got the error at this stage and how I can resolve it.

public static void ViewStudent()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=ps201\sqlexpress;Initial Catalog=Info_studednt;Integrated Security=True;Pooling=False";
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "select * from students";
SqlDataReader dr=com.ExecuteNonQuery(); //Error
while(dr.Read())
{
Console.WriteLine(dr["name"]+":"+dr["rollno"]);
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900