SQLException unhandled - c#

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();

Related

Data from Access DB wont load into textbox

I'm trying to load one string data from a local Access database, when I press a button, the data won't load and I'm getting the following error:
"An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Data type mismatch in criteria expression."
string strProvider = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\training\Documents\Visual Studio 2013\Projects\WindowsFormsApplication1\WindowsFormsApplication1\LucruCilindrii.accdb";
OleDbConnection con = new OleDbConnection(strProvider);
con.Open();
string passingValue = "SELECT NumarDeOreLucrate FROM Ore_lucru_Cilindrii WHERE DiametruSuperior = " + textBox4.Text;
OleDbCommand cmmd = new OleDbCommand(passingValue, con);
OleDbDataReader reader1 = cmmd.ExecuteReader();
while (reader1.Read())
{
textBox1.Text = (reader1["NumarDeOreLucrate"].ToString());
}
con.Close();

Data type mismatch in criteria expression. MS AccessDB (cmd:ExecuteNonQuery())

Trying to update data in AccessDB using following code.
OleDbConnection conn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Saumil\Projects\REV-OEM\LoginInforREVOEM.accdb");
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("Select * from ManualRunSettings", conn);
DataSet dss = new DataSet();
da.Fill(dss, "ManualRunSettings");
//Pump1
OleDbCommand cmd = new OleDbCommand("UPDATE ManualRunSettings SET [Pump1RunAccording] = #Pump1RunAccording WHERE [ID] = '1'", conn);
cmd.Parameters.AddWithValue("#Pump1RunAccording", domainUpDown1.Text);
da.UpdateCommand = cmd;
da.UpdateCommand.ExecuteNonQuery();
I am having following error,
An unhandled exception of type 'System.Data.OleDb.OleDbException'
occurred in System.Data.dll
Additional information: Data type mismatch in criteria expression.
Can anyone lead to where the problem might be?
Thanks in advance.

SQL exception was unhandled by user code on connection open

i am beginner to microsoft asp.net and i got An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code error when trying to select value from Microsoft visual studio database. The error was on the con.Open() line
Below is my code:
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\Ng\Documents\Visual Studio 2015\Projects\Assignment6\Assignment6\App_Data\photoCompetition.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM User WHERE email=#username and password=#word", con);
cmd.Parameters.AddWithValue("#username", emailtext.Text);
cmd.Parameters.AddWithValue("#word", passwordtext.Text);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
int i = cmd.ExecuteNonQuery();
con.Close();
if (dt.Rows.Count > 0)
{
Response.Redirect("Default.aspx");
}
else
{
lblMsg.Text = "Your username and word is incorrect";
lblMsg.ForeColor = System.Drawing.Color.Red;
}
}
So your first issue is that you had a space in your Sql Connection string.
C: \Users\Ng\Documents\Visual Studio 2015\Projects\Assignment6\Assignment6\App_Data\photoCompetit‌​ion.mdf
Now since your query looks valid and assuming the tables and columns exists, you are having a problem with the SQL query you are trying to execute. I see two options here.
Enclose your parameters with a single quote to denote it as a string
SqlCommand cmd = new SqlCommand("SELECT * FROM User WHERE email='#username' and password='#word'", con);
Use SqlParameterCollection.Add Method (String, SqlDbType, Int32).
cmd.Parameters.Add("#username", SqlDbType.Text).Value = emailtext.Text;
cmd.Parameters.Add("#word", SqlDbType.Text).Value = passwordtext.Text;
Also, don't forget to close your SqlConnection with con.Close();

Regarding error An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

I am beginner with c#, when i execute my code it shows an error "An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code".
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL
This is my Code :
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
showdata();
}
private void showdata()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("server=demo; database=demo; Integrated Security=True;");
SqlCommand cmd = new SqlCommand("select * from demo, con");
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
dt.Load(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
}
Your cmd command is not correct.
change
SqlCommand cmd = new SqlCommand("select * from demo, con");
To
SqlCommand cmd = new SqlCommand("select * from demo", con);
Thank you for your help i appreciate the same. I got my answer from a friend, there was an # missing in my query.
SqlConnection con = new SqlConnection(#"server=demo; database=demo; Integrated Security=True;");
After updating it i was able to see my data on browser.
But once again thank you for your help everyone.

C# .mdf connection failed

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();

Categories

Resources