Click here to Skip to main content
15,901,708 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace StudensDetails
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'database1DataSet1.Table' table. You can move, or remove it, as needed.
            this.tableTableAdapter1.Fill(this.database1DataSet1.Table);
            // TODO: This line of code loads data into the 'database1DataSet.Table' table. You can move, or remove it, as needed.
            this.tableTableAdapter.Fill(this.database1DataSet.Table);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(global::StudensDetails.Properties.Settings.Default.Database1ConnectionString);
            try
            {
                string sql = "INSERT INTO [Table] (Student_ID,StudentName) values(" + textBox1.Text + ",N'" + textBox2.Text + "')";
                SqlCommand exeSql = new SqlCommand(sql, cn);
                cn.Open();
                exeSql.ExecuteNonQuery();

                MessageBox.Show("Add New Record Done.", "Messeg", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message , "Error" , MessageBoxButtons.OK , MessageBoxIcon.Error);
            }
            finally
            {
                cn.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.tableTableAdapter.Fill(this.database1DataSet.Table);
        }
    }
}


What I have tried:

I try a lot of time i cant add
Posted
Updated 14-Nov-16 19:54pm
v2
Comments
StM0n 15-Nov-16 0:17am    
* Sorry to ask, but what have you tried?
* Any error messages?
* Is there a table called [Table] in your database?
* Do they have the proper columns you specified?
* Are you aware of the fact, that your code is open for sql injection?

Help us to help you :)
Suvendu Shekhar Giri 15-Nov-16 0:40am    
Have you tried debugging?

I suspect, you table name should be something like "Students" or so and you just forgot to replace that in the INSERT query.

In that case, your query should look like-
INSERT INTO [Students] (Student_ID,StudentName) values ... 

1 solution

For starters, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO [Table] (StudentId, StudentName) VALUES (@ID, @NM)", con))
        {
        cmd.Parameters.AddWithValue("@ID", textBox1.Text);
        cmd.Parameters.AddWithValue("@NM", textBox2.Text);
        cmd.ExecuteNonQuery();
        }
    }

Once you have done that, check your connection string. Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.
Then, check your names and columns: "Table" is an unlikely name for a table, and the two columns you reference use different naming conventions, so it's likely that one of them is wrong as well.
Check the datatypes: are they text? I.e. in SQL Server are they declared as VARCHAR or NVARCHAR? How big are they? Are they INT, or FLOAT instead?

And look at the exception: use the debugger to examine what it says is going on - SQL error message can be unhelpful, but they do give clues.
 
Share this answer
 
Comments
Member 12850701 15-Nov-16 3:01am    
I have use the code that you gave me , but the result is still the same i click add but the database have no update
OriginalGriff 15-Nov-16 3:10am    
And? What did you find for all the other things I suggested?
Member 12850701 15-Nov-16 7:28am    
no respond while click on test connection

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