Afan Ali

Afan Ali

  • NA
  • 4
  • 747

Delete, Search And Update Records in ADO.NET

Feb 1 2017 1:32 PM
Hi.
I am facing problem in my update command. i am trying to update my database but getting "not updated" mesasge.Search and Delete commands are working properly. Please help me as soon as possible.
 
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;
namespace Std_Course
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
SqlCommand comm;
SqlDataReader dreader;
string connstring = "server=localhost;database=TestPhase2;Trusted_Connection=True";
/// <summary>
/// Update Button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Updatebutton_Click(object sender, EventArgs e)
{
conn = new SqlConnection(connstring);
conn.Open();
comm = new SqlCommand("update Std_Course set Course_Code= " + CCodetextBox.Text + " , Course_Name=' " + CNametextBox.Text + ",Cridet_Hours=' " + CHourstextBox.Text + "' where Std_roll_number = " + RNotextBox.Text + " ", conn);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("Updated..");
}
catch (Exception)
{
MessageBox.Show(" Not Updated");
}
finally
{
conn.Close();
}
}
/// <summary>
/// Search Button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Searchbutton_Click(object sender, EventArgs e)
{
conn = new SqlConnection(connstring);
conn.Open();
comm = new SqlCommand("select * from Std_Course where Std_roll_number = " + RNotextBox.Text + " ", conn);
try
{
dreader = comm.ExecuteReader();
if (dreader.Read())
{
CCodetextBox.Text = dreader[1].ToString();
CNametextBox.Text = dreader[2].ToString();
CHourstextBox.Text = dreader[3].ToString();
}
else
{
MessageBox.Show(" No Record");
}
dreader.Close();
}
catch (Exception)
{
MessageBox.Show(" No Record");
}
finally
{
conn.Close();
}
}
/// <summary>
/// Delete Button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Deletebutton_Click(object sender, EventArgs e)
{
conn = new SqlConnection(connstring);
conn.Open();
comm = new SqlCommand("delete from Std_Course where Std_roll_number = " + RNotextBox.Text + " ", conn);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("Deleted...");
CCodetextBox.Clear();
CNametextBox.Clear();
CHourstextBox.Clear();
RNotextBox.Clear();
RNotextBox.Focus();
}
catch (Exception x)
{
MessageBox.Show(" Not Deleted" + x.Message);
}
finally
{
conn.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
RNotextBox.Focus();
}
}
}

Attachment: Std_Course.rar

Answers (3)