Insert a Record
-
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
-
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
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. [^]