I am facing the following problem in my VS 2013
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Here is the code:
SqlConnection myConnection = new SqlConnection("Data Source =.\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\Database1.mdf; Integrated Security = True; User Instance = True;");
myConnection.Open();
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = "SELECT * FROM Customer";
SqlDataAdapter myDataAdapter = new SqlDataAdapter(myCommand);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet);
myConnection.Close();
foreach(DataRow myRow in myDataSet.Tables[0].Rows)
{
Console.WriteLine(myRow["FirstName"]+" " +myRow["LastName"]);
}
Console.ReadLine();
Related
I' using Visual Studio Enterprise 2015 WPF to do my Project and my database is a ms access file
I'm not sure why I'm having this error Can someone
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: No value given for one or more required parameters.
Here is my Code
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select `Name1`,`ID` from `Employee` WHERE `Name1` = Jacob ";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;
I have also tried
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select Name1,ID from [Employee] WHERE Name1 = Jacob ";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select * from [Employee] WHERE Name1 = Jacob ";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;
But still the same Error
My Connection String
<connectionStrings>
<add name="Connection" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\wpfdb.accdb;Persist Security Info=False;"/>
In your command text lines, for example:
> cmd.CommandText = "select `Name1`,`ID` from `Employee` WHERE `Name1` =
> Jacob ";
Jacob needs to be wrapped in quotes, not column and table names:
cmd.CommandText = "select * Employee WHERE Name1 = 'Jacob' ";
I have successfully built connection string and able to populate table data when the database is Access as:
DataTable results = new DataTable();
using (OleDbConnection thisConnection = new OleDbConnection(connectionname))
{
OleDbCommand cmd = new OleDbCommand("SELECT * from TABLE_A", thisConnection); //EDIT : change table name for Oracle
thisConnection.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(results);
}
I am new to Oracle though. Can somebody mention what changes to make in above code for Oracle database?
You can try this;
OracleConnection conn = new OracleConnection("Your Connection string");
//Open the connection to the database
conn.Open();
DataSet dataSet = new DataSet();
OracleCommand cmd = new OracleCommand("your select query");
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataSet);
}
Basically I am trying to develop a software and I am new in programming. I am trying to insert the data of textbox into SQL Server 2008 R2 Standard and I am getting an error:
System.NullReferenceException was unhandled
Here is my code.
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(local);Initial Catalog=songs_db;Persist Security Info=True;User ID=sa;Password=iloveyourb";
con.Open();
DataSet ds = new DataSet();
String sql = "Select * From tbl_songdb";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataRow drow = ds.Tables["tbl_songdb"].NewRow(); // I am getting error message here.
drow[1] = txt_songName.Text;
drow[2] = txt_minute.Text;
drow[3] = txt_albumnName.Text;
drow[4] = txt_location.Text;
ds.Tables["tbl_songdb"].Rows.Add(drow);
con.Close();
actually my dataset was empty, thats why it was showing NULL error
da.Fill(ds, "tbl_studentData");
i used these lines to fill it and now everything is working fine.
thanks to all for giving their time.
Just do exactly what error said to you. Handle it with try catch like that:
try{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(local);Initial Catalog=songs_db;Persist Security Info=True;User ID=sa;Password=iloveyourb";
con.Open();
DataSet ds = new DataSet();
String sql = "Select * From tbl_songdb";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataRow drow = ds.Tables["tbl_songdb"].NewRow(); // I am getting error message here.
drow[1] = txt_songName.Text;
drow[2] = txt_minute.Text;
drow[3] = txt_albumnName.Text;
drow[4] = txt_location.Text;
ds.Tables["tbl_songdb"].Rows.Add(drow);
da.Update(ds);
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(local);Initial Catalog=songs_db;Persist Security Info=True;User ID=sa;Password=iloveyourb";
con.Open();
DataSet ds = new DataSet();
String sql = "Select * From tbl_songdb";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds);
DataRow drow = ds.Tables[0].NewRow(); // I am getting error message here.
drow[1] = txt_songName.Text;
drow[2] = txt_minute.Text;
drow[3] = txt_albumnName.Text;
drow[4] = txt_location.Text;
ds.Tables[0].Rows.Add(drow);
SQLiteCommandBuilder cmdbuilder = new SQLiteCommandBuilder(da);
da.InsertCommand = cmdbuilder.GetInsertCommand();
da.Update(ds);
ds.AcceptChanges();
con.Close();
I am getting an error when I try to run my program which says -
An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll
Additional information: Initialiseringsstrengens format does not match the specification starting at index 0.
at using (conn = new SqlConnection(connstring))
the whole code for that -
public partial class MainWindow : Window
{
public SqlConnection conn;
public SqlCommand cmd;
string connstring = (#"Data Source=SINDALSQL\MSSQL14; Initial Catalog=OminiData; Integrated Security =True");
string sql = ("SELECT Mærke, Model, Årgang, [Motor Type], Krydsmål, Centerhul, Møtrik, Bolter, Dæk, Fælge FROM Hjuldata");
private void binddata()
{
DataSet1 ds = new DataSet1();
using (conn = new SqlConnection(connstring))
{
cmd = new SqlCommand(sql, conn);
SqlDataAdapter adp = new SqlDataAdapter();
conn.Open();
adp.SelectCommand = cmd;
adp.Fill(ds, "Hjuldata");
hjuldata.DataContext = ds;
}
}`
public partial class MainWindow : Window {
public SqlConnection conn;
public SqlCommand cmd;
string connstring = (#"Data Source=SINDALSQL\MSSQL14; Initial Catalog=OminiData; Integrated Security =True");
string sql= ("SELECT Mærke, Model, Årgang, [Motor Type], Krydsmål, Centerhul, Møtrik, Bolter, Dæk, Fælge FROM Hjuldata");
private void binddata() {
DataTable dt = new DataTable();
using (SqlDataAdapter adp = new SqlDataAdapter(sql,connstring)) {
adp.Fill(dt);
hjuldata.DataContext = dt;
}
}
}
I'm not sure whats wrong with my codes. When I run it, I get the error: SqlException was unhandled by user code. An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: Invalid object name 'ProductionPlanFabric'.
What does this mean?
Thank you :)
private void displayPendingFabric()
{
string strConn = ConfigurationManager.ConnectionStrings
["ZZFashionIMSConnectionString"].ToString();
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand("SELECT ProductionPlanID, FashionStyleID, FabricID, WarehouseID, PPStatus, PPFabricReqd, PPFabricIssued FROM ProductionPlanFabric WHERE PPStatus = 0 OR PPStatus = 2", conn);
SqlDataAdapter daPPFabric = new SqlDataAdapter(cmd);
DataSet result = new DataSet();
conn.Open();
daPPFabric.Fill(result, "ProductionPlanFabric");
DataView dvPPFabric = result.Tables["ProductionPlanFabric"].DefaultView;
conn.Close();
gvPPFabric.DataSource = dvPPFabric;
gvPPFabric.DataBind();
}
Try this code:
conn.Open();
daPPFabric.Fill(result);
DataView dvPPFabric = result.Tables[0].DefaultView;
conn.Close();