C# Cannot modify an Access DataBase - c#

I'm trying to modify an specific register on Access by C# but when I run the code, It doesn't modify anything, I have tried the same structure on adding resgisters or deleting and it works well, Help please
using OleDB
dataGridView1.Rows.Clear();
string sql = "UPDATE Medicos SET Id_clinica=#Id_clinica, Id_especialidad=#Id_especialidad, Nombre=#Nombre, Apellidos=#Apellidos WHERE Id_medico=#Id_medico";
OleDbConnection con = new OleDbConnection(cs);
con.Open();
OleDbCommand cmd = new OleDbCommand(sql, con);
cmd.Parameters.AddWithValue("#Id_medico", textBox1.Text);
cmd.Parameters.AddWithValue("#Id_clinica", textBox2.Text);
cmd.Parameters.AddWithValue("#Id_especialidad", textBox3.Text);
cmd.Parameters.AddWithValue("#Nombre", textBox4.Text);
cmd.Parameters.AddWithValue("#Apellidos", textBox5.Text);
cmd.ExecuteNonQuery();
con.Close();

Related

Database not updating C#

I'm trying to save data from textboxes to a database, the new data appears in the grid, but it doesn't appear in the database table. When I close the application and restart it, the data which appeared in the grid disappears. There is no error, but no data is uploaded. It works if I hardcode the path but I wanted to use the DataDirectory so that it can be used on other computers. The database is set to Copy if newer (for the copy to output directory). Could someone help?
if (dialogResult == DialogResult.Yes)
{
SqlCommand cmd;
SqlConnection con;
con = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = |DataDirectory|\TestDatabase.mdf;Integrated Security=True");
con.Open();
cmd = new SqlCommand("INSERT INTO Patient (FirstName, LastName, DOB) VALUES (#FirstName,#LastName,#DOB)", con);
cmd.Parameters.AddWithValue("#FirstName", txtFirstName.Text);
cmd.Parameters.AddWithValue("#LastName", txtLastName.Text);
cmd.Parameters.AddWithValue("#DOB", dateTimePicker1.Text);
cmd.ExecuteNonQuery();
con.Close();
SqlDataAdapter SqlDA = new SqlDataAdapter("SELECT * FROM Patient", con);
DataTable dtbl = new DataTable();
SqlDA.Fill(dtbl);
dataGridView1.DataSource = dtbl;
}

No mapping exists from object type System.Windows.Forms.DateTimePicker

When I press the register button, a MessageBox will show up and then this happens:
No mapping exists from object type System.Windows.Forms.DateTimePicker to a known managed provider native type.
How do I fix this?
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = #"Data Source=MYCOMPUTER-PC\SQLEXPRESS;Database=COMPPROG;User
Id=user;Password=password";
SqlCommand scmd = new SqlCommand("SELECT COUNT (*) as cnt from USERACCOUNT", sqlcon);
SqlCommand cmd = sqlcon.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO USERACCOUNT
(FName,MName,Sname,TxtBirth,comboGender,textBox8,textBox1,textBox2)
VALUES(#FName,#MName,#Sname,#TxtBirth,#comboGender,#textBox8,#textBox1,#textBox2)";
cmd.Parameters.AddWithValue("#FName", FName.Text);
cmd.Parameters.AddWithValue("#MName", MName.Text);
cmd.Parameters.AddWithValue("#Sname", Sname.Text);
cmd.Parameters.AddWithValue("#TxtBirth", TxtBirth);
cmd.Parameters.AddWithValue("#textBox8", textBox8.Text);
cmd.Parameters.AddWithValue("#textBox1", textBox1.Text);
cmd.Parameters.AddWithValue("#textBox2", textBox2.Text);
cmd.Parameters.AddWithValue("#comboGender", comboGender);
sqlcon.Open();
MessageBox.Show("Record added sucessfully!", "Registration Success!");
cmd.ExecuteNonQuery();
sqlcon.Close();
Like #gunr2171 said in comments problem is from this line so change it like this :
cmd.Parameters.AddWithValue("#TxtBirth", TxtBirth.Value);
This is value of TxtBirth for your query.
Edit :
And your error means you are passing the DatePicker itself and it can not be done.

I have trouble inserting and deleting rows in my SQL Server database

This is my code for the insert, I got textboxes on my form and I write something inside but when pressing the button with executes the code below it shows an error by the (cmd.ExecuteNonQuery)
SqlConnection cn = new SqlConnection(global::dotasuka.Properties.Settings.Default.Database1ConnectionString);
try
{
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO Heroes (Heroname, Attacktype, patribute, role, role2, role3) VALUES (#Heroname, #Attacktype, #patribute, #role, #role2, #role3)";
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#Heroname", textBox1.Text);
cmd.Parameters.AddWithValue("#Attacktype", textBox2.Text);
cmd.Parameters.AddWithValue("#patribute", textBox3.Text);
cmd.Parameters.AddWithValue("#role", textBox4.Text);
cmd.Parameters.AddWithValue("#role2", textBox5.Text);
cmd.Parameters.AddWithValue("#role3", textBox6.Text);
cmd.ExecuteNonQuery();
textBox1.Clear(); textBox2.Clear();
textBox3.Clear(); textBox4.Clear();
textBox5.Clear(); textBox6.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cn.Close();
}
The second code snippet is my update code which shows same an error when trying to execute, same error by the execute non query
SqlDataReader reader = null;
SqlConnection cn = new SqlConnection(global::dotasuka.Properties.Settings.Default.Database1ConnectionString);
SqlCommand sda = new SqlCommand("SELECT * FROM Heroes ", cn);
cn.Open();
reader = sda.ExecuteReader();
while (reader.Read())
{
object Heroname = reader["heroname"];
listBox1.Items.Add(Heroname.ToString());
}
reader.Close();
cn.Close();
Please I need help and as quick as someone can, ty!
I think you receive this error, that because you using reserved word Role you should place It in brackets [].
cmd.CommandText = "INSERT INTO Heroes (Heroname, Attacktype,patribute,[role],role2,role3) VALUES (#Heroname, #Attacktype,#patribute,#role,#role2,#role3)";
I suggest to avoid that method of passing parameters, with
cmd.Parameters.AddWithValue("#Heroname", textBox1.Text);
you didn't specify datatype and length, that's correct ADO.NET will guess them but this is not happens always, instead of that try this method:
cmd.Parameters.Add("#Heroname", SqlDbType.VarChar, 50).Value = textBox1.Text;
for all of your parameters.
Was it a null reference exception?
Reading your code, I think you need to change SqlDataReader reader = null; to SqlDataReader reader = new SqlDataReader;
SqlConnection cn = new SqlConnection(global::dotasuka.Properties.Settings.Default.Database1ConnectionString);
try
{
SqlCommand cmd = new SqlCommand("INSERT INTO Heroes (heroname,attacktype,patribute,role,role2,role3) Values (#heroname,#attacktype,#patribute,#role,#role2,#role3) ", cn);
cn.Open();
cmd.Parameters.AddWithValue("#heroname", textBox1.Text);
cmd.Parameters.AddWithValue("#attacktype", textBox2.Text);
cmd.Parameters.AddWithValue("#patribute", textBox3.Text);
cmd.Parameters.AddWithValue("#role", textBox4.Text);
cmd.Parameters.AddWithValue("#role2", textBox5.Text);
cmd.Parameters.AddWithValue("#role3", textBox6.Text);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cn.Close();
}
This code worked now, inside my form i added a listbox and it does show the added row with its values! But closing form and checking inside database, it doesnt show that row? Whats wrong?

How to update an entire selected same column?

I want to update an entire selected same column with another value...
heres a code i have tried, apparently its not working (the error is no changes in DB)
OleDbConnection con = new OleDbConnection();
con.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Rock.accdb";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "UPDATE profile SET [year]=#1 WHERE [year]=#2";
cmd.Parameters.AddWithValue("#1", comboBox1.Text);
cmd.Parameters.AddWithValue("#2", comboBox2.Text);
cmd.ExecuteNonQuery();
con.Close();
label4.Text = "Updated successfully";
label4.ForeColor = Color.Green;
Please be respectful, if you guys have any doubts just comment...
try:
cmd.CommandText = "UPDATE profile SET [year]=? WHERE [year]=?"
and update parameters (the parameters must be added in order of the '?'s in the query)
cmd.Parameters.AddWithValue("?", comboBox1.Text);
cmd.Parameters.AddWithValue("?", comboBox2.Text);

Syntax error in INSERT INTO statement with parameters

i am getting this error so often, i can't find out what's wrong in here?
con.Open();
String query = "INSERT INTO user (username,[password]) values(a,b)";
OleDbCommand cmd1 = new OleDbCommand(query, con);
cmd1.Parameters.AddWithValue("a",textBox1.Text);
cmd1.Parameters.AddWithValue("b", textBox2.Text);
cmd1.ExecuteNonQuery();
con.Close();
MessageBox.Show("User Account Created");
this.Close();
SqlParameters should start with # like #Name. try this:
con.Open();
String query = "INSERT INTO user (Username, [Password]) values(#Username, #Password)";
OleDbCommand cmd = new OleDbCommand(query, con);
cmd.Parameters.AddWithValue("#Username",textBox1.Text);
cmd.Parameters.AddWithValue("#Password", textBox2.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("User Account Created");
this.Close();
Read Using Variables and Parameters

Categories

Resources