Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
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 <<

C#
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
Updated 9-Jul-13 21:31pm
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...
berrymaria 10-Jul-13 3:38am    
:D

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

C#
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();
}
 
Share this answer
 
This is my project.

Download here >>

http://www.boyr.com/getfile.php?id=1395506&key=51dd0b5db1a17
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900