Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a global function code using C#.

My code is as follows:
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;


namespace Faculty_Schedule_Time_Table
{

        public class Global_Function
    {

            public SqlConnection con = new SqlConnection();
            private string connectionstring ="Data Source=NARASIMAN;connect timeout=120;initial catalog=master";
            private SqlCommand cmd;
            private SqlDataReader dr;

            public string error;

            public Global_Function()
            {

            }

            public void BindCon()
            {
                con = new SqlConnection();
                con.ConnectionString = connectionstring;
                con.Open();

            }

            public void insertdata(String SQL)
            {
                try
                {
                    BindCon();
                    cmd = new SqlCommand(SQL, con);
                    cmd.ExecuteReader();
                }
                 catch(Exception e1)
                  {
                   error = e1.Message.ToString();
                  }
            }
            public SqlDataReader ReadSql(string SQL)
            {

            con = new SqlConnection(connectionstring);
            con.Open();
            cmd  = new SqlCommand(SQL,con);
            dr = cmd.ExecuteReader();
            return (dr);
            
        }
           
            public string CntString()
        {
            return connectionstring;
        }
    }
}

When I run error shows as follows:
Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection.

The above error shows in the below code con.open(); line
C#
public SqlDataReader ReadSql(string SQL)
       {

       con = new SqlConnection(connectionstring);
       con.Open();(in this line the above error shows)
       cmd  = new SqlCommand(SQL,con);
       dr = cmd.ExecuteReader();
       return (dr);

   }

How can I fix this?

Please help me.

Regards & Thanks,
Narasiman P
Posted
Updated 3-Feb-13 5:37am
v3

1 solution

Your connection string is not good. You have to either specify user name and password or set it to use trusted connection (check here for proper syntax: http://www.connectionstrings.com/sql-server-2008[^], supposing SQL 2008; but there you can find other versions of SQL server and other datasources too).
Could be this:
private string connectionstring =@"Server=NARASIMAN;Database=master;Trusted_Connection=True;";
 
Share this answer
 
v2

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