Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I have written a global function code using C#.
 
My code is as follows:
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
     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 3-Feb-13 5:26am
Edited 3-Feb-13 5:37am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;";
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 566
1 OriginalGriff 345
2 CPallini 200
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,028
2 CPallini 4,700
3 George Jonsson 3,142
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 3 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100