1

I'm having trouble using system.data.sqlclient namespace. I have made a very simple console application to save entered data in database. Need help regarding this, please help. I have attached screenshot of console.

using System;
using System.Data.SqlClient;

namespace StudentDetails_Proj
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Name:");
            string name = Console.ReadLine();
            Console.WriteLine("Enter Age:");
            string age = Console.ReadLine();
            Console.WriteLine("Enter Id:");
            string id = Console.ReadLine();
            Console.WriteLine("Enter Address:");
            string address = Console.ReadLine();
            // string connectionstring = "Server=G2W9DRV2E;Database=UserLogin;User Id=sa;Password=Password1";
            string connectionstring = "Server=G2W9DRV2E;Database=UserLogin;Integrated Security=true";
            SqlConnection connection = new SqlConnection(connectionstring);
            connection.Open();
            string cmdText = string.Format("Insert into StudentInfo values('{0}',{1},{2},'{3}')", name, age, id, address);
            SqlCommand cmd = new SqlCommand(cmdText, connection);
            cmd.ExecuteNonQuery();
            connection.Close();

        }
    }
}

I'm getting this error message:

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)

System.BadImageFormatException: Cannot load a reference assembly for execution

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vishal Gupta
  • 57
  • 1
  • 6
  • 2
    Try to install the [System.Data.SqlClient](https://www.nuget.org/packages/System.Data.SqlClient/) NuGet package. – mm8 Aug 09 '19 at 15:13
  • If you are working in .NET Core try this: https://stackoverflow.com/questions/49439150/could-not-load-file-or-assembly-system-data-sqlclient-version-4-2-0-2-when-i-us – Salah Akbari Aug 09 '19 at 15:13
  • If it is PCL try this https://stackoverflow.com/a/34674123/2946329 – Salah Akbari Aug 09 '19 at 15:15
  • Try Navigating to `Tools > Options > Projects and Solutions > Web Projects and check > Use the 64 bit version of IIS Express for web sites and projects` Clean up Solution and Build it and Run in Admin Mode – Nivas Pandian Aug 09 '19 at 15:16
  • Integrated Security=true means you are using a windows credential so you cannot use a login. Open Server with SQL Server Management Studio. There is a Server/Instance in the login window. Make sure you are using both the Server/Instance in your connection string. – jdweng Aug 09 '19 at 15:25
  • 1
    Thank you !! It worked after installing System.Data.SqlClient nuget package – Vishal Gupta Aug 12 '19 at 04:38
  • See my answer on **[System.BadImageFormatException: Reference assemblies should not be loaded for execution](https://stackoverflow.com/questions/61336306/system-badimageformatexception-reference-assemblies-should-not-be-loaded-for-ex/61702386#61702386)**. – Murat Yıldız May 09 '20 at 19:18

0 Answers0