I have a datareader set up where it adds the Customer ID each time results are returned.
I have set up a 'next record' button which will run the same query but i want to set it so that the customer record returned does not equal the values stored in my list.
Is there a way to reference the list in a parameter for my MS Access query?
public void LoopThroughRecs(OleDbDataReader Reader)
{
List<int> customer = new List<int>();
while (Reader.Read())
{
int result;
CustID.Text = Reader["CustID"].ToString();
FirstName.Text = Reader["Initial"].ToString();
LastName.Text = Reader["Surname"].ToString();
Address1.Text = Reader["Address 1"].ToString();
Address2.Text = Reader["Address 2"].ToString();
Address3.Text = Reader["Address 3"].ToString();
TownCity.Text = Reader["Post Town"].ToString();
PostCode.Text = Reader["Post Code"].ToString();
Telephone.Text = Reader["Telephone"].ToString();
LstSvcDat.Text = Reader["LastService"].ToString();
BoilerMan.Text = Reader["Manufacturer"].ToString();
BoilerMod.Text = Reader["Model"].ToString();
result = Convert.ToInt32(CustID.Text);
customer.Add(int.Parse(CustID.Text));
}
}
public void SearchButton_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb";
try
{
conn.Open();
OleDbCommand command = new OleDbCommand("SELECT Contacts.CustID AS CustID,CustCode,Initial,Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone,Equipment.CustID AS CustID1,Equipment.Manufacturer AS Manufacturer,Equipment.Model AS Model,Equipment.LastService AS LastService FROM Contacts LEFT OUTER JOIN Equipment ON Equipment.CustID = Contacts.CustID WHERE Archived = 0 AND ([Address 1] = '" + textBox12.Text + "' OR CustCode LIKE '" + textBox12.Text + '%' + "' OR Initial = '" + textBox12.Text + '%' + "' OR Surname = '" + textBox12.Text + '%' + "' OR Initial = '" + textBox12.Text + '%' + "' OR [Post Town] LIKE '" + textBox12.Text + '%' + "' OR [Post Code] = '" + textBox12 + '%' + "')", conn);
OleDbDataReader Reader = command.ExecuteReader();
LoopThroughRecs(Reader);
}
finally
{
conn.Close();
}
}
public void NextRecord_Click_3(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb";
try
{
conn.Open();
OleDbCommand command = new OleDbCommand("SELECT Contacts.CustID AS CustID,CustCode,Initial,Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone,Equipment.CustID AS CustID1,Equipment.Manufacturer AS Manufacturer,Equipment.Model AS Model,Equipment.LastService AS LastService FROM Contacts LEFT OUTER JOIN Equipment ON Equipment.CustID = Contacts.CustID WHERE Archived = 0 AND Contacts.CustID <> #CustID AND ([Address 1] = '" + textBox12.Text + "' OR CustCode LIKE '" + textBox12.Text + '%' + "' OR Initial = '" + textBox12.Text + '%' + "' OR Surname = '" + textBox12.Text + '%' + "' OR Initial = '" + textBox12.Text + '%' + "' OR [Post Town] LIKE '" + textBox12.Text + '%' + "' OR [Post Code] = '" + textBox12 + '%' + "')", conn);
command.Parameters.Add(new OleDbParameter("#CustID" == Customer));
OleDbDataReader Reader = command.ExecuteReader();
LoopThroughRecs(Reader);
}
finally
{
conn.Close();
}
}
Related
I want to delete selected current row in datagrid from Microsoft SQL Server Management Studio but it gives syntax error whatever I did. (I will click the row and will delete data in SQL)Here is the codes and errors that I received:
Can you dear friends help me? thanks.
First: ERROR : 'Incorrect syntax near '='.'
private void VeriSİL()
{
baglan.Open();
string bgn = cwGrid.CurrentRow.Cells[0].Value.ToString();
string m1 = cwGrid.CurrentRow.Cells[1].Value.ToString();
string m2 = cwGrid.CurrentRow.Cells[2].Value.ToString();
string m3 = cwGrid.CurrentRow.Cells[3].Value.ToString();
string kturu = cwGrid.CurrentRow.Cells[4].Value.ToString();
string m1m = cwGrid.CurrentRow.Cells[5].Value.ToString();
string m2m = cwGrid.CurrentRow.Cells[6].Value.ToString();
string m3m = cwGrid.CurrentRow.Cells[7].Value.ToString();
string urunno = cwGrid.CurrentRow.Cells[8].Value.ToString();
string guc = cwGrid.CurrentRow.Cells[9].Value.ToString();
string hiz = cwGrid.CurrentRow.Cells[10].Value.ToString();
string odk = cwGrid.CurrentRow.Cells[11].Value.ToString();
string gaz = cwGrid.CurrentRow.Cells[12].Value.ToString();
string aci = cwGrid.CurrentRow.Cells[13].Value.ToString();
string noz = cwGrid.CurrentRow.Cells[14].Value.ToString();
string sur = cwGrid.CurrentRow.Cells[15].Value.ToString();
string foto = cwGrid.CurrentRow.Cells[16].Value.ToString();
string not = cwGrid.CurrentRow.Cells[17].Value.ToString();
SqlCommand komut = new SqlCommand("delete from CW where Beğeni = '" + bgn + "' AND [1.Malz] = '" + m1 + "' AND [2.Malz] = '" + m2 + "' AND [3.Malz] = '" + m3 + "' AND Kaynaktürü = '" + kturu + "' AND [1/mm] = '" + m1m + "' AND [2/mm] = '" + m2m + "' AND [3/mm] = '" + m3m + "' AND ÜrünNo = '" + urunno + "'AND Güç = '" + guc + "' AND Hız = '" + hiz + "' AND Gaz = = '" + gaz + "' AND Açı = '" + aci + "' AND Nozzle = '" + noz + "' AND Süre = '" + sur + "' AND Foto= '" + foto + "' AND [Not]= '" + noz + "'", baglan);
komut.ExecuteNonQuery();
baglan.Close();
}
private void silButton_Click(object sender, EventArgs e)
{
VeriSİL();
}
My Second try and the error : 'Incorrect syntax near ','.'
private void VeriSİL()
{
baglan.Open();
SqlCommand komut = new SqlCommand("Delete CW WHERE Beğeni=#bgn,[1.Malz]=#m1,[2.Malz]=#m2,[3.Malz]=#m3,KaynakTürü=#kturu,[1/mm]=#m1m,[2/mm]=#m2m,[3/mm]=#m3m,Güç=#guc,Hız=#hiz,Odak=#odk,Gaz=#gaz,Açı=#aci,Nozzle=#noz,Süre=#sur,Foto=#foto,[Not]=#not,ÜrünNo=#urunno", baglan);
komut.Parameters.AddWithValue("#bgn", cwGrid.CurrentRow.Cells[0].Value.ToString());
komut.Parameters.AddWithValue("#m1", cwGrid.CurrentRow.Cells[1].Value.ToString());
komut.Parameters.AddWithValue("#m2", cwGrid.CurrentRow.Cells[2].Value.ToString());
komut.Parameters.AddWithValue("#m3", cwGrid.CurrentRow.Cells[3].Value.ToString());
komut.Parameters.AddWithValue("#kturu", cwGrid.CurrentRow.Cells[4].Value.ToString());
komut.Parameters.AddWithValue("#m1m", cwGrid.CurrentRow.Cells[5].Value.ToString());
komut.Parameters.AddWithValue("#m2m", cwGrid.CurrentRow.Cells[6].Value.ToString());
komut.Parameters.AddWithValue("#m3m", cwGrid.CurrentRow.Cells[7].Value.ToString());
komut.Parameters.AddWithValue("#urunno", cwGrid.CurrentRow.Cells[8].Value.ToString());
komut.Parameters.AddWithValue("#guc", cwGrid.CurrentRow.Cells[9].Value.ToString());
komut.Parameters.AddWithValue("#hiz", cwGrid.CurrentRow.Cells[10].Value.ToString());
komut.Parameters.AddWithValue("#odk", cwGrid.CurrentRow.Cells[11].Value.ToString());
komut.Parameters.AddWithValue("#gaz", cwGrid.CurrentRow.Cells[12].Value.ToString());
komut.Parameters.AddWithValue("#aci", cwGrid.CurrentRow.Cells[13].Value.ToString());
komut.Parameters.AddWithValue("#noz", cwGrid.CurrentRow.Cells[14].Value.ToString());
komut.Parameters.AddWithValue("#sur", cwGrid.CurrentRow.Cells[15].Value.ToString());
komut.Parameters.AddWithValue("#foto", cwGrid.CurrentRow.Cells[16].Value.ToString());
komut.Parameters.AddWithValue("#not", cwGrid.CurrentRow.Cells[17].Value.ToString());
komut.ExecuteNonQuery();
MessageBox.Show("Silme İşlemi Başarılı.");
baglan.Close();
}
private void silButton_Click(object sender, EventArgs e)
{
VeriSİL();
}
My last try and error : 'An expression of non-boolean type specified in a context where a condition is expected, near ','.'
private void VeriSİL()
{
baglan.Open();
SqlCommand komut = new SqlCommand("Delete from CW where Beğeni,[1.Malz],[2.Malz],[3.Malz],KaynakTürü,[1/mm],[2/mm],[3/mm],ÜrünNo,Güç,Hız,Gaz,Odak,Açı,Nozzle,Süre,Foto,[Not] like '" +
cwGrid.CurrentRow.Cells[0].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[1].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[2].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[3].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[4].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[5].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[6].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[7].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[8].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[9].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[10].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[11].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[12].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[13].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[14].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[15].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[16].Value.ToString() + "' , '" +
cwGrid.CurrentRow.Cells[17].Value.ToString() + "'", baglan);
komut.ExecuteNonQuery();
MessageBox.Show("Silme İşlemi Başarılı.");
baglan.Close();
}
private void silButton_Click(object sender, EventArgs e)
{
VeriSİL();
}
I want to make a notification if in table barang < 0.
This my code:
if (cm_status.SelectedItem.ToString() == "Keluar")
{
cmd = new SqlCommand("SELECT Qty FROM Tb_Barang WHERE Nama_Barang = '"+ cm_pilihBarang.SelectedItem.ToString() +"'", conn);
object S = cmd.ExecuteScalar();
if (S < 0)
{
MessageBox.Show("not enough");
}
else
{
cmd = new SqlCommand("UPDATE Tb_Barang SET Qty=Qty - '" + txt_jumlah.Text + "' WHERE Nama_Barang= '" + cm_pilihBarang.SelectedItem.ToString() + "'", conn);
cmd.ExecuteNonQuery();
cmd = new SqlCommand("INSERT INTO Tb_Transaksi VALUES ('" + txt_KodeTransaksi.Text + "', '" + cm_pilihBarang.SelectedItem.ToString() + "', '" + txt_jumlah.Text + "', '" + Datepicker1.Value.Date.ToString("M/d/yyyy") + "', '" + cm_select.SelectedItem.ToString() + "', '" + txt_mengetahui.Text + "', '" + txt_diketahui.Text + "', '" + txt_keterangan.Text + "', '" + cm_status.SelectedItem.ToString() + "' )", conn);
cmd.ExecuteNonQuery();
}
}
Please help me out.
I have written code to return values based on a search box however at times, the value entered into the search box can apply to several records within an MSAccess DB.
Is there a way where I can scroll through the records that apply to the value in the search box by using a button?
public void LoopThroughRecs(OleDbDataReader Data)
{
Data.Read();
{
FirstName.Text = Data["Initial"].ToString();
LastName.Text = Data["Surname"].ToString();
Address1.Text = Data["Address 1"].ToString();
Address2.Text = Data["Address 2"].ToString();
Address3.Text = Data["Address 3"].ToString();
TownCity.Text = Data["Post Town"].ToString();
PostCode.Text = Data["Post Code"].ToString();
Telephone.Text = Data["Telephone"].ToString();
LstSvcDat.Text = Data["LastService"].ToString();
BoilerMan.Text = Data["Manufacturer"].ToString();
BoilerMod.Text = Data["Model"].ToString();
}
Data.Close();
}
public void button2_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb";
try
{
conn.Open();
OleDbCommand command = new OleDbCommand("SELECT CustCode,Initial,Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone,Equipment.CustID AS CustID,Equipment.Manufacturer AS Manufacturer,Equipment.Model AS Model,Equipment.LastService AS LastService FROM Contacts LEFT OUTER JOIN Equipment ON Equipment.CustID = Contacts.CustID WHERE Archived = 0 AND (CustCode = '" + textBox12.Text + "' OR Initial = '" + textBox12.Text + "' OR Surname = '" + textBox12.Text + "' OR Initial = '" + textBox12.Text + "' OR [Post Town] = '" + textBox12.Text + "' OR [Post Code] = '" + textBox12 + "')", conn);
OleDbDataReader Data = command.ExecuteReader();
LoopThroughRecs(Data);
}
finally
{
conn.Close();
}
}
I am assigning the value to the variable from the text box on the page during the pageupload event of AjaxFileUpload1.The problem is that, I am not getting the value from the text box to my variable even though no error throws.My variables are
string scn = txtSCN.Text;
string line1 = txtLineitem.Text;
string aging1 = txtAging.Text;
Any idea why AjaxFileUpload1_UploadComplete is not able to read text box value
My cs Code is:
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string c = System.IO.Path.GetFileName(e.FileName);
string dpath = "~/Profile/Images/";
string scn = txtSCN.Text;
string line1 = txtLineitem.Text;
string aging1 = txtAging.Text;
AjaxFileUpload1.SaveAs(MapPath(Path.Combine(dpath,c)));
dpath = dpath + c;
string str1 = ConfigurationManager.ConnectionStrings["ProTracConnGMCH"].ConnectionString;
SqlConnection cn = new SqlConnection(str1);
cn.Open();
string sql = "Update tbNoquoteFollowupupdate set MailreceivedURL = '" + dpath + "', chkMailreceived = 1 , Buyername = '" + buyername + "' where scn = '" + scn + "' AND lineItem = '" + line1 + "' and Aging ='" + aging1 + "' ";
SqlCommand cmd = new SqlCommand(sql, cn);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
// AjaxFileUpload1.SaveAs(Path.Combine(dpath, e.FileName));
//AjaxFileUpload1.SaveAs(MapPath(dpath));
}
cn.Close();
BindGridviewData1();
cn.Open();
string cmd2 = "Insert Into tbMulitmailsreived (scn, lineItem,followupdate, Aging,MailreceivedURL) Values ('" + scn + "', '" + line1 + "','" + DateTime.Now + "','" + aging1 + "','" + dpath + "')";
SqlCommand sqlCommand2 = new SqlCommand(cmd2, cn);
sqlCommand2.ExecuteNonQuery();
cn.Close();
}
Please help me
I spent some time last week investigating this question but in the end couldn't find an easy solution for this. The OP in that question solved it by storing values in the Session but for this to work you would still need to cause a postback at some stage.
There apparently was functionality planned for the AjaxFileUpload control to pass values in the Context Keys collections but this was never implemented. This question describes how to implement this yourself though.
I think I saw another question around the same topic and the OP solved it by changing to using the AsyncFileUpload control but I stand to be corrected...
i think you need to add !Page.IsPostBack to your code. like this
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
if(!Page.IsPostBack)
{
string c = System.IO.Path.GetFileName(e.FileName);
string dpath = "~/Profile/Images/";
string scn = txtSCN.Text;
string line1 = txtLineitem.Text;
string aging1 = txtAging.Text;
AjaxFileUpload1.SaveAs(MapPath(Path.Combine(dpath,c)));
dpath = dpath + c;
string str1 = ConfigurationManager.ConnectionStrings["ProTracConnGMCH"].ConnectionString;
SqlConnection cn = new SqlConnection(str1);
cn.Open();
string sql = "Update tbNoquoteFollowupupdate set MailreceivedURL = '" + dpath + "', chkMailreceived = 1 , Buyername = '" + buyername + "' where scn = '" + scn + "' AND lineItem = '" + line1 + "' and Aging ='" + aging1 + "' ";
SqlCommand cmd = new SqlCommand(sql, cn);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
// AjaxFileUpload1.SaveAs(Path.Combine(dpath, e.FileName));
//AjaxFileUpload1.SaveAs(MapPath(dpath));
}
cn.Close();
BindGridviewData1();
cn.Open();
string cmd2 = "Insert Into tbMulitmailsreived (scn, lineItem,followupdate, Aging,MailreceivedURL) Values ('" + scn + "', '" + line1 + "','" + DateTime.Now + "','" + aging1 + "','" + dpath + "')";
SqlCommand sqlCommand2 = new SqlCommand(cmd2, cn);
sqlCommand2.ExecuteNonQuery();
cn.Close();
}
}
i have a button that suppose to update data into the database.
private void button4_Click(object sender, EventArgs e)
{
//need update code//
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daCount = new SqlDataAdapter("select iCount from ComDet where cName = #cName", conn);
daCount.SelectCommand.Parameters.Add("#cName", SqlDbType.VarChar).Value = ListU.SelectedValue;
DataTable dtC = new DataTable();
daCount.Fill(dtC);
DataRow firstRow = dtC.Rows[0];
string x = firstRow["iCount"].ToString();
int y = Int32.Parse(x);
int z = y + 1;
//SqlCeCommand cmdC = conn.CreateCommand();
SqlCommand cmdC = conn.CreateCommand();
cmdC.CommandText = "Update ComDet set iCount = '" + z + "', ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
conn.Close();
}
but i get this error..
can someone help?
update =
i've changed my code to
cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
but the problem now is that , there's no update.
the iCount in the database is an INT , value is 0.
There is also no update for the viewtime and lastview.
where did i go wrong now?
change this:
cmdC.CommandText = "Update ComDet set iCount = '" + z + "', ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
to
cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
you dont need the "'" apostrophe around it becuase its a number. That would definitely get you string not in correct format error
I would guess maybe the icount value is not a number, i would recommend using TryParse just in case. And that should keep this error from happening. What to do about a bad value getting returned by the query is another issue.
private void button4_Click(object sender, EventArgs e)
{
//need update code//
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daCount = new SqlDataAdapter("select iCount from ComDet where cName = #cName", conn);
daCount.SelectCommand.Parameters.Add("#cName", SqlDbType.VarChar).Value = ListU.SelectedValue;
DataTable dtC = new DataTable();
daCount.Fill(dtC);
DataRow firstRow = dtC.Rows[0];
string x = firstRow["iCount"].ToString();
int y = 0;
if(Int32.TryParse(x,out y))
{
System.Diagnostics.Debug.WriteLine("iCount was an valid int32");
int z = y + 1;
//SqlCeCommand cmdC = conn.CreateCommand();
SqlCommand cmdC = conn.CreateCommand();
cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
}
else
System.Diagnostics.Debug.WriteLine("iCount was NOT a valid int32, value: " + x);
conn.Close();
}
Have you checked the value of the 'x' variable? The exception informs that the value of X isn't a valid integer, so the FormatException is thrown.