I am very, very new to C# & VS2010 .net programming, and this is my first ever attempt at SQL, though I am an experienced Mainframe programmer & have done a bit of VB & VBA.
I have a single Form with a single ComboBox and I am trying to populate it via a new SQL Database upon entry. That is all it does at the moment, but I get the error "Cannot open database "KA_LC_Database" requested by the login. The login failed. Login failed for user 'MEDESKTOP\Gary'."
Can anybody tell me why I am getting this problem ?
In Server Explorer my Server is called MeDesktop, in SQL Server Management Studio my Server is named MEDESKTOP and the connection is MEDESKTOP\Gary. It says it is "Ready" & Authentication Method is "Windows Authentication".
Also in VS2010, in Server Explorer, under Data Connections, there are 2 entries, KA_LC_Database.mdf & medesktop.master.dbo, both of these have a little red cross on them that changes to something white & black if I expand them.
I hope this information helps, here is ALL my code ...
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace KA_League_Cup
{
public partial class KA_LC_Form1 : Form
{
public KA_LC_Form1()
{
InitializeComponent();
}
private void KA_LC_Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Entering Form1 Code");
KA_LA_comboBox1.Items.Clear();
SqlConnection cs = new SqlConnection("SERVER=MeDesktop;DATABASE=KA_LC_Database;Trusted_Connection=True");
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM LEAGUES", cs);
DataTable dt = new DataTable();
da.Fill(dt); <<<--- I get the error here
for(int i=0; i < dt.Rows.Count; i++)
{
KA_LA_comboBox1.Items.Add(dt.Rows[i]["LeagueName"]);
}
}
}
}[/code]
Posted Jan 19, 2012, 1:30 AM
Hope this help you and let me know, if you need further information.
Gary HeathPosted Jan 19, 2012, 9:08 AM
"Data Source=MEDESKTOP;Initial Catalog=master;Integrated Security=True"
When I run the program I get an error message saying that the Table "Leagues" does not exist, which insinuates to me that the Connection is now being made, but to the wrong Database (I chose master). However, if I rerun it and try to point at my database in the Add Connection screen, my database (KA_LC_Database) is not there, the options are master, model, msdb, ReportServer, ReportServerTempDB & tempdb.
Does this mean that it is the way I have set up my Database ?
I set it up using Project / Add new Item / Data / Service Based Database which created an MDF database listed under Data Connections. I tried again using Local Database which created an SDF database, and these appear alongside medesktop.master.dbo (and dbo1 as created by following your instructions). Should my Database be within master, or type .dbo ?!?
Does that all make sense ?!?!?
EDIT ...
I've done it, woo hoo !!! I needed to Browse to where the database was held & as there are spaces in some of the Folder names I needed to preceed the info with an @ sign, as follows ...
[code]SqlConnection cs = new SqlConnection(@"Data Source=MEDESKTOP;AttachDbFilename=J:\Users\Gary\Documents\Visual Studio 2010\Projects\KALeagueCup\KALeagueCup\KADatabase.mdf;Initial Catalog=myKADB;Integrated Security=True");[/code]
Thank you very much for your help, I will undoubtedly talk to you again when I run into another problem !!!
Thanks again, much appreciated ...
Jignesh TrivediPosted Jan 19, 2012, 5:19 AM
Kindly check, your data base is connected through SQL Management Studio ?
Gary HeathPosted Jan 18, 2012, 11:25 AM
step1 ... Do you mean " create one more form in the windows application " or " create one more (Server) from the windows application " ?
step2 ... What do you mean ? Add SQLDataSource to what ?!? I guess that depends on what you mean in step1 !!!
Thanks in advance for your help, this is very frustrating !!!
Gary
Posted Jan 18, 2012, 10:49 AM
step 1: Just create one more form the in windows application.
step 2 : Add SQLDataSource and configure it.
step 3: During the configuration , it will ask you connect sql server, saving password, test connection etc..
step 4 : It will ask you save the configuration in app.config file. Please save it.
Now compare the connectionstring in app.config and yours , then you will come to know, where it is failing.
To know more about connectionstring , please refer : www.connectionstrings.com