I am using Oledb to connect with an excel sheet. My connection is working fine. Insert and retrieve data are also working fine. But when I try to update a record in excel sheet with where clause then my code executes successfully without any error but excel row does not update.
My code is:
strQry = #"Update [Guests$] set [FirstName]=#FirstName,[LastName]=#LastName,[Address]=#Address,
[EmailId]=#EmailId,[TelNo]=#TelNo,[MobileNo]=#MobileNo,[FaxNo]=#FaxNo where [GuestId]=#GuestId";
using (OleDbConnection con = new OleDbConnection(clsConnection.conStr))
{
using (OleDbCommand cmd = new OleDbCommand(strQry,con))
{
cmd.Parameters.Add("#GuestId", OleDbType.Integer).Value = intId;
cmd.Parameters.Add("#FirstName", OleDbType.VarChar, 15).Value = txtFirstName.Text;
cmd.Parameters.Add("#LastName", OleDbType.VarChar, 15).Value = txtLastName.Text;
cmd.Parameters.Add("#Address", OleDbType.VarChar, 200).Value = txtAddress.Text;
cmd.Parameters.Add("#EmailId", OleDbType.VarChar, 50).Value = txtEmail.Text;
cmd.Parameters.Add("#TelNo", OleDbType.VarChar, 20).Value = txtTelNo.Text;
cmd.Parameters.Add("#MobileNo", OleDbType.VarChar, 15).Value = txtMobileNo.Text;
cmd.Parameters.Add("#FaxNo", OleDbType.VarChar, 20).Value = txtFaxNo.Text;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
MessageBox.Show(strSuccessMsg);
BinddgvGuests();
ClearControls();
}
}
But when I remove the where clause then all the records are updated.
Is there something wrong with my where clause?
After searching a lot, finally I encounter my mistake. Actually parameters should be in same sequence as in the query. So I change my parameters to:
cmd.Parameters.Add("#FirstName", OleDbType.VarChar, 15).Value = txtFirstName.Text;
cmd.Parameters.Add("#LastName", OleDbType.VarChar, 15).Value = txtLastName.Text;
cmd.Parameters.Add("#Address", OleDbType.VarChar, 200).Value = txtAddress.Text;
cmd.Parameters.Add("#EmailId", OleDbType.VarChar, 50).Value = txtEmail.Text;
cmd.Parameters.Add("#TelNo", OleDbType.VarChar, 20).Value = txtTelNo.Text;
cmd.Parameters.Add("#MobileNo", OleDbType.VarChar, 15).Value = txtMobileNo.Text;
cmd.Parameters.Add("#FaxNo", OleDbType.VarChar, 20).Value = txtFaxNo.Text;
cmd.Parameters.Add("#GuestId", OleDbType.Integer).Value = intId;
Now this is working fine for me.
Related
I want to do update and insert in one button.
i want it to check if there's data then it will just update it but if not it will add it.
I don't know what i'm missing in this code
cnx.Open();
SqlCommand cmd = cnx.CreateCommand();
SqlDataReader dr;
cmd.CommandText = "Select * from TPayement where ID='" + txt_ID.Text + "'";
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
cmd.CommandText = "update TPayement (ID, Nom, QUA, Salaire, NombreJour, HeurSupplimentaire, SalaireHeur, Somme) values (#ID, #Nom, #QUA, #Salaire, #NombreJour, #HeurSupplimentaire, #SalaireHeur, #Somme) where ID='" + txt_ID.Text + "'";
cmd.Parameters.Add("#ID", SqlDbType.Int);
cmd.Parameters.Add("#Nom", SqlDbType.VarChar, 50);
cmd.Parameters.Add("#QUA", SqlDbType.VarChar, 50);
cmd.Parameters.Add("#Salaire", SqlDbType.Float);
cmd.Parameters.Add("#NombreJour", SqlDbType.Float);
cmd.Parameters.Add("#HeurSupplimentaire", SqlDbType.Float);
cmd.Parameters.Add("#SalaireHeur", SqlDbType.Float);
cmd.Parameters.Add("#Somme", SqlDbType.Float);
cmd.Parameters["#ID"].Value = txt_ID.Text;
cmd.Parameters["#Nom"].Value = txt_Nom.Text;
cmd.Parameters["#QUA"].Value = txt_QUA.Text;
cmd.Parameters["#Salaire"].Value = txt_Salaire.Text;
cmd.Parameters["#NombreJour"].Value = txt_NBRJ.Text;
cmd.Parameters["#HeurSupplimentaire"].Value = txt_HSUP.Text;
cmd.Parameters["#SalaireHeur"].Value = txt_SalireHeur.Text;
cmd.Parameters["#Somme"].Value = txt_Somme.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("Se payement est enregistrer");
}
else
{
cmd.CommandText = "insert into TPayement (ID, Nom, QUA, Salaire, NombreJour, HeurSupplimentaire, SalaireHeur, Somme) values (#ID, #Nom, #QUA, #Salaire, #NombreJour, #HeurSupplimentaire, #SalaireHeur, #Somme)";
cmd.Parameters.Add("#ID", SqlDbType.Int);
cmd.Parameters.Add("#Nom", SqlDbType.VarChar, 50);
cmd.Parameters.Add("#QUA", SqlDbType.VarChar, 50);
cmd.Parameters.Add("#Salaire", SqlDbType.Float);
cmd.Parameters.Add("#NombreJour", SqlDbType.Float);
cmd.Parameters.Add("#HeurSupplimentaire", SqlDbType.Float);
cmd.Parameters.Add("#SalaireHeur", SqlDbType.Float);
cmd.Parameters.Add("#Somme", SqlDbType.Float);
cmd.Parameters["#ID"].Value = txt_ID.Text;
cmd.Parameters["#Nom"].Value = txt_Nom.Text;
cmd.Parameters["#QUA"].Value = txt_QUA.Text;
cmd.Parameters["#Salaire"].Value = txt_Salaire.Text;
cmd.Parameters["#NombreJour"].Value = txt_NBRJ.Text;
cmd.Parameters["#HeurSupplimentaire"].Value = txt_HSUP.Text;
cmd.Parameters["#SalaireHeur"].Value = txt_SalireHeur.Text;
cmd.Parameters["#Somme"].Value = txt_Somme.Text;
dr.Close();
cmd.ExecuteNonQuery();
MessageBox.Show("Se payement est enregistrer");
}
dr.Close();
cnx.Close();
The problem is that you open a DataReader with the Command object cmd.
Then, before you close the DataReader, you try to re-purpose the same cmd object and Execute it.
You have to close the DataReader before you can re-use the cmd object for an insert or update.
I am trying to insert into an access database. all the fields worked except for the "passwordx" field in which I get data type mismatch in criteria expression.
string commandSql = "INSERT INTO Members ( lastName, firstName, id, dob, phone, areaCodeNo, adress, cityNo, zipCode, email, active,passwordx) VALUES ( #lastName, #firstName, #id, #dob, #phone, #areaCodeNo, #adress, #cityNo, #zipCode, #email, #active,#passwordx); ";
OleDbCommand command = new OleDbCommand(commandSql, connection);
command.Parameters.Add("#lastName", OleDbType.VarChar, 100).Value = mem.lastName;
command.Parameters.Add("#firstName", OleDbType.VarChar, 100).Value = mem.firstName;
command.Parameters.Add("#id", OleDbType.VarChar, 100).Value = mem.id;
command.Parameters.Add("#dob", OleDbType.VarChar, 100).Value = mem.dob;
command.Parameters.Add("#phone", OleDbType.VarChar, 100).Value = mem.phone;
command.Parameters.Add("#areaCodeNo", OleDbType.Integer, 100).Value = mem.areaCodeNo;
command.Parameters.Add("#adress", OleDbType.VarChar, 100).Value = mem.adress;
command.Parameters.Add("#cityNo", OleDbType.Integer, 100).Value = mem.cityNo;
command.Parameters.Add("#zipCode", OleDbType.VarChar, 100).Value = mem.zipCode;
command.Parameters.Add("#email", OleDbType.VarChar, 100).Value = mem.email;
command.Parameters.Add("#passwordx", OleDbType.VarChar, 100).Value = mem.password;
command.Parameters.Add("#active", OleDbType.Boolean, 100).Value = mem.active;
connection.Open();
affectedRows = command.ExecuteNonQuery();
connection.Close();
thanks for the help.
Shuffle the last two parameters to match the sequence of your SQL:
command.Parameters.Add("#active", OleDbType.Boolean, 100).Value = mem.active;
command.Parameters.Add("#passwordx", OleDbType.VarChar, 100).Value = mem.password;
And a Boolean doesn't have a length.
I'm new to C# coding and this website so bear with me. On the button click I want my stored procedure to execute and populate the datagridview. When I run the application and click the button I keep getting an error saying:
"No mapping exists from object type System.Windows.Forms.DataGridViewTextBoxCell to a known managed provider native type."
How do I fix this? I've tried converting the parameters to their data types which doesn't seem to work either. I've been stuck on this for a while and am struggling to find anything on the internet about my specific error. Thank you.
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sqlcn1 = new SqlConnection("My Server Connection String");
sqlcn1.Open();
//Stored Procedure
SqlCommand sqlcmddel = new SqlCommand("My_Stored_Procedure", sqlcn1);
sqlcmddel.CommandType = CommandType.StoredProcedure;
sqlcmddel.ExecuteNonQuery();
sqlcn1 = new SqlConnection("My Server Connection String");
sqlcn1.Open();
foreach (DataGridViewRow row in dataGridView1.Rows) ;
SqlCommand sqlcmdins = new SqlCommand("My_Stored_Procedure", sqlcn1);
//Stored Procedure
sqlcmdins.CommandType = CommandType.StoredProcedure;
sqlcmdins.Parameters.Add("#sugnum", SqlDbType.Int).Value = dataGridView1.Rows[Ournum].Cells[0];
sqlcmdins.Parameters.Add("#sugtype", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[1];
sqlcmdins.Parameters.Add("#buyerid", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[2];
sqlcmdins.Parameters.Add("#duedate", SqlDbType.DateTime).Value = dataGridView1.Rows[Ournum].Cells[3];
sqlcmdins.Parameters.Add("#xrelqty", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[4];
sqlcmdins.Parameters.Add("#purchasingfactor", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[5];
sqlcmdins.Parameters.Add("#relqty", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[6];
sqlcmdins.Parameters.Add("#jobnum", SqlDbType.NVarChar, 20).Value = dataGridView1.Rows[Ournum].Cells[7];
sqlcmdins.Parameters.Add("#assemblyseq", SqlDbType.SmallInt).Value = dataGridView1.Rows[Ournum].Cells[8];
sqlcmdins.Parameters.Add("#jobseq", SqlDbType.SmallInt).Value = dataGridView1.Rows[Ournum].Cells[9];
sqlcmdins.Parameters.Add("#warehousecode", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[10];
sqlcmdins.Parameters.Add("#fob", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[11];
sqlcmdins.Parameters.Add("#shipviacode", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[12];
sqlcmdins.Parameters.Add("#termscode", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[13];
sqlcmdins.Parameters.Add("#vendornum", SqlDbType.Int).Value = dataGridView1.Rows[Ournum].Cells[14];
sqlcmdins.Parameters.Add("#purpoint", SqlDbType.Int).Value = dataGridView1.Rows[Ournum].Cells[15];
sqlcmdins.Parameters.Add("#linedesc", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[16];
sqlcmdins.Parameters.Add("#ium", SqlDbType.NVarChar, 10).Value = dataGridView1.Rows[Ournum].Cells[17];
sqlcmdins.Parameters.Add("#unitprice", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[18];
sqlcmdins.Parameters.Add("#docunitprice", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[19];
sqlcmdins.Parameters.Add("#taxable", SqlDbType.Bit).Value = dataGridView1.Rows[Ournum].Cells[20];
Your issue is that you're trying to pass the Cell into the stored procedure. You need to pass in the cells value.
Also, because you've put a ';' at the end of the foreach call, your loop around the rows isn't doing anything. Assuming you're trying to insert all the rows from the datagridview, you'll need something like this...
using (SqlConnection sqlcn1 = new SqlConnection("My Server Connection String"))
{
sqlcn1.Open();
//Stored Procedure
using (SqlCommand sqlcmddel = new SqlCommand("My_Stored_Procedure", sqlcn1))
{
sqlcmddel.CommandType = CommandType.StoredProcedure;
sqlcmddel.ExecuteNonQuery();
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
using (SqlCommand sqlcmdins = new SqlCommand("My_Stored_Procedure", sqlcn1))
{
sqlcmdins.CommandType = CommandType.StoredProcedure;
//Stored Procedure
sqlcmdins.Parameters.Add("#sugnum", SqlDbType.Int).Value = row.Cells[0].Value;
sqlcmdins.Parameters.Add("#sugtype", SqlDbType.NVarChar, 50).Value = row.Cells[1].Value;
sqlcmdins.Parameters.Add("#buyerid", SqlDbType.NVarChar, 50).Value = row.Cells[2].Value;
sqlcmdins.Parameters.Add("#duedate", SqlDbType.DateTime).Value = row.Cells[3].Value;
sqlcmdins.Parameters.Add("#xrelqty", SqlDbType.Float).Value = row.Cells[4].Value;
sqlcmdins.Parameters.Add("#purchasingfactor", SqlDbType.Float).Value = row.Cells[5].Value;
sqlcmdins.Parameters.Add("#relqty", SqlDbType.Float).Value = row.Cells[6].Value;
sqlcmdins.Parameters.Add("#jobnum", SqlDbType.NVarChar, 20).Value = row.Cells[7].Value;
sqlcmdins.Parameters.Add("#assemblyseq", SqlDbType.SmallInt).Value = row.Cells[8].Value;
sqlcmdins.Parameters.Add("#jobseq", SqlDbType.SmallInt).Value = row.Cells[9].Value;
sqlcmdins.Parameters.Add("#warehousecode", SqlDbType.NVarChar, 50).Value = row.Cells[10].Value;
sqlcmdins.Parameters.Add("#fob", SqlDbType.NVarChar, 50).Value = row.Cells[11].Value;
sqlcmdins.Parameters.Add("#shipviacode", SqlDbType.NVarChar, 50).Value = row.Cells[12].Value;
sqlcmdins.Parameters.Add("#termscode", SqlDbType.NVarChar, 50).Value = row.Cells[13].Value;
sqlcmdins.Parameters.Add("#vendornum", SqlDbType.Int).Value = row.Cells[14].Value;
sqlcmdins.Parameters.Add("#purpoint", SqlDbType.Int).Value = row.Cells[15].Value;
sqlcmdins.Parameters.Add("#linedesc", SqlDbType.NVarChar, 50).Value = row.Cells[16].Value;
sqlcmdins.Parameters.Add("#ium", SqlDbType.NVarChar, 10).Value = row.Cells[17].Value;
sqlcmdins.Parameters.Add("#unitprice", SqlDbType.Float).Value = row.Cells[18].Value;
sqlcmdins.Parameters.Add("#docunitprice", SqlDbType.Float).Value = row.Cells[19].Value;
sqlcmdins.Parameters.Add("#taxable", SqlDbType.Bit).Value = row.Cells[20].Value;
sqlcmdins.ExecuteNonQuery();
}
}
}
I'm having to select, update, delete, etc. basically everywhere on my web application and every time I'm having to write something like this:
con.Open();
cmd = new SqlCommand("update items set item_cost = #cost, item_retail_value = #retail, item_v_style = #v_style, item_v_color = #v_color, item_description = #description, " +
"item_date_modify = #date, item_time_modify = #time, item_user_modify = #user where item_style = #style and item_color = #color and item_sec_dimenssion = #sec", con);
cmd.Parameters.Add("#style", SqlDbType.VarChar, 15).Value = styl;
cmd.Parameters.Add("#color", SqlDbType.VarChar, 3).Value = colr;
cmd.Parameters.Add("#sec", SqlDbType.VarChar, 8).Value = sdim;
cmd.Parameters.Add("#size", SqlDbType.VarChar, 3).Value = size;
cmd.Parameters.Add("#cost", SqlDbType.VarChar, 8).Value = sprice;
cmd.Parameters.Add("#retail", SqlDbType.VarChar, 8).Value = sretail;
cmd.Parameters.Add("#uom", SqlDbType.VarChar, 3).Value = uom;
cmd.Parameters.Add("#sku", SqlDbType.VarChar, 10).Value = sku;
cmd.Parameters.Add("#barcode", SqlDbType.VarChar, 20).Value = barcode;
cmd.Parameters.Add("#v_style", SqlDbType.VarChar, 100).Value = v_style;
cmd.Parameters.Add("#v_color", SqlDbType.VarChar, 20).Value = v_color;
cmd.Parameters.Add("#description", SqlDbType.VarChar, 40).Value = description;
cmd.Parameters.Add("#date", SqlDbType.VarChar, 20).Value = date;
cmd.Parameters.Add("#time", SqlDbType.VarChar, 20).Value = time;
cmd.Parameters.Add("#user", SqlDbType.VarChar, 20).Value = user;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
Is there a way to create a Class to create SqlConnection with a SqlCommand for "Select", "Update", "Delete", etc. and just provide the table, the fields, parameters and the criteria so I don't have to write all this code every time.
Any help will really appreciate it.
Linq2Sql
Entity Framework
nHibernate
Dapper
BLToolkit
create a class with Singleton and then every time you want to do anything with SqlConnection
you can use the same object.
I'm going crazy here, can someone please tell me what's wrong with this code
con.Open();
cmd = new SqlCommand("INSERT INTO COUNTERS(COUNTER_START, COUNTER_CURRENT, COUNTER_NEXT, COUNTER_TYPE, COUNTER_DATE_CREATED, COUNTER_TIME_CREATED, COUNTER_CREATED_BY) " +
"VALUES(#counter_start, #counter_current, #counter_next, #counter_type, #date, #time, #user)", con);
cmd.Parameters.Add("#counter_start", SqlDbType.Int).Value = counter_start.Text;
cmd.Parameters.Add("#counter_current", SqlDbType.Int).Value = counter_current.Text;
cmd.Parameters.Add("#counter_next", SqlDbType.Int).Value = counter_next.Text;
cmd.Parameters.Add("#counter_type", SqlDbType.VarChar, 10).Value = counter_type.Text;
cmd.Parameters.Add("#date", SqlDbType.VarChar, 20).Value = date;
cmd.Parameters.Add("#time", SqlDbType.VarChar, 20).Value = time;
cmd.Parameters.Add("#user", SqlDbType.VarChar, 50).Value = user;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
I'm getting this error
The parameterized query '(#counter_start int,#counter_current int,#counter_next int,#coun' expects the parameter '#user', which was
not supplied
Any input will be really appreciate it.
You may try debugging the application and see what value is being held by variable user. I think the user you are supplying is null. If you are using C# 4.0 then try
cmd.Parameters.Add("#user", SqlDbType.VarChar, 50).Value = user ?? DBNull.Value;
or for earlier versions of C#
cmd.Parameters.Add("#user", SqlDbType.VarChar, 50).Value = user != null ? user : DBNull.Value;
You may also use string.Empty in place of DBNull.Value, if you want an empty string instead of DBNull