[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.2

C# windows application cannot connect on other machines

Asked by knowlton in C# Programming Language

"It works on my box, that's all that matters...."

When I run my C# windows application which connects to SQL Server through the network, it connects and reads/writes just fine.  If I copy the EXE to another macine, no joy.

My authentication form attempts to authenticate against the database and KABOOM.

Trying to figure-out why, but in the meantime, I thought it would be prudent to post here on EE and see what feedback I get.

The other machine I tested on is able to ping the SQL Server box over the network.

My connection string is (with some data obfuscated):

  <connectionStrings>
    <add name="leadcollatordb" connectionString="Data Source=10.1.1.XXX\SQLEXPRESS;Initial Catalog=LeadCollator;User Id=XXXXXXXXXXXX;Password=jXXXXXXXXXXXXXXXXXXXXXX;"/>
  </connectionStrings>


DataHelper is a public static class.

Right now there is NO constructor.  But if I need one ... why has it been working?  Even the compiled exe works fine outside of the dev environment (with visual studio shut down).

Here is the exception:


See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.TypeInitializationException: The type initializer for 'FinishPointLeadCollator.DataHelper' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at FinishPointLeadCollator.DataHelper..cctor()
   --- End of inner exception stack trace ---
   at FinishPointLeadCollator.DataHelper.ReturnDataSet(String sql)
   at FinishPointLeadCollator.FormAuthenticate.UserExists()
   at FinishPointLeadCollator.FormAuthenticate.buttonLogin_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3603 (GDR.050727-3600)
    CodeBase: file:///c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
FinishPointLeadCollator
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Documents%20and%20Settings/Rob/Desktop/Release/FinishPointLeadCollator.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Data
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3082 (QFE.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.5.423 / Virus Database: 270.14.26/2451 - Release Date: 10/22/09 08:51:00
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
Here is DataHelper.cs
 
//I'll omit the usings...
 
namespace FinishPointLeadCollator
{
    static class DataHelper
    {
        private static string global_connection_string = ConfigurationManager.ConnectionStrings["leadcollatordb"].ConnectionString;
        
 
        public static SqlDataReader ReturnSqlDataReader(string sql)
        {
            SqlConnection conn = new SqlConnection(global_connection_string);
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = System.Data.CommandType.Text;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
 
            //do not close the connection here ... it will close when you close the reader
            //back in the caller.
        }
 
        public static DataSet ReturnDataSet(string sql)
        {
            SqlConnection conn = new SqlConnection(global_connection_string);
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = System.Data.CommandType.Text;
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            conn.Close();
            return ds;
        }
 
 
        public static DataSet RunStoredProcReturnDataSet(string tempsql)
        {
            using (var conn = new SqlConnection(global_connection_string))
            {
                //Taken from:  http://davidhayden.com/blog/dave/archive/2006/01/27.aspx
                //Creating a Stored Procedure Example using SQL Server Management Objects (SMO) and SQL Server 2005
                //After doing a little investigation, SMO allows me to create stored procedures in SQL Server 2005 programatically in my code generator to keep me from having to manually copy and paste the scripts.
                //SMO, a .NET based object model, ships with SQL Server 2005 in an assembly named Microsoft.SqlServer.Smo.dll. Some other supporting DLLs are also included in C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies folder. These include Microsoft.SqlServer.ConnectionInfo.dll and Microsoft.SqlServer.SmoEnum.dll. Like any other assembly, to use SMO in your application, add reference to Microsoft.SqlServer.Smo.dll (and Microsoft.SqlServer.ConnectionInfo.dll) and begin using the SMO classes.
                //Shown below is an example of creating a stored procedure, called GetClubByID, in a database, called MyDatabase.  The comments pretty much tell the story.
 
                string tem = "if exists (select 1 from dbo.sysobjects where id = object_id(N'[dbo].[SelectDataToExport]') and OBJECTPROPERTY(id , N'IsProcedure') = 1 ) begin drop procedure SelectDataToExport end";
                DataHelper.RunSQLStatement(tem);
 
 
                Server server = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection(conn));
                Database db = server.Databases["LeadCollator"];                
                StoredProcedure mySP = new StoredProcedure(db, "SelectDataToExport");
                mySP.TextMode = false;
                mySP.AnsiNullsStatus = false;
                mySP.QuotedIdentifierStatus = false;
                mySP.TextBody = tempsql;
                mySP.Create();
 
                SqlCommand cmd = new SqlCommand("dbo.SelectDataToExport",conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;                
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                conn.Close();
                return ds;
            }
        }
 
 
        public static void RunSQLStatement(string sql)
        {
            SqlConnection conn = new SqlConnection(global_connection_string);
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = System.Data.CommandType.Text;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
 
        public static int GetUserID(string username)
        {
            DataSet ds = DataHelper.ReturnDataSet("select id from userinfo where username = '" + username + "'");
 
            int id = -1;
 
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                id = Convert.ToInt32(dr[0].ToString());
            }
 
            return id;
        }
 
        public static int GetFriendlyBatchNameID(string batchfriendlyname)
        {
            DataSet ds = DataHelper.ReturnDataSet("select id from batchnames where friendlyname = '" + batchfriendlyname + "'");
 
            int id = -1;
 
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                id = Convert.ToInt32(dr[0].ToString());
            }
 
            return id;
        }
    }
}
 
 
 
Here is my Authentication code:
 
 
namespace FinishPointLeadCollator
{
    public partial class FormAuthenticate : Form
    {
        private FormMainLeadCollator _fmlc;
 
        public FormAuthenticate(FormMainLeadCollator fmlc)
        {
            this._fmlc = fmlc;
 
            InitializeComponent();
        }
 
        public FormAuthenticate()
        {
            InitializeComponent();
        }
 
        private void buttonExitApplication_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
 
        private void buttonLogin_Click(object sender, EventArgs e)
        {               
            if(UserExists())
            {
                this._fmlc._username = this.textBoxUserName.Text.Trim();
                Close();
            }
            else
            {
                this.textBoxUserName.Text = "";
                this.textBoxUserPassword.Text = "";
                MessageBox.Show("username / password combination does not exist");                
            }
        }
 
        private bool UserExists()
        {
            string tempsql = "select * from UserInfo where username = '" + this.textBoxUserName.Text.Trim() + "' and password = '" + this.textBoxUserPassword.Text.Trim() + "'";
 
 
//HERE IS WHERE THE EXCEPTION IS RAISED...
            DataSet ds = DataHelper.ReturnDataSet(tempsql);
 
            if (ds.Tables[0].Rows.Count == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
 
      
    }
}
[+][-]10/22/09 09:53 AM, ID: 25636296Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: C# Programming Language
Sign Up Now!
Solution Provided By: lucius_the
Participating Experts: 1
Solution Grade: A
 
[+][-]10/22/09 12:23 PM, ID: 25637965Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_EXPERT_20070906