Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2012/5.0 > BOOK: Beginning Object-Oriented Programming with C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Object-Oriented Programming with C#
This is the forum to discuss the Wrox book Beginning Object-Oriented Programming with C# by Jack Purdum; ISBN: 978-1-1183-3692-2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Object-Oriented Programming with C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 26th, 2014, 10:48 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 14 - Unable to list DB from the given sample program.

Dear Sir,

Referring to the first Sample program from chapter 14.

I have encountered this error "InvalidArgument=Value of "0" is not valid for 'SelectedIndex' during run the Sample program "Ch14createDatabase" -> Database management Subsystem > click on file > click on Select Server.

The error is captured from frmserverSelect.cs file as below.

private void btnServer_Click(object sender, EventArgs e)
{
short i;

cmbServer.SelectedIndex = 0; // Use the server they selected to list databases
serverToUse = cmbServer.SelectedItem.ToString();

string conn = "Data Source=" + serverToUse + "; Integrated Security=True;";
cmbDatabase.Visible = true;
lblDb.Visible = true;

try
{
using (SqlConnection sqlConn = new SqlConnection(conn))
{
sqlConn.Open();
DataTable tblDbs = sqlConn.GetSchema("Databases");
sqlConn.Close();

foreach (DataRow row in tblDbs.Rows)
{
i = (short)row.ItemArray[1];
if (i > SYSTEMDBTYPES)
cmbDatabase.Items.Add(row["database_name"].ToString());
}
}
cmbDatabase.SelectedIndex = 0; error occur here....
} catch (SqlException ex)
{
MessageBox.Show("Error occurred while reading database data: " + ex.Message);
}
}

I have followed every steps from the book. From my understanding this code is used to display the available database on the machine(my PC). In my case, i think, it fail to list out the SQL server name...

By the way, is there any way to use C# to detect the available SQL server on the local PC?

Perhaps, you can advice what goes wrong the codes or the System configuration so that i can resolved.

My PC setup..
1.Installed Microsoft Visual Studio 2012.
2.Installed Microsoft SQL Server Developer
3.Created New rule for TCP port 1433.

Thanks ..

Regards,
ReGuy
  #2 (permalink)  
Old January 27th, 2014, 01:24 PM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default DB error

Make sure you are using the correct Microsoft Framework. The code was compiled using the old 3.5 Framework and you are likely running a later version. Go to the Project menu, select Properties-->Application and see which framework is there. It should match your compiler's environment. If you still have problems, go to the Build properties option and change the platform target to x86. Let me know what you find out.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
  #3 (permalink)  
Old August 19th, 2014, 09:58 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: Chapter 14 - Unable to list DB from the given sample program.

For anyone else who ran into this problem.
I too ran into the same problem and realized that it had to do with the named instance on my computer. what I did was to concatenate the server name and instance name in the combobox items. Like this:
Code:
 foreach (DataRow row in dbTable.Rows)
            {
                SqlSL = new clsSqlServerList();
                serverName = row[0].ToString();
                
Quote:
cmbServer.Items.Add(serverName +"\\" + row["InstanceName"])
Also you could use the following console application code to list SQL server instances:
Code:
Hope this helps. Good luck!

using System.Data;

using System.Data.Sql;

using System;

public class ListSqlInstances
{

    public static int Main(string[] args)
    {

        SqlDataSourceEnumerator sqldatasourceenumerator1 = SqlDataSourceEnumerator.Instance;

        DataTable datatable1 = sqldatasourceenumerator1.GetDataSources();

        foreach (DataRow row in datatable1.Rows)
        {

            Console.WriteLine("****************************************");

            Console.WriteLine("Server Name:" + row["ServerName"]);

            Console.WriteLine("Instance Name:" + row["InstanceName"]);

            Console.WriteLine("Is Clustered:" + row["IsClustered"]);

            Console.WriteLine("Version:" + row["Version"]);

            Console.WriteLine("****************************************");
         
        }
        Console.ReadLine();
        return 1;
  #4 (permalink)  
Old November 30th, 2014, 10:40 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Unfortunately, nothing works. I can use SQL Server, I made a new database Cards with three tables. I filled them with the info, however neither Chapter14ProgramCreateDatabase program nor this console application can see my SQL Server.

What should I try?
Would be very grateful for help

My PC setup..
1.Installed Microsoft Visual Studio 2014.
2.Installed Microsoft SQL 2014
3.Created New rules for TCP port 1433 and dynamic ports.
4. Turned off Microsoft Fire Wall.
5. OS is Windows 7 Professional 64-bit
  #5 (permalink)  
Old December 1st, 2014, 01:06 AM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default DB Problem

Saying "nothing works" is not helpful in figuring out what's wrong. You are the first person who has not been able to get the code working, which suggests there's something wrong on your end. What are the error messages you are getting? What does happen when you run the program? Is anything displayed on the screen?
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
  #6 (permalink)  
Old December 1st, 2014, 06:19 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If I start the program and try to select server (File-Select server) in 10 or so seconds I can see cmbServer with the only name, which is the name of my computer (Toshiba). Actual SQL Server name is: Toshiba\SQLEXPRESS.

Of course if I select it I have an error window with the message:
=================================================
Error occurred while reading database data: A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connection. (provider: Named Pipes Provider, error: 40-Could not open a connection to SQL Server.)
=================================================

After I close window (named: Select server and Database) this line:

this.mdiParent.getDatabaseName = cmbDatabase.SelectedItem.ToString();

in frmServerSelect throws an exception:
=================================================

Microsoft Visual Studio Express 2013 for Windows Desktop

An unhandled exception of type 'System.NullReferenceException' occurred in Ch14CreateDatabase.exe

Additional information: Object reference not set to an instance of an object.
=================================================

If I hardcode I can establish the connection with database.

Console application only proofs that SqlDataSourceEnumerator Class does not work properly on my computer.
=================================================

using System.Data;

using System.Data.Sql;

using System;

public class ListSqlInstances
{

public static int Main(string[] args)
{

SqlDataSourceEnumerator sqldatasourceenumerator1 = SqlDataSourceEnumerator.Instance;

DataTable datatable1 = sqldatasourceenumerator1.GetDataSources();

foreach (DataRow row in datatable1.Rows)
{

Console.WriteLine("******************************* *********");

Console.WriteLine("Server Name:" + row["ServerName"]);

Console.WriteLine("Instance Name:" + row["InstanceName"]);

Console.WriteLine("Is Clustered:" + row["IsClustered"]);

Console.WriteLine("Version:" + row["Version"]);

Console.WriteLine("******************************* *********");

}
Console.ReadLine();
return 1;
}
}
=================================================

The result is:
================================
Server Name:Toshiba
Instance Name:
Is Clustered:
Version:
================================

What should I do to get it working?
I really appreciate your help.

Best regards.
  #7 (permalink)  
Old December 1st, 2014, 08:24 PM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default

It sounds like your connection string may be wrong. Check that it is as specified, with the necessary commas and (perhaps) empty fields. Also, if your server requires a user ID and password, that must be figured in, too.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
  #8 (permalink)  
Old December 6th, 2014, 07:50 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

Thank you VERY MUCH to all who helped me to solve the problem!!! A few days of frustration and struggle…I would like to share my experience and might help somebody else who are in the same trap.

If you experience a problem with the program see the answer of olo-B above and concatenate the server name:

Code:
cmbServer.Items.Add(serverName +"\\" + row["InstanceName"]);
If it still does not help then,

In Microsoft Visual Studio open Database Explorer and try to add a new connection.

In a list of server names could be only the name of your computer, even if you hit the “Refresh” button.

After you type the name of the server, there is a list of databases. Chose a database which you need and test the connection (there is the button). You can receive a confirmation that test connection succeeded.

However, when you hit OK, you got an error message:
-------------------------------------------------------------------------------------
Database Explorer

Unable to add data connection. Could not load file or assembly ‘Microsoft.SqlServer, Management. Sdk.Sfc, Version=11.0.0.0, Cuture=neutral, PublickeyToken=89845dcd8080cc91’ or one of its dependencies. The system cannot find the file specified.
--------------------------------------------------------------------------------------
It means that you need to install two missing components, which you can find in:
Microsoft® SQL Server® 2012 SP2 Feature Pack
http://www.microsoft.com/en-us/downl....aspx?id=43339
These components are:
1) SQL System CLR types
and
2) SQL 2012 Shared Management Objects
[SQL 2012 Shared Management Objects] has dependency on [SQL System CLR types] component, so you have to install SQL Server System CLR Types first.

For more info go to:
http://sqlbeyond.blogspot.co.uk/2012...d-file-or.html
My specs:
OS Windows 7 Professional
Microsoft Visual Studio Express 2013 for Windows Desktop
Microsoft SQL Server Express 2014
Microsoft Security Essentials
Microsoft Fire Wall

Good luck!
  #9 (permalink)  
Old December 14th, 2014, 12:52 PM
Registered User
Points: 27, Level: 1
Points: 27, Level: 1 Points: 27, Level: 1 Points: 27, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2010
Posts: 6
Thanks: 0
Thanked 1 Time in 1 Post
Default

Im getting the same error as someone else did: "InvalidArgument=Value of "0" is not valid for 'SelectedIndex'.

http://i.imgur.com/h6LdKRC.png
http://i.imgur.com/G4d8ywS.png
http://i.imgur.com/HOkY1jK.png

I've tried the following suggestions:
- cmbServer.Items.Add(serverName +"\\" + row["InstanceName"]);
- Changed configuration to Net 3.5 with and without x86:
http://i.imgur.com/SL6xoR4.png
http://i.imgur.com/fMx922X.png

Anyone have any suggestions?

"If I manually add an item to the combobox with my servername: ""(localdb)\MSSQLLocalDB" I'm able to connect and get the Cards database to show in the combobox on the form.

Last edited by Cryotic; December 14th, 2014 at 12:54 PM.
  #10 (permalink)  
Old December 16th, 2014, 08:10 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Wink

Quote:
Originally Posted by C#Learner View Post
Thank you VERY MUCH to all who helped me to solve the problem!!! A few days of frustration and struggle…I would like to share my experience and might help somebody else who are in the same trap.

If you experience a problem with the program see the answer of olo-B above and concatenate the server name:

Code:
cmbServer.Items.Add(serverName +"\\" + row["InstanceName"]);
If it still does not help then,

In Microsoft Visual Studio open Database Explorer and try to add a new connection.

In a list of server names could be only the name of your computer, even if you hit the “Refresh” button.

After you type the name of the server, there is a list of databases. Chose a database which you need and test the connection (there is the button). You can receive a confirmation that test connection succeeded.

However, when you hit OK, you got an error message:
-------------------------------------------------------------------------------------
Database Explorer

Unable to add data connection. Could not load file or assembly ‘Microsoft.SqlServer, Management. Sdk.Sfc, Version=11.0.0.0, Cuture=neutral, PublickeyToken=89845dcd8080cc91’ or one of its dependencies. The system cannot find the file specified.
--------------------------------------------------------------------------------------
It means that you need to install two missing components, which you can find in:
Microsoft® SQL Server® 2012 SP2 Feature Pack
http://www.microsoft.com/en-us/downl....aspx?id=43339
These components are:
1) SQL System CLR types
and
2) SQL 2012 Shared Management Objects
[SQL 2012 Shared Management Objects] has dependency on [SQL System CLR types] component, so you have to install SQL Server System CLR Types first.

For more info go to:
http://sqlbeyond.blogspot.co.uk/2012...d-file-or.html
My specs:
OS Windows 7 Professional
Microsoft Visual Studio Express 2013 for Windows Desktop
Microsoft SQL Server Express 2014
Microsoft Security Essentials
Microsoft Fire Wall

Good luck!
Hi to all,

I need to make a note to my previous post. After you’ve downloaded and installed two additional components you also should start SQL Browser (it does not start by default).

Go to all programs
Microsoft SQL Server 2014
Configuration Tools
SQL Server 2014 Configuration Manager and start it.

On the left side left-click SQL Server Services.
On the right site you can see SQL Server Browser. If it has a red square, it means it does work at the moment.

Right –click it and go to Properties. In a check box Built-in Account should be a Local Service.
Go to Service (it’s on the top of window) and in the Start Mode choose Automatic.
Push Apply.

If you restart SQL Server 2014 Configuration Manager, SQL Server Browser should have a green arrow.

Now your program should work.
Good luck!
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 19: Configuring the File System (unable to open DB) Happs BOOK: Beginning ASP.NET 4.5 : in C# and VB 10 September 20th, 2013 05:07 AM
Unable to open sample code downloaded williamgcl BOOK: Beginning Microsoft Visual Studio LightSwitch Development 0 August 17th, 2011 03:53 AM
unable to get Chapter 4 To do list 2 to work abhi BOOK: Professional Android 2 Application Development 2 June 2nd, 2010 10:38 PM
Chapter 14 Code example in Oracle DB theinscrutable BOOK: Professional Jakarta Struts 0 January 10th, 2005 01:31 PM



All times are GMT -4. The time now is 09:50 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.