I used following code for deleting record from my SQL server database but this query deleted all my records. I want to delete just the selected row, not all of them.
SqlCommand cmd = new SqlCommand("DELETE FROM tablename WHERE id=id ", con);
cmd.ExecuteNonQuery();
You should transfer param id, my friend. Refer the code below:
var Id = ""; //set the value that you want to delete
SqlCommand cmd = new SqlCommand("DELETE FROM tablename WHERE id=#Id ", con);
command.Parameters.AddWithValue("#Id", Id);
cmd.ExecuteNonQuery();
Related
I'm trying to enter the id, first and last name into a table and then according to a combo box input I create another record right after that saves id of the student and the id of the Team which was chosen bu the combo box. Here is my code. everything runs well the only problem is that after that the record in the TeamPlayers table is not added. Please anyone ?!?
try
{
string team = null;
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=;Persist Security Info=False;"))
{
OleDbCommand comm = new OleDbCommand("INSERT INTO Students(BaruchID, FirstName, LastName) VALUES(#id, #first, #last)", conn);
conn.Open();
comm.Parameters.AddWithValue("#id", tbBaruchID.Text);
comm.Parameters.AddWithValue("#id", FirstName.Text);
comm.Parameters.AddWithValue("#id", LastName.Text);
comm.ExecuteNonQuery();
conn.Close();
}
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Junglists/Documents/Visual Studio 2013/Projects/SACC_Baruch/SACC_Baruch/Teams.accdb;Persist Security Info=False;"))
{
OleDbCommand comm = new OleDbCommand("SELECT TeamNum FROM Teams WHERE TeamName='" + cbTeam.Text +"'", conn);
conn.Open();
OleDbDataReader dr = comm.ExecuteReader(CommandBehavior.SingleResult);
if (dr.Read())
{
team = dr["TeamNum"].ToString();
}
conn.Close();
}
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Junglists/Documents/Visual Studio 2013/Projects/SACC_Baruch/SACC_Baruch/Teams.accdb;Persist Security Info=False;"))
{
OleDbCommand comm = new OleDbCommand("INSERT INTO TeamPlayers(ID, BaruchID, TeamID) VALUES(#i, #id, #teamid)", conn);
conn.Open();
comm.Parameters.AddWithValue("#i", 1);
comm.Parameters.AddWithValue("#id", tbBaruchID.Text);
comm.Parameters.AddWithValue("#teamid", int.Parse(team));
conn.Close();
}
MessageBox.Show("Student Added for team"+ cbTeam.Text);
}
The parameter names in the first INSERT statement are not used when adding the parameter values. All the comm.Parameters.AddWithValue("#id", .....); lines use #id. Therefore, the actual id value is never saved to table student.
The second INSERT has always uses 1 as the value of 'id'. Assuming that 'id' is a primary key, it can only contain unique values. Make the 'id' field an IDENTITY field and then remove it from the INSERT statement. Each time a record is then added to the table it will be given the next number in an ever incrementing sequence.
Corrected Code: http://dotnetfiddle.net/Dr9842
Hello I'm trying to SELECT multiple rows from table and INSERT them into another I thought that it can be done as following:
This part should select multiple rows:
string sqcom = "SELECT text,castka,rocnik FROM zajsluz WHERE akce='"+tentoradek+"' and rocnik='"+klientClass.Rocnik()+"'";
SqlCommand sc = new SqlCommand(sqcom,spojeni);
spojeni.Open();
sc.ExecuteNonQuery();
spojeni.Close();
This is how I try to INSERT selected rows from SqlCommand sc:
string sqlcom2 = "INSERT INTO zajsluz(akce,text,castka,rocnik) values (#akce,#text,#castka,#rocnik)";
SqlCommand sc2 = new SqlCommand(sqlcom2, spojeni);
sc2.Parameters.AddWithValue("#akce", klientClass.Rocnik());
sc2.Parameters.AddWithValue("#text", ); // I dont know how to define this parameter according to what was selected in SqlCommand sc
spojeni.Open();
sc2.ExecuteNonQuery();
spojeni.Close();
Now I'm wondering hwo can I insert into "#text" (sc2) parameter values from SqlCommand "sc" would you please help me solve this out?
Thanks in advance
Edit: ยจ
this is what I tried:
DataSet dt2 = new DataSet();
SqlDataAdapter SDA2 = new SqlDataAdapter("SELECT text,castka FROM zajsluz WHERE akce='" + tentoradek + "' and rocnik='" + klientClass.Rocnik() + "'", spojeni);
SDA2.Fill(dt2);
spojeni.Close();
string sqlcom2 = "INSERT INTO zajsluz(akce,text,castka,rocnik) values (#akce,#text,#castka,#rocnik)";
SqlCommand sc2 = new SqlCommand(sqlcom2, spojeni);
sc2.Parameters.AddWithValue("#akce", zakce.Text);
sc2.Parameters.AddWithValue("#rocnik", klientClass.Rocnik());
sc2.Parameters.AddWithValue("#text", dt2.Tables[0].Columns["text"]);
sc2.Parameters.AddWithValue("#castka", dt2.Tables[0].Columns["castka"]);
spojeni.Open();
sc2.ExecuteNonQuery();
spojeni.Close();
You can directly use insert into & select combination
string sqcom = "INSERT INTO zajsluz(akce,text,castka,rocnik) SELECT rocnik,text,castka,rocnik FROM zajsluz WHERE akce='"+tentoradek+"' and rocnik='" + klientClass.Rocnik() + "'"
SqlCommand sc = new SqlCommand(sqcom,spojeni);
spojeni.Open();
sc.ExecuteNonQuery();
spojeni.Close();
I would try to do this in a single statement if that is possible, i.e. you aren't doing anything to the data in between the two statements.
string sqlcom = "INSERT INTO zajsluz(akce,text,castka,rocnik) SELECT akce,text,castka,rocnik FROM zajsluz WHERE akce='"+tentoradek+"' and rocnik='"+klientClass.Rocnik()+"'";
SqlCommand sc = new SqlCommand(sqcom,spojeni);
spojeni.Open();
sc.ExecuteNonQuery();
spojeni.Close();
Another option would be to use a SQL DataSet/DataTable, which allows you to query and return from SQL an entire table, or a set of rows, that you can then update, delete or insert into. It's described in the following MS article: http://support.microsoft.com/kb/326009/en
This summary answer for your question:
StringBuilder query = new Stringbuilder();
query.AppendLine("INSERT INTO zajsluz(akce,text,castka,rocnik) ");
query.AppendLine("(SELECT #akce, text, castka, #rocnik");
query.AppendLine("FROM zajsluz WHERE akce=#Tentoradek");
query.AppendLine("AND rocnik=#rocnik)");
SqlCommand sc2 = new SqlCommand(sqlcom2, spojeni);
sc2.Parameters.AddWithValue("#Tentoradek", tentoradek);
sc2.Parameters.AddWithValue("#akce", zakce.Text);
sc2.Parameters.AddWithValue("#rocnik", klientClass.Rocnik());
spojeni.Open();
sc2.ExecuteNonQuery();
spojeni.Close();
I want to insert multiple rows with different value into one table from anthor table which have some device_id but it gives error this is my query
MySqlCommand cmd = new MySqlCommand("INSERT INTO ind_master(ind,device_id) SELECT ind FROM schedule_days WHERE device_id = '"+DBdevice_id+"','"+DBdevice_id+"'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MySqlCommand cmd = new MySqlCommand("INSERT INTO ind_master(ind,device_id) SELECT ind,'"+DBdevice_id+"' FROM schedule_days WHERE device_id = '"+DBdevice_id+"'", con);
if device_id is integer, then remove the single quotes ''
I have a text box that retrieve email from membership table in database. User may edit their email and update the new email. My question is, how to replace the old email with the new one? What is the query for sql?
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
SqlCommand cmd = new SqlCommand("Insert into aspnet_membership("i dont know how whether to write all columns or only email);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#email", Textbox2.Text);
My question is, how to replace the old email with the new one?
You need an UPDATE in this case, instead of an INSERT:
SqlCommand cmd = new SqlCommand("UPDATE aspnet_membership SET
email = #email WHERE userID = #userID", conn);
It will work like any other simple update SQL Query:
update TableName set Columname = #Value where Username = #Value
Try this:
You may need UserId, and new Email to replace existing email address for the given existing user.
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
SqlCommand cmd = new SqlCommand("UPDATE aspnet_membership SET email = #newEmail WHERE UserID = #userID");
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#email", Textbox2.Text);
cmd.Parameters.AddWithValues("#UserId", YourUserId);
conn.open();
cmd.ExecuteNonQuery();
conn.close();
you need to use update query and not insert query.
see syntax here:
http://www.w3schools.com/sql/sql_update.asp
check this link from MSDN http://msdn.microsoft.com/en-us/library/ms186862.aspx
SELECT REPLACE('abcdefghicde','cde','xxx');
GO
As you want to Update EmailID so your query should be
SqlCommand cmd = new SqlCommand("Update aspnet_membership set email=#email where UserID=#UserID");
This should work for you, because your Insertquery which you are trying, this will always insert a new record instead of updating older records..
I've had help here
Inserting and Updating data to MDB
but still have a problem with update
I have an access mdb file with table "Table1" and 3 colums
ID
INFO
TEXT
I can add new info, find info but the update gives me an unknown error. Something is wrong with the command I sent.
con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = c:\\mdb\\testmdb.mdb");
cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "UPDATE Table1 SET Info = #Info, text = #text WHERE ID = #ID;";
cmd.Parameters.AddWithValue("#ID", textBox1.Text);
cmd.Parameters.AddWithValue("#Info", textBox2.Text);
cmd.Parameters.AddWithValue("#text", textBox3.Text);
con.Open(); // open the connection
int numAffected = cmd.ExecuteNonQuery();
con.Close();
The OleDbCommand does not support named parameters. This:
"UPDATE Table1 SET Info = #Info, text = #text WHERE ID = #ID;";
is equivalent to this:
"UPDATE Table1 SET Info = ?, text = ? WHERE ID = ?;";
and when you add parameters to the Parameters collection they are assigned in the order they are added. So the first parameter added will be assigned to the first placeholder, the second parameter to the second etc. You may use names for the placeholders for readability purposes but they do not matter when assigning values.
So you need to change the order in which you add the values to match the order in your query:
cmd.Parameters.AddWithValue("#Info", textBox2.Text);
cmd.Parameters.AddWithValue("#text", textBox3.Text);
cmd.Parameters.AddWithValue("#ID", textBox1.Text);