Skip to main content

Example of code of c sharp with SQL Server connectivity


 using System;  
  using System.Data;  
  using System.Text;  
  using System.Windows.Forms;  
  using System.Data.SqlClient;  
  using System.Drawing;  
  namespace DatabaseExp  
  {  
  public partial class DatabaseExp : Form  
  {  
  SqlConnection conn;  
  SqlDataAdapter adaptr;  
  SqlCommand cmd;  
  DataTable tbl;  
  static int count = 0, current = 0;  
  static int check = 0;  
  public DatabaseExp()  
  {  
  InitializeComponent();  
 }  
  private void DatabaseExp_Load(object sender, EventArgs e)  
  {  
  string connectionString = null;  
  adaptr = new SqlDataAdapter();  
  tbl = new DataTable();  
  connectionString = "Data Source=127.0.0.1;Initial Catalog=Abdul;User ID=sa;Password=allah";  
  conn = new SqlConnection(connectionString);  
  try  
  {  
  conn.Open();  
  MessageBox.Show("Connection Open !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  //conn.Close();  
  }  
  catch (Exception ex)  
  {  
  MessageBox.Show("Can not open connection ! ");  
  }  
  }  
 private void cmd_Add_Click(object sender, EventArgs e)  
  {  
  string sql;  
  sql = "Insert INTO Student VALUES('" + tRoll.Text + "','" + tName.Text + "','" + tAddress.Text + "')";  
  try  
  {  
  cmd = new SqlCommand(sql, conn);  
  cmd.ExecuteNonQuery();  
  MessageBox.Show("Record Inserted Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  cmd.Dispose();  
  Refresh();  
  }  
  catch (Exception ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  
  }  
 private void cmd_Refresh_Click(object sender, EventArgs e)  
  {  
  Refresh();  
  }  
 private void Refresh()  
  {  
  adaptr.Dispose();  
  tbl.Reset();  
  adaptr.SelectCommand = new SqlCommand("Select * from Student", conn);  
  adaptr.Fill(tbl);  
  count = tbl.Rows.Count;  
  current = 0;  
  if (count > 0)  
  Display(tbl);  
  else  
  MessageBox.Show("No Records In The Table", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
 }  
  private void Display(DataTable tbl)  
  {  
  tRoll.Text = tbl.Rows[current]["Roll Number"].ToString();  
  tName.Text = tbl.Rows[current]["Name"].ToString();  
  tAddress.Text = tbl.Rows[current]["Address"].ToString();  
  }  
 private void cmd_Clear_Click(object sender, EventArgs e)  
  {  
  tRoll.Text = "";  
  tName.Text = "";  
  tAddress.Text = "";  
  }  
 private void cmd_Delete_Click(object sender, EventArgs e)  
  {  
  if (MessageBox.Show("Do You Want to Delete This Record?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)  
  {  
  string sql;  
  string key = tRoll.Text.ToString();  
  sql = "Delete From Student Where [Roll Number]='" + key + "'";  
  try  
  {  
  cmd = new SqlCommand(sql, conn);  
  cmd.ExecuteNonQuery();  
  MessageBox.Show("Record Deleted Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  cmd.Dispose();  
  Refresh();  
  }  
  catch (Exception ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  
  }  
  }  
  private void cmd_Search_Click(object sender, EventArgs e)  
  {  
  string sql;  
  string nm = tName.Text.ToString();  
  sql = "Select * From Student Where [Name]='" + nm + "'";  
  adaptr.Dispose();  
  tbl.Reset();  
  adaptr.SelectCommand = new SqlCommand(sql, conn);  
  adaptr.Fill(tbl);  
  count = tbl.Rows.Count;  
  current = 0;  
  //MessageBox.Show(count + "");  
  if (count > 0)  
  Display(tbl);  
  else  
  MessageBox.Show("No Record In The Table", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  }  
 private void cmd_Previous_Click(object sender, EventArgs e)  
  {  
  if (current > 0)  
  {  
  current--;  
  Display(tbl);  
  }  
  else  
  {  
  current = count - 1;  
  Display(tbl);  
  }  
  }  
  private void cmd_Next_Click(object sender, EventArgs e)  
  {  
  if (current == count - 1)  
  {  
  current = 0;  
  Display(tbl);  
  }  
  else  
  {  
  current++;  
  Display(tbl);  
  }  
  }  
 private void cmd_Cancel_Click(object sender, EventArgs e)  
  {  
  if (check == 1)  
  {  
  Button_Status(true);  
  check = 0;  
  }  
  else  
  {  
  try  
  {  
  conn.Close();  
  this.Dispose();  
  }  
  catch (Exception ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  
  }  
  }  
 private void Button_Status(Boolean value)  
  {  
  cmd_Add.Enabled = value;  
  cmd_Refresh.Enabled = value;  
  cmd_Clear.Enabled = value;  
  cmd_Delete.Enabled = value;  
  cmd_Previous.Enabled = value;  
  cmd_Next.Enabled = value;  
 }  
  private void cmd_Edit_Click(object sender, EventArgs e)  
  {  
  check = 1;  
  Button_Status(false);  
  tRoll.Enabled = false;  
  }  
 private void cmd_Save_Click(object sender, EventArgs e)  
  {  
  string sql;  
  string key = tRoll.Text.ToString();  
  if (check == 1)  
  {  
  check = 0;  
  tRoll.Enabled = true;  
  sql = "Update Student Set [Name]='" + tName.Text + "',[Address]='" + tAddress.Text + "' Where [Roll Number]='" + key + "'";  
  try  
  {  
  cmd = new SqlCommand(sql, conn);  
  cmd.ExecuteNonQuery();  
  MessageBox.Show("Record Edited Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  cmd.Dispose();  
  Button_Status(true);  
  tbl.Reset();  
  Refresh();  
  }  
  catch (Exception ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  
  }  
  }  
 }  
  }  

Popular posts from this blog

Resolved : Power BI Report connection error during execution

Getting Below Power BI Report connection error during execution . Error: Something went wrong Unable to connect to the data source undefined. Please try again later or contact support. If you contact support, please provide these details. Underlying error code: -2147467259 Table: Business Sector. Underlying error message: AnalysisServices: A connection cannot be made. Ensure that the server is running. DM_ErrorDetailNameCode_UnderlyingHResult: -2147467259 Microsoft.Data.Mashup.ValueError.DataSourceKind: AnalysisServices Microsoft.Data.Mashup.ValueError.DataSourcePath: 10.10.10.60;T_CustomerMaster_ST Microsoft.Data.Mashup.ValueError.Reason: DataSource.Error Cluster URI: WABI-WEST-EUROPE-redirect.analysis.windows.net Activity ID: c72c4f12-8c27-475f-b576-a539dd81826a Request ID: dfb54166-c78f-4b40-779f-e8922a6687ad Time: 2019-09-26 10:03:29Z Solution: We found report connection not able to connect to SQL Analysis service so tried below option. Re

Song- Khamoshiyan Piano keyboard Chord,Notation and songs Lyrics

All songs notation and chords at one place

Song : O Saathi Re Film : Mukhathar Ka Sikkandhar Uses : C D D# E G A Note : The numbers at the end of the lines indicate line numbers. Pallavi: O saathi re, tere binaa bhi kya jina, tere binaa bhi kya jina A- C D D#....,D D C DD E...C..CA-...,D D C DD E...CC.......1 Play line 1 again phulon men khaliyon men sapnom ki galiyon men GGG...GAGE.. GGG G A G E.................................................2 tere bina kuchh kahin naa E A G E D C D D#.......................................................................3 tere binaa bhi kya jina, tere binaa bhi kya jina D D C DD E....C..CA-..., D D C DDE....CC.............................4 Charanam: har dhadkan men, pyaas hai teri, sanson men teri khushboo hai CCC C D C A-, CCC C D C A-, DDD DED CD EE.. CCCC......................5 is dharthi se, us ambar tak, meri nazar men tu hi tu hai CCC C D C A-, CCC C D C A-, DDD DED CD EE.. CCCC......................6 pyaar yeh tute naa GGG... GAG D#......E.........................