Connecting Database using c# in Visual Studio 2013 [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a problem connecting my database (created in SQL Server 2008 R2 Express) with c# in vs 2013
Here is the code I wrote
string connStr = ConfigurationManager.ConnectionStrings["newSchool"].ToString();
SqlConnection conn = new SqlConnection(connStr);
the error is NullReferenceException

Change this:
string connStr = ConfigurationManager.ConnectionStrings["newSchool"].ToString();
to...
string connStr = ConfigurationManager.ConnectionStrings["newSchool"].ConnectionString;
Cheers -

Where did you define your connection string? In the app.config? Please send us the place where you configure this.
See http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx for help.
See http://www.connectionstrings.com/ how to configure a connection string for .net

I assume that there is no connection string named newSchool. It is probably NewSchool or some other casing.
In response to your comment: You need to provide a proper connection string. The ConfigurationManager is used to get a value from the configuration. The NullReferenceException shows you that the setting you request is not there.
You need to create a connection string first using the settings manager and then you can access it.

Related

Add Application Name / Program Name in mysql connection string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am searching for a solution to add application name or program name in connection string so that it is visible under "Client Connection" in "MySQL Workbench".
SQL Server : MySql Server 5.6 |
.Net DLL Version : 8.0.11.0 (download from https://dev.mysql.com/downloads/connector/net/8.0.html)
Here is my connection string
private static string myConnectionString = string.Format("server=192.168.2.2;uid={0};pwd={1};database=databse;SslMode
= none;Application Name=My Application;", Username, Password);
The "Program Name" column in MySQL Workbench comes from a program_name connection attribute. The MySQL documentation incorrectly claims that:
MySQL Connector/NET defines these attributes:
_program_name: The client name
This is wrong in two ways: the attribute name has a typo (leading underscore) and the code that sets it was deleted.
There is no way (a connection string setting or otherwise) to set the value of this attribute in MySQL Connector/NET. Furthermore, the connection attributes are part of the initial handshake so there is no way to set them after the connection is established (e.g., in your application code).
If you're willing to change ADO.NET connector libraries, the MySqlConnector library added support for an Application Name connection string option in v0.44.0; this will let you control the connection attribute that's sent to the server (and it will show up in MySQL Workbench).

Hpload an image into SQL Server database using c# and asp.net [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Before I ask the question I just want to say that I have gone through a lot of other similar questions like this and they didn't help that's why i decided to ask mine.
Ok, so I'm trying to upload a selected image to the database when an Upload button is clicked, the front-end was written in ASP.NET and the back-end in C#.
This is my code:
if (FileUpload1.HasFile)
{
SqlConnection con = new SqlConnection("i put my connection string here");
con.Open();
int length = FileUpload1.PostedFile.ContentLength;
byte[] pic = new byte[length];
FileUpload1.PostedFile.InputStream.Read(pic, 0, length);
SqlCommand com = new SqlCommand("insert into LogHelp (file) values (#image)", con);
com.Parameters.AddWithValue("#image", pic);
com.ExecuteNonQuery();
con.close();
}
Now I'm using Visual Studio 2015 web form. When I run the code above, it throws this error:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: Incorrect syntax near the keyword 'file'.
Please what exactly does this mean? And if you have another way I can use to accomplish the same task please I'm all ears.
I've since tested the hypothesis I put forward in my comment:
You need to surround your file with square brackets - FILE is a keyword in SQLServer
insert into LogHelp([file]) values(#image)
I disagree with the other commenter; I'm of the opinion that no database system I've used needs to have a space between the end of a table name and the opening bracket listing the columns to insert into. I do, however concur with his observation that there is no need to write the SQL as two strings that are concatenated ("INSERT..."+" values...") in your code though
Consider NOT getting into the AddWithValue habit, for reasons set out here: https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/

Gives Exception (throws) when tries to insert data to SQLite [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
When I tries to insert data to sqlite in window store app it gives unexpected exception in SQLite.cs file. My code looks like:-
SQLiteConnection con = new SQLiteConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "Fav.db"));
favorities addData = new favorities();
addData.Name = Title.Text;
addData.desciption = desciption.Text;
con.Insert(addData);`
and when I click on button on which these operation are being performed run time exception arises which looks like:-
in fact this SQLite.cs file is an API which I downloaded from "Manage Nuget Package" please help me out.
Thanks & Regards,
The table doesn't exist in the database.
Use this after you create the extension:
con.CreateTable<favorities>();
The creation of the connection would be better in a using statement too.

Getting error in visual studio while reading from mysql database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
In the following code I try to fill the combobox
while (myReader.Read())
{
string sName = myReader.GetString("profesor"); <--- here is the error
cb_1najprof.Items.Add(sName);
cb_2najprof.Items.Add(sName);
cb_3najprof.Items.Add(sName);
cb_1najsprof.Items.Add(sName);
cb_2najsprof.Items.Add(sName);
cb_3najsprof.Items.Add(sName);
}
When the code above is run I get the error cannot convert string to int on the indicated line.
You can try this..
string sName = myReader.GetString(myReader.GetOrdinal("profesor"));
or
string sName = myReader["profesor"].ToString();
Also see related question
How to get data by SqlDataReader.GetValue by column name
GetString takes an integer as an argument, not a string. You have called it with "profesor". Try calling it with eg 1 instead. This integer corresponds to the column you want to read from.
That's because you can't pass a string to the .GetString() method [documentation] You have to pass an int. The int (zero-based) is the column number you want to read.
Yes I know my documentation link is for a SqlDataReader, but the same concept applies for a MySqlDataReader

Test sql connect without using System.Data.SqlClient [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to test sql connection without using "System.Data.SqlClient"?
I want to check if the input ip, databasename, username and password of database is correct. I always used SqlConnection like this:
SqlConnection myConnection = new SqlConnection("network address=192.168.1.120; password=userpassword; user id=username; database=myDB");
But now I don't want to test sql server connection with this code.
You can use telnet to check the connectivity:
telnet yourhostorip 1433
And you can use sqlcmd to test both the authentication and the connectivity:
sqlcmd -S yourhostorip -U username -P password
Example how you can use it in C#:
var sqlProcess = new ProcessStartInfo();
sqlProcess.FileName = "SQLCMD.EXE";
sqlProcess.Arguments = String.Format("-S {0} -U {1} -P {2}",
server,
user,
password);
Process.Start(sqlProcess);
You can always use others
using System.Data.Odbc;
using System.Data.OleDb;

Categories