Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Insert a Record

Insert a Record

Scheduled Pinned Locked Moved C#
csharphelpgraphicsquestionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mwith
    wrote on last edited by
    #1

    Hi all, I am a beginner for C# and trying to study about ADO.NET. I wrote a simple application with a datagridview and three text boxes. I can populate data from table. Now I am trying to insert a record to a table. When I execute the following code, it is updating that table only a blank line. please can you help me to solve this problem? Or any other ways to update a record better than this? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace FundOrg { public partial class frmBankDetails : Form { private DataSet dsBank; private SqlDataAdapter da = new SqlDataAdapter(); SqlConnection myConnection = new SqlConnection(); private static frmBankDetails frmB = null; public static frmBankDetails Instance() { if (frmB == null) { frmB = new frmBankDetails(); } return frmB; } public frmBankDetails() { InitializeComponent(); myConnection.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["dbcon"]; try { myConnection.Open(); } catch (SqlException E) { MessageBox.Show("Connection Erro : " + E.Message); } SqlCommand cmd = new SqlCommand("SELECT * FROM Bank_Details", myConnection); // BindingSource bsBank = new BindingSource(); da.SelectCommand = cmd; dsBank=new DataSet(); da.Fill(dsBank,"Bank_Details"); dgBankCode.DataSource = dsBank; dgBankCode.DataMember = "Bank_Details"; dgBankCode.RowHeadersVisible = false; dgBankCode.Columns[0].HeaderText = "Bank Code"; dgBankCode.Columns[1].Visible = false; dgBankCode.Columns[2].Visible = false; txtBankCode.DataBindings.Add("Text", dsBank, "Bank_Details.Bank_Code"); txtBankName.DataBindings.Add("Text", dsBank, "Bank_Details.Bank_Name"); txtAccNo.DataBindings.Add("Text", dsBank, "Bank_Details.Acc_No"); dgBankCode.ReadOnly = true; } public void SaveEntry() { SqlCommand cmdUp

    L 1 Reply Last reply
    0
    • M mwith

      Hi all, I am a beginner for C# and trying to study about ADO.NET. I wrote a simple application with a datagridview and three text boxes. I can populate data from table. Now I am trying to insert a record to a table. When I execute the following code, it is updating that table only a blank line. please can you help me to solve this problem? Or any other ways to update a record better than this? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace FundOrg { public partial class frmBankDetails : Form { private DataSet dsBank; private SqlDataAdapter da = new SqlDataAdapter(); SqlConnection myConnection = new SqlConnection(); private static frmBankDetails frmB = null; public static frmBankDetails Instance() { if (frmB == null) { frmB = new frmBankDetails(); } return frmB; } public frmBankDetails() { InitializeComponent(); myConnection.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["dbcon"]; try { myConnection.Open(); } catch (SqlException E) { MessageBox.Show("Connection Erro : " + E.Message); } SqlCommand cmd = new SqlCommand("SELECT * FROM Bank_Details", myConnection); // BindingSource bsBank = new BindingSource(); da.SelectCommand = cmd; dsBank=new DataSet(); da.Fill(dsBank,"Bank_Details"); dgBankCode.DataSource = dsBank; dgBankCode.DataMember = "Bank_Details"; dgBankCode.RowHeadersVisible = false; dgBankCode.Columns[0].HeaderText = "Bank Code"; dgBankCode.Columns[1].Visible = false; dgBankCode.Columns[2].Visible = false; txtBankCode.DataBindings.Add("Text", dsBank, "Bank_Details.Bank_Code"); txtBankName.DataBindings.Add("Text", dsBank, "Bank_Details.Bank_Name"); txtAccNo.DataBindings.Add("Text", dsBank, "Bank_Details.Acc_No"); dgBankCode.ReadOnly = true; } public void SaveEntry() { SqlCommand cmdUp

      L Offline
      L Offline
      lost in transition
      wrote on last edited by
      #2

      MySQLString = "INSERT INTO Bank_Details(Bank_Code,Bank_Name,Acc_No)" +
      "VALUES('"+ txtBankCode.Text +"','"+ txtBankName.Text +"','"+ txtAccNo.Text +"')";

      SqlConnection MySqlConn = new SqlConnection(MyConnectionString);
      SqlCommand MySqlComm = new SqlCommand(MySQLString, MySqlConn);
      // MySqlConn and MySqlComm refer to variable and not the DBMS
      MySqlComm.Connection.Open();
      MySqlComm.ExecuteNonQuery();
      MySqlComm.Connection.Close();

      mwith wrote:

      And can anybody explain me, what are the good/bad coding practices with this?

      When working with DB try your best to keep it simple. Also, check the SQL statement if Bank_Code and Acc_No are 'int' data types then you need to convert the textboxes from string to int. i.e. Convert.ToInt32(txtBankCode.Text) Good Luck, Jason -- modified at 12:01 Thursday 11th January, 2007

      Programmer: A biological machine designed to convert caffeine into code. * Developer: A person who develops working systems by writing and using software. [^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups