Код для поиска записи с помощью C# и SQL
Я разрабатываю решение, которое имеет master и addEmployee в виде таблицы и формы соответственно. Решение имеет кнопку поиска для поиска записи. Решение использует C# и базы данных sql2008 в качестве бэк-конец. После компиляции решения я не получаю никакого ответа при нажатии кнопки поиска. Кто-нибудь может мне помочь?
Что я уже пробовал:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Configuration; using System.IO; using System.Data.SqlTypes; namespace FHAMortgageBank { public partial class Add_Employee : Form { public Add_Employee() { InitializeComponent(); double bsal, tranA, hous, lunc, uti, furn, harz, educ, housM, dres, risk, veh, driv, dome, pfs, nhf, nhfs, oded, housU, staffM, thrift, otherP, Nhis, pens, paye, tpay, tded, npay; ClearData();// Method FillBank(); FillStatus(); FillPenAdmin(); FillYear(); FillGrade(); FillDpt(); New_Rec(); DisplayData(); New_Rec(); Calculation(); } //public void calculateTotal() //{ //} public static string constring = "Data Source=AKINTOMIDE-HP\\SQLEXPRESS;Initial Catalog=fham;Integrated Security=True"; SqlConnection con = new SqlConnection("Data Source=AKINTOMIDE-HP\\SQLEXPRESS;Initial Catalog=fham;Integrated Security=True"); SqlCommand cmd; SqlDataReader dreader; SqlDataAdapter adapt = new SqlDataAdapter(); private DataViewManager dviewmanager; int SN = 0; //double bsal; bsal;double tranA;double hous;double lunc;uti;furn;harz;educ;housM;dres;risk;veh;driv;dome;pfs;nhf;nhfs;oded;housU;staffM;thrift;otherP;Nhis;pens;paye;tded;tpay;npay; private void frmMain_Enter(object sender, EventArgs e) { } private void picStafPix_Click(object sender, EventArgs e) { //this.picStafPix.Image = Image.FromFile(@"Images\a.bmp"); } private void button1_Click(object sender, EventArgs e) { // open file dialog OpenFileDialog open = new OpenFileDialog(); // image filters open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"; if (open.ShowDialog() == DialogResult.OK) { // display image in picture box this.picStafPix.Image = new Bitmap(open.FileName); // image file path textBox1.Text = open.FileName; } } private void Calculation() { // double bsal = Convert.ToInt32(txtBSal.Text); // double hous = Convert.ToInt32(txtHous.Text); // double tpay = bsal + hous; // txtTPay.Text = tpay.ToString(); //double temp2 = double.Parse(textBox2.Text); //txtTPay.Text += tpay; txtBSal.Text += bsal; txtTran.Text += tranA; txtHous.Text += hous; txtLunc.Text += lunc; //txtDres.Text += dres; txtFurn.Text += furn; txtHarz.Text += harz; txtEduc.Text += educ; txtRisk.Text += risk; //txtVeh.Text += veh; txtDriv.Text += driv; txtDome.Text += dome; txtOtherPay.Text += otherP; txtUti.Text += uti; txtHousM.Text += housM; ////bsal = Convert.ToInt32(txtBSal.Text); tpay = Convert.ToInt32(txtTPay.Text); tranA = Convert.ToInt32(txtTran.Text); ////// tpay =bsal + tranA + hous+lunc+uti+furn+harz+educ+housM+dres+risk+veh+driv+dome+otherP; ////// tpay = bsal + tranA ; ////// txtTPay.Text =tpay.ToString(); ////// txtTPay.Text = (Convert.ToInt32(txtBSal.Text) + Convert.ToInt32(txtTran.Text).ToString()); ////// txtTPay.Text = convert.toString(Convert.ToInt32(txtBSal.Text) + Convert.ToInt32(txtTran.Text)).toString(); //txtTPay.Text = Convert.ToString((Convert.ToInt32(txtBSal.Text) + Convert.ToInt32(txtTran.Text) + Convert.ToInt32(txtHous.Text) + Convert.ToInt32(txtLunc.Text) + Convert.ToInt32(txtUti.Text) + Convert.ToInt32(txtFurn.Text) + Convert.ToInt32(txtHarz.Text) + Convert.ToInt32(txtEduc.Text) + Convert.ToInt32(txtHousM.Text) + Convert.ToInt32(txtDres.Text) + Convert.ToInt32(txtRisk.Text) + Convert.ToInt32(txtVeh.Text) + Convert.ToInt32(txtDriv.Text) + Convert.ToInt32(txtDome.Text) + Convert.ToInt32(txtOtherPay.Text))); ////TextBox3.Text = result.ToString(); } //public frmMain() //{ //} private void BntSave_Click(object sender, EventArgs e) { //if (txteid.Text != "" && txtStaf.Text != "") //{ // MessageBox.Show("Please enter valid data..."); //} //else if (textBox1.Text == "") { MessageBox.Show("Please enter valid data..."); } else { Image img = this.picStafPix.Image; byte[] bt; ImageConverter converter = new ImageConverter(); bt = (byte[])converter.ConvertTo(img, typeof(byte[])); SqlConnection con = new SqlConnection(); con.ConnectionString = "Data Source=AKINTOMIDE-HP\\SQLEXPRESS;Initial Catalog=fham;Integrated Security=True"; cmd = new SqlCommand("insert into master (eid,mon,yr,dat,staf,adrs,coy,loc,dpt,grd,datead,datemod,bsal,tranA,hous,lunc,uti,furn,harz,educ,housM,dres,risk,veh,driv,dome,pfs,nhf,nhfs,oded,housU,staffM,thrift,otherP,accno,bank,email,penadmin,pennum,status,Nhis,picstaffpix,photo,[browse]) values(@eid, @mon,@yr,@dat,@staf,@adrs,@coy,@loc,@dpt,@grd,@datead,@datemod,@bsal,@tran,@hous,@lunc,@uti,@furn,@harz,@educ,@housM,@dres,@risk,@veh,@driv,@dome,@pfs,@nhf,@nhfs,@oded,@housU,@staffM,@thrift,@otherP,@accno,@bank,@email,@penadmin,@pennum,@status,@Nhis,@picstaffpix,@photo, @BrowsePic )", con); con.Open(); // StaffParameter(); cmd.Parameters.AddWithValue("@eid", txteid.Text); cmd.Parameters.AddWithValue("@yr", cmbYr.Text); cmd.Parameters.AddWithValue("@dat", datAd.Text); cmd.Parameters.AddWithValue("@datead", datEmp.Text); cmd.Parameters.AddWithValue("@datemod", DatMod.Text); cmd.Parameters.AddWithValue("@staf", txtStaf.Text); cmd.Parameters.AddWithValue("@adrs", txtAdrs.Text); cmd.Parameters.AddWithValue("@coy", txtCoy.Text); cmd.Parameters.AddWithValue("@dpt", cmbDpt.Text); cmd.Parameters.AddWithValue("@loc", cmbLoc.Text); cmd.Parameters.AddWithValue("@grd", CmbGrd.Text); cmd.Parameters.AddWithValue("@accno", TxtAccno.Text); cmd.Parameters.AddWithValue("@email", txtemail.Text); cmd.Parameters.AddWithValue("@picstaffpix", bt); cmd.Parameters.AddWithValue("@bank", cmbBank.Text); cmd.Parameters.AddWithValue("@penadmin", cmbPenAdmin.Text); cmd.Parameters.AddWithValue("@status", cmbStatus.Text); cmd.Parameters.AddWithValue("@pennum", txtPenNum.Text); cmd.Parameters.AddWithValue("@bsal", txtBSal.Text); cmd.Parameters.AddWithValue("@dres", txtDres.Text); cmd.Parameters.AddWithValue("@Nhis", txtNhis.Text); cmd.Parameters.AddWithValue("@educ", txtEduc.Text); cmd.Parameters.AddWithValue("@furn", txtFurn.Text); cmd.Parameters.AddWithValue("@harz", txtHarz.Text); cmd.Parameters.AddWithValue("@tran", txtTran.Text); cmd.Parameters.AddWithValue("@hous", txtHous.Text); cmd.Parameters.AddWithValue("@lunc", txtLunc.Text); cmd.Parameters.AddWithValue("@uti", txtUti.Text); cmd.Parameters.AddWithValue("@housM", txtHousM.Text); cmd.Parameters.AddWithValue("@risk", txtRisk.Text); cmd.Parameters.AddWithValue("@veh", txtVeh.Text); cmd.Parameters.AddWithValue("@pfs", txtPfS.Text); cmd.Parameters.AddWithValue("@nhf", txtNHF.Text); cmd.Parameters.AddWithValue("@nhfs", txtNHFS.Text); cmd.Parameters.AddWithValue("@oded", txtOded.Text); cmd.Parameters.AddWithValue("@driv", txtDriv.Text); cmd.Parameters.AddWithValue("@dome", txtDome.Text); cmd.Parameters.AddWithValue("@housU", this.txtHousU.Text); cmd.Parameters.AddWithValue("@staffM", this.txtStaffM.Text); cmd.Parameters.AddWithValue("@thrift", this.txtThrift.Text); cmd.Parameters.AddWithValue("@mon", cmbMon.Text); cmd.Parameters.AddWithValue("@otherP", this.txtOtherPay.Text); cmd.Parameters.AddWithValue("@photo", this.textBox1.Text); cmd.Parameters.AddWithValue("@BrowsePic", SqlDbType.NVarChar).Value = button1.Text; cmd.Connection = con; try { int k = cmd.ExecuteNonQuery(); MessageBox.Show("Saved..."); ClearData();// Method } catch (Exception ex) { MessageBox.Show("Not Save:\n" + ex.Message); } finally { con.Close(); } } } private void btnUpdate_Click(object sender, EventArgs e) { //if (txteid.Text != "" && txtStaf.Text != "") //{ // MessageBox.Show("Please enter valid data..."); //} //else if (textBox1.Text == "") { MessageBox.Show("Please enter valid data..."); } else { Image img = this.picStafPix.Image; byte[] bt; ImageConverter converter = new ImageConverter(); bt = (byte[])converter.ConvertTo(img, typeof(byte[])); SqlDataAdapter adapt = new SqlDataAdapter(); SqlConnection con = new SqlConnection(); con.ConnectionString = "Data Source=AKINTOMIDE-HP\\SQLEXPRESS;Initial Catalog=fham;Integrated Security=True"; cmd = new SqlCommand("update master set mon=@Mon , yr=@Yr, dat=@dat,staf=@Staf,adrs=@Adrs,coy=@Coy,loc=@Loc,dpt=@Dpt,grd= @Grd,datead=@datead,datemod=@datemod,bsal=@BSal,tranA=@Tran,hous=@Hous,lunc=@Lunc,uti=@Uti,furn=@Furn,harz=@Harz,educ=@Educ,housM=@HousM,dres=@Dres,risk=@Risk,veh=@Veh,driv=@Driv,dome=@Dome,pfs=@PfS,nhf=@NHF,nhfs=@NHFS,oded=@Oded,housU=@HousU,staffM=@StaffM,thrift=@Thrift,accno=@Accno,bank= @Bank,email=@email,penadmin=@PenAdmin,pennum=@PenNum,status=@Status,Nhis=@Nhis,picstaffpix=@picstaffpix,[browse]=@BrowsePic,photo=@photo WHERE eid= @eid ", con); con.Open(); // StaffParameter(); cmd.Parameters.AddWithValue("@eid", txteid.Text); cmd.Parameters.AddWithValue("@yr", cmbYr.Text); cmd.Parameters.AddWithValue("@dat", datAd.Text); cmd.Parameters.AddWithValue("@datead", datEmp.Text); cmd.Parameters.AddWithValue("@datemod", DatMod.Text); cmd.Parameters.AddWithValue("@staf", txtStaf.Text); cmd.Parameters.AddWithValue("@adrs", txtAdrs.Text); cmd.Parameters.AddWithValue("@coy", txtCoy.Text); cmd.Parameters.AddWithValue("@dpt", cmbDpt.Text); cmd.Parameters.AddWithValue("@loc", cmbLoc.Text); cmd.Parameters.AddWithValue("@grd", CmbGrd.Text); cmd.Parameters.AddWithValue("@accno", TxtAccno.Text); cmd.Parameters.AddWithValue("@email", txtemail.Text); cmd.Parameters.AddWithValue("@picstaffpix", bt); cmd.Parameters.AddWithValue("@bank", cmbBank.Text); cmd.Parameters.AddWithValue("@penadmin", cmbPenAdmin.Text); cmd.Parameters.AddWithValue("@status", cmbStatus.Text); cmd.Parameters.AddWithValue("@pennum", txtPenNum.Text); cmd.Parameters.AddWithValue("@bsal", txtBSal.Text); cmd.Parameters.AddWithValue("@dres", txtDres.Text); cmd.Parameters.AddWithValue("@Nhis", txtNhis.Text); cmd.Parameters.AddWithValue("@educ", txtEduc.Text); cmd.Parameters.AddWithValue("@furn", txtFurn.Text); cmd.Parameters.AddWithValue("@harz", txtHarz.Text); cmd.Parameters.AddWithValue("@tran", txtTran.Text); cmd.Parameters.AddWithValue("@hous", txtHous.Text); cmd.Parameters.AddWithValue("@lunc", txtLunc.Text); cmd.Parameters.AddWithValue("@uti", txtUti.Text); cmd.Parameters.AddWithValue("@housM", txtHousM.Text); cmd.Parameters.AddWithValue("@risk", txtRisk.Text); cmd.Parameters.AddWithValue("@veh", txtVeh.Text); cmd.Parameters.AddWithValue("@pfs", txtPfS.Text); cmd.Parameters.AddWithValue("@nhf", txtNHF.Text); cmd.Parameters.AddWithValue("@nhfs", txtNHFS.Text); cmd.Parameters.AddWithValue("@oded", txtOded.Text); cmd.Parameters.AddWithValue("@driv", txtDriv.Text); cmd.Parameters.AddWithValue("@dome", txtDome.Text); cmd.Parameters.AddWithValue("@housU", this.txtHousU.Text); cmd.Parameters.AddWithValue("@staffM", this.txtStaffM.Text); cmd.Parameters.AddWithValue("@thrift", this.txtThrift.Text); cmd.Parameters.AddWithValue("@mon", cmbMon.Text); cmd.Parameters.AddWithValue("@otherP", this.txtOtherPay.Text); cmd.Parameters.AddWithValue("@photo", this.textBox1.Text); cmd.Parameters.AddWithValue("@BrowsePic", SqlDbType.NVarChar).Value = button1.Text; adapt.UpdateCommand = cmd; cmd.Connection = con; try { int k = cmd.ExecuteNonQuery(); MessageBox.Show("Updated..."); ClearData();// Method //textBox1.Text = ""; } catch (Exception ex) { MessageBox.Show("Not Updated:\n" + ex.Message); } finally { con.Close(); } } } private void btnSearch_Click(object sender, EventArgs e) { // if (txteid.Text != "" && txtStaf.Text != "") //{ // MessageBox.Show("Please enter valid data..."); //} //else //if (ID_textBox1.Text.Trim().Length > 0) //{ // try // { // query = "SELECT ProductName,ProductDescription,SellPrice FROM Table2 WHERE ProductID=@ProductID"; // using (SqlConnection Conn = CreateConnection.create_connection()) // { // // NOTE: If CreateConnection.create_connection() does not return // // an opened connection, you will need to open it like this: // // Conn.Open(); // SqlCommand cd = new SqlCommand(query, Conn); // cd.Parameters.AddWithValue("@ProductID", ID_textBox1.Text); // using (SqlDataReader reader = cd.ExecuteReader()) // { // while (reader.Read()) // { // Name_textBox2.Text = reader["ProductName"].ToString(); // Description_textBox3.Text = reader["ProductDescription"].ToString(); // Unit_Price_textBox5.Text = reader["SellPrice"].ToString(); // } // } // } // decimal quantity; // decimal unitPrice; // QTY_textBox4.Text = "1"; // decimal.TryParse(QTY_textBox4.Text, out quantity); // decimal.TryParse(Unit_Price_textBox5.Text, unitPrice); // Price_textBox6.Text = (quantity * unitPrice).ToString(); // } // catch (Exception ex) // { // MessageBox.Show(ex.Message); // } //} if (textBox1.Text == "") { } else { Image img = this.picStafPix.Image; byte[] bt; ImageConverter converter = new ImageConverter(); bt = (byte[])converter.ConvertTo(img, typeof(byte[])); SqlConnection con = new SqlConnection(); con.ConnectionString = "Data Source=AKINTOMIDE-HP\\SQLEXPRESS;Initial Catalog=fham;Integrated Security=True"; cmd = new SqlCommand("select * from master where eid =@eid", con); con.Open(); cmd.Parameters.AddWithValue("@eid", txteid.Text); //cmd.Parameters.AddWithValue("@picstaffpix", bt); cmd.Connection = con; try { dreader = cmd.ExecuteReader(); if (dreader.Read()) //if (textBox1.Text == "") //{ } //else //{ // try // } // Image img = this.picStafPix.Image; // byte[] bt; // ImageConverter converter = new ImageConverter(); // bt = (byte[])converter.ConvertTo(img, typeof(byte[])); // query = new SqlCommand("select * from master where staf= '%' + @staf '%'", con); // // cmd.Parameters.AddWithValue("@eid", txteid.Text); // con.ConnectionString = "Data Source=AKINTOMIDE-HP\\SQLEXPRESS;Initial Catalog=fham;Integrated Security=True"; // // using (SqlConnection Conn = CreateConnection.create_connection()) //// { // using ( SqlConnection con = new SqlConnection()); // con.Open(); // //SqlConnection connection = new SqlConnection("put your connectionstring here"); // SqlCommand cmd = new SqlCommand(query, Con); // cmd.Parameters.AddWithValue("@staf", txtStaf.Text); // using (SqlDataReader dreader = cmd.ExecuteReader()) // { // //cmd.Connection = con; // //try // //{ // ////SqlDataReader dataReader = command.ExecuteReader(); // //SqlDataReader dreader = cmd.ExecuteReader(); // while (dreader.Read()) { //txtname.Text = dreader[1].ToString(); // textboxName.Text = dataReader["name"].ToString(); txteid.Text = dreader[" eid"].ToString(); cmbYr.Text = dreader["Yr"].ToString(); datAd.Text = dreader["Dat"].ToString(); datEmp.Text = dreader["Datead"].ToString(); DatMod.Text = dreader["DateMod"].ToString(); txtStaf.Text = dreader["staf"].ToString(); txtAdrs.Text = dreader["Adrs"].ToString(); txtCoy.Text = dreader["coy"].ToString(); cmbDpt.Text = dreader["dpt"].ToString(); cmbLoc.Text = dreader["loc"].ToString(); CmbGrd.Text = dreader["Grd"].ToString(); TxtAccno.Text = dreader["accno"].ToString(); txtemail.Text = dreader["email"].ToString(); // bt=dreader[14].ToString(); cmbBank.Text = dreader["bank"].ToString(); cmbPenAdmin.Text = dreader["penadmin"].ToString(); cmbStatus.Text = dreader["status"].ToString(); txtPenNum.Text = dreader["pennum"].ToString(); txtBSal.Text = dreader["bsal"].ToString(); txtDres.Text = dreader["dres"].ToString(); txtNhis.Text = dreader["nhis"].ToString(); txtEduc.Text = dreader["educ"].ToString(); txtFurn.Text = dreader["furn"].ToString(); txtHarz.Text = dreader["harz"].ToString(); txtTran.Text = dreader["tranA"].ToString(); txtHous.Text = dreader["hous"].ToString(); txtLunc.Text = dreader["lunc"].ToString(); txtUti.Text = dreader["uti"].ToString(); txtHousM.Text = dreader["housM"].ToString(); txtRisk.Text = dreader["risk"].ToString(); txtVeh.Text = dreader["veh"].ToString(); txtPfS.Text = dreader["pfs"].ToString(); txtNHF.Text = dreader["nhf"].ToString(); txtNHFS.Text = dreader["NHFS"].ToString(); txtOded.Text = dreader["oded"].ToString(); txtDriv.Text = dreader["driv"].ToString(); txtDome.Text = dreader["dome"].ToString(); txtHousU.Text = dreader["housU"].ToString(); txtStaffM.Text = dreader["staffM"].ToString(); txtThrift.Text = dreader["Thrift"].ToString(); cmbMon.Text = dreader["mon"].ToString(); txtOtherPay.Text = dreader["otherpay"].ToString(); textBox1.Text = dreader["photo"].ToString(); button1.Text = dreader["browse"].ToString(); //txtname.Text = dreader[1].ToString(); // } // } // } //} // string eid; // int Yr; // datetime Dat; // datetime Datead; // datetime DateMod; // string staf; // string Adrs; // string coy; // string dpt; // string loc; // string Grd; // int accno ; // string email; // // bt=dreader[14].ToString(); // string bank; // string penadmin; // string status; // decimal pennum; // decimal bsal; // decimal dres; // decimal nhis; // decimal educ; // decimal furn; // decimal harz; // decimal tranA; // decimal hous ; // decimal lunc; // decimal uti; // decimal housM ; // decimal risk ; // decimal veh; // decimal pfs ; // decimal nhf ; // decimal Nhfs; // decimal oded; // decimal driv; // decimal dome; // decimal housU; // decimal staffM ; // float Thrift; // string mon; //float otherpay; // struct photo ; // struct browse; // decimal.TryParse(txtbsal.text, bsal); // decimal quantity; // decimal unitPrice; // QTY_textBox4.Text = "1"; // decimal.TryParse(QTY_textBox4.Text, out quantity); // decimal.TryParse(Unit_Price_textBox5.Text, unitPrice); // Price_textBox6.Text = (quantity * unitPrice).ToString(); //while (dreader.Read()) //{ // //txtname.Text = dreader[1].ToString(); // // textboxName.Text = dataReader["name"].ToString(); // txteid.Text = dreader[" eid"].ToString(); // cmbYr.Text = dreader["Yr"].ToString(); // datAd.Text = dreader["Dat"].ToString(); // datEmp.Text = dreader["Datead"].ToString(); // DatMod.Text = dreader["DateMod"].ToString(); // txtStaf.Text = dreader["staf"].ToString(); // txtAdrs.Text = dreader["Adrs"].ToString(); // txtCoy.Text = dreader["coy"].ToString(); // cmbDpt.Text = dreader["dpt"].ToString(); // cmbLoc.Text = dreader["loc"].ToString(); // CmbGrd.Text = dreader["Grd"].ToString(); // TxtAccno.Text = dreader["accno"].ToString(); // txtemail.Text = dreader["email"].ToString(); // // bt=dreader[14].ToString(); // cmbBank.Text = dreader["bank"].ToString(); // cmbPenAdmin.Text = dreader["penadmin"].ToString(); // cmbStatus.Text = dreader["status"].ToString(); // txtPenNum.Text = dreader["pennum"].ToString(); // txtBSal.Text = dreader["bsal"].ToString(); // txtDres.Text = dreader["dres"].ToString(); // txtNhis.Text = dreader["nhis"].ToString(); // txtEduc.Text = dreader["educ"].ToString(); // txtFurn.Text = dreader["furn"].ToString(); // txtHarz.Text = dreader["harz"].ToString(); // txtTran.Text = dreader["tranA"].ToString(); // txtHous.Text = dreader["hous"].ToString(); // txtLunc.Text = dreader["lunc"].ToString(); // txtUti.Text = dreader["uti"].ToString(); // txtHousM.Text = dreader["housM"].ToString(); // txtRisk.Text = dreader["risk"].ToString(); // txtVeh.Text = dreader["veh"].ToString(); // txtPfS.Text = dreader["pfs"].ToString(); // txtNHF.Text = dreader["nhf"].ToString(); // txtNHFS.Text = dreader["NHFS"].ToString(); // txtOded.Text = dreader["oded"].ToString(); // txtDriv.Text = dreader["driv"].ToString(); // txtDome.Text = dreader["dome"].ToString(); // txtHousU.Text = dreader["housU"].ToString(); // txtStaffM.Text = dreader["staffM"].ToString(); // txtThrift.Text = dreader["Thrift"].ToString(); // cmbMon.Text = dreader["mon"].ToString(); // txtOtherPay.Text = dreader["otherpay"].ToString(); // textBox1.Text = dreader["photo"].ToString(); // button1.Text = dreader["browse"].ToString(); //txtname.Text = dreader[1].ToString(); // //First of all, import the sqlClient library : //using System.Data.SqlClient; //Then make a connection and stuff : //SqlConnection connection = new SqlConnection("put your connectionstring here"); //Make a command : //SqlCommand command = new SqlCommand("select * from student where name= '%' + @Name '%'", connection); //declare the parameter of the sql query : //command.Parameters.Add(new SqlParameter("@Name", textboxSearch.Text)); //then create a dataReader to execute : //SqlDataReader dataReader = command.ExecuteReader(); //Then retrieve the data : //while (dataReader.Read()) //{ //textboxName.Text = dataReader["name"].ToString(); //textboxAge.Text = dataReader["age"].ToString(); //... //} //txtage.Text = dreader[2].ToString(); //txtcourse.Text = dreader[3].ToString(); ClearData(); } else { MessageBox.Show(" No Record"); } dreader.Close(); } catch (Exception x) { MessageBox.Show("No Record:\n" + x.Message); } finally { con.Close(); } } } private void btnDelete_Click(object sender, EventArgs e) { if (textBox1.Text == "") { MessageBox.Show("Please enter valid data..."); } else { //FileStream fs = new FileStream(textBox1.Text, FileMode.OpenOrCreate, FileAccess.Read); //byte[] bt = new byte[fs.Length]; //fs.Read(bt, 0, System.Convert.ToInt32(fs.Length)); //fs.Close(); Image img = this.picStafPix.Image; byte[] bt; ImageConverter converter = new ImageConverter(); bt = (byte[])converter.ConvertTo(img, typeof(byte[])); // System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(); //cmd.CommandType = System.Data.CommandType.Text; //SqlDataAdapter adapt = new SqlDataAdapter(); SqlConnection con = new SqlConnection(); con.ConnectionString = "Data Source=AKINTOMIDE-HP\\SQLEXPRESS;Initial Catalog=fham;Integrated Security=True"; //SqlCommand cmd = new SqlCommand("delete master where txteid.Text =@eid", con); cmd = new SqlCommand("delete master where eid =@eid", con); con.Open(); //StaffParameter(); cmd.Parameters.AddWithValue("@eid", txteid.Text); cmd.Parameters.AddWithValue("@yr", cmbYr.Text); cmd.Parameters.AddWithValue("@dat", datAd.Text); cmd.Parameters.AddWithValue("@datead", datEmp.Text); cmd.Parameters.AddWithValue("@datemod", DatMod.Text); cmd.Parameters.AddWithValue("@staf", txtStaf.Text); cmd.Parameters.AddWithValue("@adrs", txtAdrs.Text); cmd.Parameters.AddWithValue("@coy", txtCoy.Text); cmd.Parameters.AddWithValue("@dpt", cmbDpt.Text); cmd.Parameters.AddWithValue("@loc", cmbLoc.Text); cmd.Parameters.AddWithValue("@grd", CmbGrd.Text); cmd.Parameters.AddWithValue("@accno", TxtAccno.Text); cmd.Parameters.AddWithValue("@email", txtemail.Text); cmd.Parameters.AddWithValue("@picstaffpix", bt); cmd.Parameters.AddWithValue("@bank", cmbBank.Text); cmd.Parameters.AddWithValue("@penadmin", cmbPenAdmin.Text); cmd.Parameters.AddWithValue("@status", cmbStatus.Text); cmd.Parameters.AddWithValue("@pennum", txtPenNum.Text); cmd.Parameters.AddWithValue("@bsal", txtBSal.Text); cmd.Parameters.AddWithValue("@dres", txtDres.Text); cmd.Parameters.AddWithValue("@Nhis", txtNhis.Text); cmd.Parameters.AddWithValue("@educ", txtEduc.Text); cmd.Parameters.AddWithValue("@furn", txtFurn.Text); cmd.Parameters.AddWithValue("@harz", txtHarz.Text); cmd.Parameters.AddWithValue("@tran", txtTran.Text); cmd.Parameters.AddWithValue("@hous", txtHous.Text); cmd.Parameters.AddWithValue("@lunc", txtLunc.Text); cmd.Parameters.AddWithValue("@uti", txtUti.Text); cmd.Parameters.AddWithValue("@housM", txtHousM.Text); cmd.Parameters.AddWithValue("@risk", txtRisk.Text); cmd.Parameters.AddWithValue("@veh", txtVeh.Text); cmd.Parameters.AddWithValue("@pfs", txtPfS.Text); cmd.Parameters.AddWithValue("@nhf", txtNHF.Text); cmd.Parameters.AddWithValue("@nhfs", txtNHFS.Text); cmd.Parameters.AddWithValue("@oded", txtOded.Text); cmd.Parameters.AddWithValue("@driv", txtDriv.Text); cmd.Parameters.AddWithValue("@dome", txtDome.Text); cmd.Parameters.AddWithValue("@housU", this.txtHousU.Text); cmd.Parameters.AddWithValue("@staffM", this.txtStaffM.Text); cmd.Parameters.AddWithValue("@thrift", this.txtThrift.Text); cmd.Parameters.AddWithValue("@mon", cmbMon.Text); cmd.Parameters.AddWithValue("@otherP", this.txtOtherPay.Text); cmd.Parameters.AddWithValue("@photo", this.textBox1.Text); cmd.Parameters.AddWithValue("@BrowsePic", SqlDbType.NVarChar).Value = button1.Text; cmd.Connection = con; try { int k = cmd.ExecuteNonQuery(); MessageBox.Show("Deleted..."); textBox1.Text = ""; } catch (Exception ex) { MessageBox.Show("Not Deleted:\n" + ex.Message); } finally { con.Close(); } } } private void DisplayData() { } // Clear Data private void ClearData() { txteid.Text = "FHA136";//1 cmbYr.Text = DateTime.Now.Year.ToString(); datAd.Text = DateTime.Now.ToString("dd/MM/yyyy"); datEmp.Text = DateTime.Now.ToString("dd/MM/yyyy"); DatMod.Text = DateTime.Now.ToString("dd/MM/yyyy"); //datAd.Text = DateTime.Today; //datEmp.Text = DateTime.Today; //DatMod.Text = DateTime.Today; txtStaf.Text = "Akintomide Ayodele"; txtAdrs.Text = "39 Durban Street, Wuse II, Abuja"; txtCoy.Text = "FHA Homes Ltd"; cmbDpt.Text = "Operations"; cmbLoc.Text = "Abuja"; CmbGrd.Text = "Banking Officer"; TxtAccno.Text = "11234567"; txtemail.Text = "info@fhamortgage.gov.ng"; cmbBank.Text = "United Bank of Africa"; cmbPenAdmin.Text = "Crusader"; cmbStatus.Text = "staff"; txtPenNum.Text = "pen1234"; txtBSal.Text = "20000"; txtDres.Text = "20000"; txtNhis.Text = "10000"; txtEduc.Text = "10000"; txtFurn.Text = "25000"; txtHarz.Text = "20000"; txtTran.Text = "15000"; txtHous.Text = "30000"; txtLunc.Text = "10000"; txtUti.Text = "10000"; txtHousM.Text = "10000"; txtRisk.Text = "20000"; txtVeh.Text = "10000"; txtPfS.Text = "10000"; txtNHF.Text = "10000"; txtNHFS.Text = "10000"; txtOded.Text = "10000"; txtDriv.Text = "10000"; txtDome.Text = "20000"; txtHousU.Text = "10000"; txtStaffM.Text = "20000"; txtThrift.Text = "20000"; cmbMon.Text = DateTime.Now.ToString("MMMM"); this.txtOtherPay.Text = "15000"; textBox1.Text = ""; button1.Text = ""; SN = 0; } //New Record private void New_Rec() { txteid.Text = "";//1 cmbYr.Text = DateTime.Now.Year.ToString(); datAd.Text = DateTime.Now.ToString("dd/MM/yyyy"); datEmp.Text = DateTime.Now.ToString("dd/MM/yyyy"); DatMod.Text = DateTime.Now.ToString("dd/MM/yyyy"); txtStaf.Text = ""; txtAdrs.Text = "39 Durban Street, Wuse II, Abuja"; txtCoy.Text = "FHA Homes Ltd"; cmbDpt.Text = "Operations"; cmbLoc.Text = "Abuja"; CmbGrd.Text = "Banking Officer"; TxtAccno.Text = "11234567"; txtemail.Text = "info@fhamortgage.gov.ng"; cmbBank.Text = "United Bank of Africa"; cmbPenAdmin.Text = "Crusader"; cmbStatus.Text = "staff"; txtPenNum.Text = "pen1234"; txtBSal.Text = "0"; txtDres.Text = "0"; txtNhis.Text = "0"; txtEduc.Text = "0"; txtFurn.Text = "0"; txtHarz.Text = "0"; txtTran.Text = "0"; txtHous.Text = "0"; txtLunc.Text = "0"; txtUti.Text = "0"; txtHousM.Text = "0"; txtRisk.Text = "0"; txtVeh.Text = "0"; txtPfS.Text = "0"; txtNHF.Text = "0"; txtNHFS.Text = "0"; txtOded.Text = "0"; txtDriv.Text = "0"; txtDome.Text = "0"; txtHousU.Text = "0"; txtStaffM.Text = "0"; txtThrift.Text = "0"; cmbMon.Text = DateTime.Now.ToString("MMMM"); this.txtOtherPay.Text = "0"; textBox1.Text = ""; button1.Text = ""; SN = 0; } private void Add_Employee_Load(object sender, EventArgs e) { { if (con.State == ConnectionState.Closed) { con.Open(); } string sqlLoc = "SELECT * FROM Location"; var lAdapter = new SqlDataAdapter(sqlLoc, con); var lset = new DataSet(); lAdapter.TableMappings.Add("Table", "Location"); lAdapter.Fill(lset); this.dviewmanager = lset.DefaultViewManager; try { for (int i = 0; i <= lset.Tables[0].Rows.Count - 1; i++) { this.cmbLoc.Items.Add(lset.Tables[0].Rows[i]["Location"].ToString()); } } catch (Exception ex) { MessageBox.Show("Not successful:\n" + ex.Message); } this.cmbMon.Items.Add("January"); this.cmbMon.Items.Add("February"); this.cmbMon.Items.Add("March"); this.cmbMon.Items.Add("April"); this.cmbMon.Items.Add("May"); this.cmbMon.Items.Add("June"); this.cmbMon.Items.Add("July"); this.cmbMon.Items.Add("August"); this.cmbMon.Items.Add("September"); this.cmbMon.Items.Add("October"); this.cmbMon.Items.Add("November"); this.cmbMon.Items.Add("December"); } } protected void FillBank() { if (con.State == ConnectionState.Closed) { con.Open(); } string sqlStr = "SELECT * FROM BANK"; var dAdapter = new SqlDataAdapter(sqlStr, con); var dset = new DataSet(); dAdapter.TableMappings.Add("Table", "Bank"); dAdapter.Fill(dset); this.dviewmanager = dset.DefaultViewManager; try { for (int i = 0; i <= dset.Tables[0].Rows.Count - 1; i++) { this.cmbBank.Items.Add(dset.Tables[0].Rows[i]["Bank"].ToString()); } } catch (Exception ex) { //Exception Message MessageBox.Show("Not successful:\n" + ex.Message); } } protected void FillDpt() { if (con.State == ConnectionState.Closed) { con.Open(); } string sqlStr = "SELECT * FROM Dpt"; var dAdapter = new SqlDataAdapter(sqlStr, con); var dset = new DataSet(); dAdapter.TableMappings.Add("Table", "Dpt"); dAdapter.Fill(dset); this.dviewmanager = dset.DefaultViewManager; try { for (int i = 0; i <= dset.Tables[0].Rows.Count - 1; i++) { this.cmbDpt.Items.Add(dset.Tables[0].Rows[i]["Dpt"].ToString()); } } catch (Exception ex) { //Exception Message MessageBox.Show("Not successful:\n" + ex.Message); } } protected void FillYear() { if (con.State == ConnectionState.Closed) { con.Open(); } string sqlStr = "SELECT * FROM Year"; var dAdapter = new SqlDataAdapter(sqlStr, con); var dset = new DataSet(); dAdapter.TableMappings.Add("Table", "Year"); dAdapter.Fill(dset); this.dviewmanager = dset.DefaultViewManager; try { for (int i = 0; i <= dset.Tables[0].Rows.Count - 1; i++) { this.cmbYr.Items.Add(dset.Tables[0].Rows[i]["Year"].ToString()); } } catch (Exception ex) { //Exception Message MessageBox.Show("Not successful:\n" + ex.Message); } } protected void FillGrade() { if (con.State == ConnectionState.Closed) { con.Open(); } string sqlStr = "SELECT * FROM Grade"; var dAdapter = new SqlDataAdapter(sqlStr, con); var dset = new DataSet(); dAdapter.TableMappings.Add("Table", "Grade"); dAdapter.Fill(dset); this.dviewmanager = dset.DefaultViewManager; try { for (int i = 0; i <= dset.Tables[0].Rows.Count - 1; i++) { this.CmbGrd.Items.Add(dset.Tables[0].Rows[i]["Grade"].ToString()); } } catch (Exception ex) { //Exception Message MessageBox.Show("Not successful:\n" + ex.Message); } } protected void FillStatus() { if (con.State == ConnectionState.Closed) { con.Open(); } string sqlStr = "SELECT * FROM Status"; var dAdapter = new SqlDataAdapter(sqlStr, con); var dset = new DataSet(); dAdapter.TableMappings.Add("Table", "Status"); dAdapter.Fill(dset); this.dviewmanager = dset.DefaultViewManager; try { for (int i = 0; i <= dset.Tables[0].Rows.Count - 1; i++) { this.cmbStatus.Items.Add(dset.Tables[0].Rows[i]["Status"].ToString()); } } catch (Exception ex) { //Exception Message MessageBox.Show("Not successful:\n" + ex.Message); } } protected void FillPenAdmin() { if (con.State == ConnectionState.Closed) { con.Open(); } string sqlStr = "SELECT * FROM PenAdmin"; var dAdapter = new SqlDataAdapter(sqlStr, con); var dset = new DataSet(); dAdapter.TableMappings.Add("Table", "PenAdmin"); dAdapter.Fill(dset); this.dviewmanager = dset.DefaultViewManager; try { for (int i = 0; i <= dset.Tables[0].Rows.Count - 1; i++) { this.cmbPenAdmin.Items.Add(dset.Tables[0].Rows[i]["PenAdmin"].ToString()); } } catch (Exception ex) { //Exception Message } } private void groupBox1_Enter(object sender, EventArgs e) { } private void label14_Click(object sender, EventArgs e) { } private void frmTpay_Enter(object sender, EventArgs e) { } private void txtHousU_TextChanged(object sender, EventArgs e) { } private void label33_Click(object sender, EventArgs e) { } private void txtStaffM_TextChanged(object sender, EventArgs e) { } private void txtPAYE_TextChanged(object sender, EventArgs e) { } private void datEmp_ValueChanged(object sender, EventArgs e) { //// Set title bar to selected date. //DateTime result = dateTimePicker1.Value; //this.Text = result.ToString(); } private void btnClose_Click(object sender, EventArgs e) { this.Dispose(); } private void txtTran_TextChanged(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { //SaveImage(); } private void btnNew_Click(object sender, EventArgs e) { New_Rec(); } private void groupBox1_Enter_1(object sender, EventArgs e) { } } }
Simon_Whale
Как и предложил Грифф, я бы поставил точку останова в вашем коде. Затем скопируйте и пропустите инструкцию SQL, которую вы создаете, и попробуйте ее непосредственно в студии SQL Manangement.
Aydotcom
Привет Саймон,
Я изучал отладчик и практиковался, как предлагалось, а также копировал и вставлял, но все еще не мог обнаружить ошибку. Пожалуйста, помогите мне дальше
Simon_Whale
когда вы попробовали SQL direct в management studio, он вернул правильные данные?