I am trying to get all the details of a User in an Access database. But i cant seem to save each columns value to a label. Here is the code i am using.
Also UserId has a value assigned to it already
string connString = (#"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=DataDirectory|HorseDB.mdb");
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = #"SELECT * FROM [Users] WHERE [UserId] = #UserId ";
cmd.Parameters.AddWithValue("#UserId", UserId);
OleDbDataReader dbReader = cmd.ExecuteReader();
while (dbReader.Read())
{
accountUserIdLabel.Text = dbReader.GetValue(0).ToString();
//Will add other labels once this works
}
dbReader.Close();
conn.Close();
Related
MySqlConnection con = new MySqlConnection(connectionstr);
con.Open();
string insertid = "SELECT UserID FROM login WHERE username =#U";
MySqlCommand cmd2 = new MySqlCommand(insertid, con);
cmd2.Parameters.AddWithValue("#U", username);
MySqlDataReader idread = cmd2.ExecuteReader();
idread.Read();
string id;
if (idread.HasRows==true)
{
id = idread.GetString(0);
string insertid2 = "INSERT INTO managementtest.fulldetails(UserID) VALUES(#I);";
MySqlCommand cmd3 = new MySqlCommand(insertid2, con);
cmd3.Parameters.AddWithValue("#I", id);
MessageBox.Show("Detail Profile Created. User ID # " + id , "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
I have tried running this code. Everything runs fine but the value of id is not added into MySQL Database. I have checked the spelling of table name and all. It isn't working. string username is predefined and holds a specific value from a textbox.
you forgot this
cmd3.CommandType = CommandType.Text;
cmd3.ExecuteNonQuery();
I failed to get the correct result with this code in Form2:
conn.Open();
OleDbCommand cmd = new OleDbCommand("Select * From udbTable Where Username Like '" + f1.textBox1.Text + "%'", conn);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
label5.Text = reader["Username"].ToString();
}
conn.Close();
I have 3 samples data in the table, but i'm always getting the same result which is the first entry of the database. Whenever i input the last entry or second entry in the textbox1.Text, i still getting the first entry.
textbox1.Text is from Form1, and i set it's property Modification to Public.
label5.text is the output.
try this fix
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection=conn;
command.CommandText = "Select * From udbTable Where Username Like ?";
cmd.Parameters.Add("#Username",OleDbType.VarChar);
cmd.Parameters["#Username"].Value=f1.textBox1.Text;
OleDbDataReader reader = cmd.ExecuteReader();
I am new on C# . I create a C# Console program and insert some data to MySql by following code .
string connection = "Server=localhost;Database=user_table;Uid=root;Pwd=";
MySqlConnection dbcon = new MySqlConnection(connection);
MySqlCommand cmd;
dbcon.Open();
cmd = dbcon.CreateCommand();
cmd.CommandText = "INSERt INTO user_table(user_name,amount) VALUES(#user_name,#amount)";
cmd.Parameters.AddWithValue("#user_name","Niloy");
cmd.Parameters.AddWithValue("#amount", "456");
cmd.ExecuteNonQuery();
Now I want to retrieve this data and display in console application .
like This
Niloy 234
Joy 500
Minal 230
how can i do this ?
You have to do the opposite of that you have already done for the insertion of data.
// You sql command
MySqlCommand selectData;
// Create the sql command
selectData = dbcon.CreateCommand();
// Declare the sript of sql command
selectData.CommandText = "SELECT user_name, amount, FROM user_table";
// Declare a reader, through which we will read the data.
MySqlDataReader rdr = selectData.ExecuteReader();
// Read the data
while(rdr.Read())
{
string userName = (string)rdr["user_name"];
string amount = (string)rdr["amount"];
// Print the data.
Console.WriteLine(username+" "+amount);
}
rdr.Close();
using (dbcon)
{
dbcon.Open();
cmd = dbcon.CreateCommand();
cmd.CommandText = "Select user_name,amount from user_table";
MySqlReader sqlreader = cmd.ExecuteReader();
while (sqlreader.Read())
{
Console.WriteLine(sqlreader[0].ToString()+ " "+(sqlreader[1].ToString());
}
sqlreader.Close();
}
You can use gridview to display your data, Using the similar way you used to insert data into your Table.
string connection = "Server=localhost;Database=user_table;Uid=root;Pwd=";
MySqlConnection dbcon = new MySqlConnection(connection);
DataTable dt = new DataTable();
MySqlCommand cmd;
dbcon.Open();
cmd = dbcon.CreateCommand();
cmd.CommandText = "SELECT * from user_table";
adapter = new MySqlDataAdapter(cmd);
adapter.Fill(dt);
Gridview1.DataSource=dt;
Gridview1.DataBind();
I have a database in which I created a table HUGO_BOSS. Its columns are [brand name], [stock quantity], [retail price] and its primery key is [Brand name]. I want to fill my textbox in windows form with the value of stock quantity present in my database. I tried the following code but it gives run-time error
The Connection was not close, the connection state is open.
if (comboBox2.Text == "HUGO BOSS")
{
try
{
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "Select [Stock quantity] as stockquantity from HUGO_BOSS WHERE [Brand name]=#name";
cmd.Parameters.AddWithValue("#name", comboBox3.SelectedItem);
con.Open();
OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleResult);
if (dr.Read())
{
textBox7.Text = dr["stockquantity"].ToString();
}
}
finally { con.Close(); }
}
One more thing, here I will select the primary key by using a combobox3
It looks you're trying to reuse a database connection that is already open.
You could try testing the connection state before trying to open it:
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "Select [Stock quantity] as stockquantity from HUGO_BOSS WHERE [Brand name]=#name";
cmd.Parameters.AddWithValue("#name", comboBox3.SelectedItem);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleResult);
if (dr.Read())
{
textBox7.Text = dr["stockquantity"].ToString();
}
Alternatively, you could create a new connection each time you need to execute a new command.
I wrote some code that takes some values from one table and inserts the other table with these values.(not just these values, but also these values(this values=values from the based on table))
and I get this error:
System.Data.OleDb.OleDbException (0x80040E10): value wan't given for one or more of the required parameters.`
here's the code. I don't know what i've missed.
string selectedItem = comboBox1.SelectedItem.ToString();
Codons cdn = new Codons(selectedItem);
string codon1;
int index;
if (this.i != this.counter)
{
//take from the DataBase the matching codonsCodon1 to codonsFullName
codon1 = cdn.GetCodon1();
//take the serialnumber of the last protein
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
string last= "SELECT proInfoSerialNum FROM tblProInfo WHERE proInfoScienceName = "+this.name ;
OleDbCommand getSerial = new OleDbCommand(last, conn);
OleDbDataReader dr = getSerial.ExecuteReader();
dr.Read();
index = dr.GetInt32(0);
//add the amino acid to tblOrderAA
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
string insertCommand = "INSERT INTO tblOrderAA(orderAASerialPro, orderAACodon1) "
+ " values (?, ?)";
using (OleDbCommand command = new OleDbCommand(insertCommand, connection))
{
connection.Open();
command.Parameters.AddWithValue("orderAASerialPro", index);
command.Parameters.AddWithValue("orderAACodon1", codon1);
command.ExecuteNonQuery();
}
}
}
EDIT:I put a messagebox after that line:
index = dr.GetInt32(0);
to see where is the problem, and I get the error before that. I don't see the messagebox
Your SELECT Command has a syntax error in it because you didn't enclose it with quotes.
Change this:
string last = "SELECT proInfoSerialNum FROM tblProInfo WHERE proInfoScienceName = "+this.name ;
OleDbCommand getSerial = new OleDbCommand(last, conn);
OleDbDataReader dr = getSerial.ExecuteReader();
to
string last = "SELECT proInfoSerialNum FROM tblProInfo WHERE proInfoScienceName = ?";
OleDbCommand getSerial = new OleDbCommand(last, conn);
getSerial.Parameters.AddWithValue("?", this.name);
OleDbDataReader dr = getSerial.ExecuteReader();
This code is example from here:
string SqlString = "Insert Into Contacts (FirstName, LastName) Values (?,?)";
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("FirstName", txtFirstName.Text);
cmd.Parameters.AddWithValue("LastName", txtLastName.Text);
conn.Open();
cmd.ExecuteNonQuery();
}
}
Try to do the same as in the example.