Click here to Skip to main content
12,065,354 members (42,541 online)
Rate this:
 
Please Sign up or sign in to vote.
These are my source code. I use Visual Studio 2010 and .NET Framework Version 4.0

I can not insert data into database.

>> Picture <<

<img border="0" src="http://image.free.in.th/v/2013/tt/626x1.jpg" alt="images by free.in.th"/>

<img border="0" src="http://image.free.in.th/v/2013/tp/o5fm2.jpg" alt="images by free.in.th"/>

<img border="0" src="http://image.free.in.th/v/2013/tm/ns8q3.jpg" alt="images by free.in.th" />


-----------------------------------------------------------------------------------------------


>> Source code <<

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 DataGridTest_WithNoDatabase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private SqlConnection connection;
        private SqlCommand command;
        private DataSet dataSt;
        private SqlDataAdapter adapter;
 
        private string sql;
 
        private void Form1_Load(object sender, EventArgs e)
        {
            //Connect Database Part
            /////////////////////////////////////////////////////////////////
            string conStr = @"Data Source=.\SQLEXPRESS; 
                            AttachDbFilename=|DataDirectory|\Order.mdf; 
                            Integrated Security=True; User Instance=True";
            connection = new SqlConnection(conStr);
            connection.Open();
            /////////////////////////////////////////////////////////////////

 
            sql = "SELECT * FROM Order_Table";
 
            command = new SqlCommand(sql, connection);
            adapter = new SqlDataAdapter(command);
 
            dataSt = new DataSet();
            adapter.Fill(dataSt, "Order_Table");
 
            dataGridView1.DataSource = dataSt.Tables["Order_Table"];
           
            string[] headers={"orderID", "Name", "Made In"};
            for (int i = 0; i < headers.Length; i++)
            {
                dataGridView1.Columns[i].HeaderText = headers[i];
            }
 
            dataGridView1.Rows[0].Height = 22;        
        }
 
        private void InsertData()
        {
            sql = @"INSERT INTO Order_Table(orderID, Name, madeIn) 
                           VALUES(@txtOrderID, @txtName, @txtMadeIn)";
 
            command = new SqlCommand(sql, connection);
            command.Parameters.AddWithValue("txtOrderID", Convert.ToInt32(txtOrderID.Text));
            command.Parameters.AddWithValue("txtName", txtName.Text);
            command.Parameters.AddWithValue("txtMadeIn", txtMadeIn.Text);
 
            int result = (int)command.ExecuteNonQuery();
            if (result != -1)
            {
                MessageBox.Show("Add Data Completed");
            }
            else
            {
                MessageBox.Show("Error ! !");
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            InsertData();
            connection.Close();
        }
    }
}
Posted 9-Jul-13 22:19pm
aptoon278
Edited 9-Jul-13 22:31pm
Johnny J.139.4K
v2
Comments
Pheonyx 10-Jul-13 3:23am
   
Do you get any exceptions being thrown?
aptoon 10-Jul-13 3:26am
   
I don't know sir.
Naz_Firdouse 10-Jul-13 3:31am
   
use try...catch and see whether you get any exceptions
Johnny J. 10-Jul-13 3:36am
   
I would recommend that you hire a programmer to fix the problem for you...
chimcham 10-Jul-13 3:38am
   
:D
Rohan Leuva 10-Jul-13 3:50am
   
How you come to know you didnt get the required result? Make breakpoint and try debugging to see whats going on.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

have made changes in the InsertData() function. Try this. Also add some exceptional handling and connection management.

private void InsertData()
{
    sql = "INSERT INTO Order_Table(orderID, Name, madeIn)
                   VALUES(@txtOrderID, @txtName, @txtMadeIn)";
 
string conStr = "Data Source=.\SQLEXPRESS;
                            AttachDbFilename=|DataDirectory|\Order.mdf;
                            Integrated Security=True; User Instance=True";
            connection = new SqlConnection(conStr);
            connection.Open();
 
    command = new SqlCommand(sql, connection);
    command.Parameters.AddWithValue(&quot;txtOrderID&quot;, Convert.ToInt32(txtOrderID.Text));
    command.Parameters.AddWithValue(&quot;txtName&quot;, txtName.Text);
    command.Parameters.AddWithValue(&quot;txtMadeIn&quot;, txtMadeIn.Text);
 
    int result = (int)command.ExecuteNonQuery();
    if (result != -1)
    {
        MessageBox.Show("Add Data Completed");
    }
    else
    {
        MessageBox.Show("Error ! !&");
    }
    connection.Close();
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This is my project.

Download here >>

http://www.boyr.com/getfile.php?id=1395506&key=51dd0b5db1a17
  Permalink  
Comments
H.Brydon 10-Jul-13 17:38pm
   
You should delete this "solution" which does not provide a solution. If you have more details for your question, then update it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160204.4 | Last Updated 10 Jul 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100