C
C#2w ago
Vegas

Connecting to database via String in XML

So i have done searching after searching and i keep getting stuck on having the connection error as a string.
<connectionStrings>
<clear />
<add name="Test"
providerName="localhost"
connectionString="Data Source=DESKTOP-6L5A1E6;Integrated Security=True;Connect Timeout=30;Encrypt=True;Trust Server Certificate=True;Application Intent=ReadWrite;Multi Subnet Failover=False" />
</connectionStrings>
<connectionStrings>
<clear />
<add name="Test"
providerName="localhost"
connectionString="Data Source=DESKTOP-6L5A1E6;Integrated Security=True;Connect Timeout=30;Encrypt=True;Trust Server Certificate=True;Application Intent=ReadWrite;Multi Subnet Failover=False" />
</connectionStrings>
private SqlConnection cont;

static class Program
{
static void Main()
{
GetConnectionStrings();
Console.ReadLine();
}

static void GetConnectionStrings()
{
ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;

foreach (ConnectionStringSettings cs in settings)
{
Console.WriteLine(cs.Name);
Console.WriteLine(cs.ProviderName);
Console.WriteLine(cs.ConnectionString);
}
}
}
private SqlConnection cont;

static class Program
{
static void Main()
{
GetConnectionStrings();
Console.ReadLine();
}

static void GetConnectionStrings()
{
ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;

foreach (ConnectionStringSettings cs in settings)
{
Console.WriteLine(cs.Name);
Console.WriteLine(cs.ProviderName);
Console.WriteLine(cs.ConnectionString);
}
}
}
private void button1_Click(object sender, EventArgs e)
{

var Connect = ConfigurationManager.ConnectionStrings["Test"];
var qury = "SELECT * FROM users WHERE Username = @username and password=@password";
string returnValue = "";
using (SqlConnection conn = new SqlConnection(**Connect**))
{
using (SqlCommand sqlcmd = new SqlCommand(qury, conn))
{
sqlcmd.Parameters.Add("@username", SqlDbType.VarChar).Value = textBox1.Text;
sqlcmd.Parameters.Add("@password", SqlDbType.VarChar).Value = textBox2.Text;
conn.Open();
returnValue = (string)sqlcmd.ExecuteScalar();
}
}

}
private void button1_Click(object sender, EventArgs e)
{

var Connect = ConfigurationManager.ConnectionStrings["Test"];
var qury = "SELECT * FROM users WHERE Username = @username and password=@password";
string returnValue = "";
using (SqlConnection conn = new SqlConnection(**Connect**))
{
using (SqlCommand sqlcmd = new SqlCommand(qury, conn))
{
sqlcmd.Parameters.Add("@username", SqlDbType.VarChar).Value = textBox1.Text;
sqlcmd.Parameters.Add("@password", SqlDbType.VarChar).Value = textBox2.Text;
conn.Open();
returnValue = (string)sqlcmd.ExecuteScalar();
}
}

}
Creating the new Connection is throwing error cannot convert from 'System.Configuration.ConnectionStringSettings' to 'string' Thanks
40 Replies
Keswiik
Keswiik2w ago
because Connect is a ConnectionStringSettings object. You even correctly print the ConnectionString in your second-to-last code block
Vegas
Vegas2w ago
I just changed to mycon and still getting it
Vegas
Vegas2w ago
No description
Keswiik
Keswiik2w ago
please look at your middle code block it is not the same as what you just showed
Motley
Motley2w ago
You want var mycon = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
Vegas
Vegas2w ago
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Motley
Motley2w ago
And your middle code block works? Set a breakpoint on the line and see what it is returning. It sounds like it isn't finding the ConnectionString named "Test".
Vegas
Vegas2w ago
i tried to get a msg block working to see if it will show my connectionstring when i seen your post let me back track
Vegas
Vegas2w ago
No description
Motley
Motley2w ago
Ok, so it can't find it. What does GetConnectionStrings() output. And for the record, the providerName should be likely be System.Data.SqlClient not localhost. Not that it's breaking anything for you. It's there to support the connection factory which you aren't using, so you can safely ignore it.
Vegas
Vegas2w ago
No description
Motley
Motley2w ago
Comment out that line, rerun it, and it should output stuff to the console.
Vegas
Vegas2w ago
No description
Vegas
Vegas2w ago
sea saw
Motley
Motley2w ago
You are coding in Visual Studio?
Vegas
Vegas2w ago
yes
Motley
Motley2w ago
Or Visual Studio Code?
Vegas
Vegas2w ago
2022 studio
Motley
Motley2w ago
There should be a window at the bottom that has text in it MIght have an "Output" tab
Motley
Motley2w ago
No description
Motley
Motley2w ago
Or might look like this when you are running at the bottom right:
No description
Motley
Motley2w ago
If you can't find the output window... That's fine. Put the cursor on the line that reads "foreach (ConnectionStringSettings cs in settings)" and hit the F9 key. It should put a red dot on that line on the left hand side. Hit the F5 key and it will run your application until it gets there. Then you can hover your mouse over the word settings and you can see what is in there. Hit the F10 key and it should go to the next line. If it doesn't, that means settings is empty and you have a problem with your config file.
Vegas
Vegas2w ago
yes, im having to rewrite the callout to xml, Im new at C# Sorry
Motley
Motley2w ago
No problem, we all started there once.
Vegas
Vegas2w ago
So iv tried old school way and this is my result
No description
Motley
Motley2w ago
You don't have those settings in your app.config file?
Vegas
Vegas2w ago
i cant get it to pull from there
Motley
Motley2w ago
Is this a .NET Framework application or a .NET Core application? When you compile the code, it should compile into the bin folder somewhere. Is there a Store My Data.exe in there? And is there a Store My Data.exe.config in the same folder?
Motley
Motley2w ago
In your Solution Explorer, right click on your project name. In the menu that appears, select "Add new item...":
No description
Motley
Motley2w ago
If the compact view appears like this:
No description
Motley
Motley2w ago
Click the Show All Templates button.
Motley
Motley2w ago
Then you should get a dialog like this, and select "Application Configuration File":
No description
Motley
Motley2w ago
Leave the default "App.config" alone at the bottom, and click add Then you should have an App.config file in your application and that is where ConfigurationManager expects it to be.
Motley
Motley2w ago
Like this:
No description
Motley
Motley2w ago
And if you open that file, it should look like the familiar:
No description
Motley
Motley2w ago
No description
Motley
Motley2w ago
This is the App.config that I added your stuff to:
No description
Motley
Motley2w ago
Hope that helps I had a pre-existing project called "RedboxDressed" that I just shoved that into.
Vegas
Vegas2w ago
Let me give it a shot @Motley That worked TY SO Very Much
Motley
Motley2w ago
Glad to help!
Want results from more Discord servers?
Add your server
More Posts