C
C#5mo 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
Keswiik5mo ago
because Connect is a ConnectionStringSettings object. You even correctly print the ConnectionString in your second-to-last code block
Vegas
VegasOP5mo ago
I just changed to mycon and still getting it
Vegas
VegasOP5mo ago
No description
Keswiik
Keswiik5mo ago
please look at your middle code block it is not the same as what you just showed
Motley
Motley5mo ago
You want var mycon = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
Vegas
VegasOP5mo ago
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Motley
Motley5mo 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
VegasOP5mo 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
VegasOP5mo ago
No description
Motley
Motley5mo 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
VegasOP5mo ago
No description
Motley
Motley5mo ago
Comment out that line, rerun it, and it should output stuff to the console.
Vegas
VegasOP5mo ago
No description
Vegas
VegasOP5mo ago
sea saw
Motley
Motley5mo ago
You are coding in Visual Studio?
Vegas
VegasOP5mo ago
yes
Motley
Motley5mo ago
Or Visual Studio Code?
Vegas
VegasOP5mo ago
2022 studio
Motley
Motley5mo ago
There should be a window at the bottom that has text in it MIght have an "Output" tab
Motley
Motley5mo ago
No description
Motley
Motley5mo ago
Or might look like this when you are running at the bottom right:
No description
Motley
Motley5mo 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
VegasOP5mo ago
yes, im having to rewrite the callout to xml, Im new at C# Sorry
Motley
Motley5mo ago
No problem, we all started there once.
Vegas
VegasOP5mo ago
So iv tried old school way and this is my result
No description
Motley
Motley5mo ago
You don't have those settings in your app.config file?
Vegas
VegasOP5mo ago
i cant get it to pull from there
Motley
Motley5mo 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
Motley5mo ago
In your Solution Explorer, right click on your project name. In the menu that appears, select "Add new item...":
No description
Motley
Motley5mo ago
If the compact view appears like this:
No description
Motley
Motley5mo ago
Click the Show All Templates button.
Motley
Motley5mo ago
Then you should get a dialog like this, and select "Application Configuration File":
No description
Motley
Motley5mo 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
Motley5mo ago
Like this:
No description
Motley
Motley5mo ago
And if you open that file, it should look like the familiar:
No description
Motley
Motley5mo ago
No description
Motley
Motley5mo ago
This is the App.config that I added your stuff to:
No description
Motley
Motley5mo ago
Hope that helps I had a pre-existing project called "RedboxDressed" that I just shoved that into.
Vegas
VegasOP5mo ago
Let me give it a shot @Motley That worked TY SO Very Much
Motley
Motley5mo ago
Glad to help!
Want results from more Discord servers?
Add your server