Retrieving From WPF AccessDatabase formatting - c#

I trying to format the data that I'm extracting from the Access Database to look something like the format below from the database.
ID Name (Age)
Company Name
Handphone Number
NRIC
Is it possible to do this in a gridview or inside a rich textbox in Visual Studio WPF?
Currently my Code is:
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select ID,Name,Age,Company,Handphone,NRIC FROM Customers WHERE Name = #txtSearch";
cmd.Parameters.AddWithValue("#txtSearch", txtSearch.Text);
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
grid1.ItemsSource = rd;

Related

System.Data.OleDb.OleDbException Require one or more parameters

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' ";

Storing multiple values from an SQL select statement

I have an SQL select query that will return multiple values, but I cannot find a way to store/access them. I am using Visual Studio 2015 and an Access database.
Below is my most recent attempt using a data table/grid view.
string now = DateTime.Today.ToString("dd/MM/yyyy");
//Establish and open new database connection.
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase32BITConnectionString"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmd.CommandText = String.Format ("select Rating from [RATINGS] where Today_Date like #now and Name like #name");
cmd.Parameters.AddWithValue("#now", now);
cmd.Parameters.AddWithValue("#name", name);
cmd.Connection = con;
adapter.SelectCommand = cmd;
adapter.Fill(dt);
con.Close();
testGridView.DataSource = dt;
name is a variable passed into the method. I don't necessarily need it stored in a data table, i just need to be able to access the results individually to average them (array?).
Any advice would be much appreciated

Auto Completing Textbox

I have a textbox in my winform. I want to autocomplete it with mobile number from a customer table. I have written the code but its not auto completing.
string CS = "data source=.; database=BillingSoftware; user id=sa; Password=9495640";
SqlConnection con = new SqlConnection(CS);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Phone FROM Customers", con);
SqlDataReader rdr = cmd.ExecuteReader();
AutoCompleteStringCollection CustomerPhone = new AutoCompleteStringCollection();
while (rdr.Read())
{
CustomerPhone.Add(rdr.GetString(0));
}
txt_customerPOS.AutoCompleteCustomSource = CustomerPhone;
con.Close();
Check rdr.Read() whether you have data are not?
or call "Phone" column name
or call rdr["Phone"].ToString()

Received values from my SQL query are displayed incorrect in my Listbox

I'm trying to get values out of my database into my Listbox, I currently send all my results into a new object called Results
I want my listbox to show something like this:
Title(1)(enter)
Url(1)(enter)
Title(2)(enter)
Url(2)(enter)
and so on
It currently still gives an error at OleDbDataReader reader = command.ExecuteReader(); but I have no idea why.
This is the exact code
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\martijn\Dropbox\Proftaak Periode 2 Identity\Database11.accdb;
Persist Security Info=False;";
connection.Open();
OleDbCommand cmd1 = new OleDbCommand();
cmd1.Connection = connection;
cmd1.CommandText = "SELECT ZoekcriteriaID from Zoekcriteria WHERE ZoekCriteria = '" + Convert.ToString(lbzoektermen.SelectedItem) + "';";
OleDbDataReader reader1 = cmd1.ExecuteReader();
if(reader1.Read())
{
resultaatid = Convert.ToInt32(reader1["ZoekcriteriaID"]);
}
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT Titel, Webadress from Resultaat WHERE ZoekriteriaID = '"+ resultaatid +"';";
OleDbDataReader reader = command.ExecuteReader();
lbresultaten.Items.Clear();
List<Results> resultaten = new List<Results>();
while(reader.Read())
{
Results result = new Results();
result.url = Convert.ToString(reader["Webadress"]);
result.titel = Convert.ToString(reader["Webadress"]);
resultaten.Add(result);
}
foreach(Results result in resultaten )
{
lbresultaten.Items.Add(result.titel);
lbresultaten.Items.Add(result.url);
}
I hope someone could help me,
Kind Regards,
Martijn
Your problem probably lays in your where clause:
SELECT ZoekcriteriaID from Zoekcriteria WHERE **ZoekCriteria**
It should be a column name, not a table name.

display individual columns from sql result in asp.net c#

I'm coding a simple application using c# asp.net. I'm getting the averages of columns. How can I get the individual values from the result and display it in a new label (label1, label2, label3....)?
I tried ExecuteScalar().ToString(); but it returns only the first column.
Below is my code:
SqlConnection con;
con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\STATDB.MDF;Integrated Security=True;User Instance=True");
SqlCommand com = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
string result = "SELECT AVG(p_tan) AS p_tang, AVG(e_tan) AS e_tang, AVG(p_rel) AS p_reli FROM statistics";
SqlCommand showresult = new SqlCommand(result, con);
con.Open();
Label1.Text = showresult.ExecuteScalar().ToString();
//Label2.Text = p_tang
//Label3.Text = e_tang
//Label4.Text = p_reli
con.Close();
Any help will be appreciated.
Use showresult.ExecuteReader() and then iterate over the row to get the values
SqlDataReader reader=showresult.ExecuteReader();
while (reader.Read())
{
Label1.Text= reader["p_tang"].ToString().Trim();
Label2.Text= reader["e_tang"].ToString().Trim();
Label3.Text= reader["p_reli"].ToString().Trim();
}
ExecuteScalar will only pull one value. You will need do either use a DataReader or DataAdapter to get multiple values from the database.

Categories

Resources