hi. I'm trying to make a simple c# program using sqlserver.

when i try to save something into database, it gives this error.

here's the code. that 15(age value) is the default value on my numericupdown

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 WindowsFormsApplication10
{
    public partial class formDefineStudent : Form
    {
        public formDefineStudent()
        {
            InitializeComponent();
        }

        private void formDefineStudent_Load(object sender, EventArgs e)
        {
            txtStudentName.Text = "";
            txtParentName.Text = "";
            btnDelete2.Enabled = false;
            
         
        }

        private void btnSave2_Click(object sender, EventArgs e)
        {
            string cmdtext;
            string connStr = "Data Source=FIRAT;Initial Catalog=school;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();

            cmdtext = "insert into tblSTUDENT(FULLNAME, PARENTNAME, AGE) VALUES('"
                + txtStudentName.Text + "',  '" + txtParentName.Text + "' " + txtAge.Text.ToString() +")";

            SqlCommand cmd = new SqlCommand(cmdtext,conn);
            cmd.ExecuteNonQuery();
            conn.Close();
     


        }
        private void grdRefresh()
        {

            string connStr = " Initial Catalog = school ; Integrated Security =true";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();

            SqlDataAdapter dAdapter = new SqlDataAdapter("SELECT * FROM tblSTUDENT", conn);
            DataTable dTable = new DataTable();
            dAdapter.Fill(dTable);
            dgStudentList.DataSource = dTable;

            conn.Close();

        
        }

       


        
    }
}

Recommended Answers

All 3 Replies

You forgot to add a comma between the parent name and age values:

cmdtext = "insert into tblSTUDENT(FULLNAME, PARENTNAME, AGE) VALUES('"
                + txtStudentName.Text + "',  '" + txtParentName.Text + "', " + txtAge.Text.ToString() +")";

Sometimes it helps to use string.Format() instead of concatenation because the formatting around placeholders is easier to see:

cmdtext = string.Format(
    "insert into tblSTUDENT(FULLNAME, PARENTNAME, AGE) VALUES ('{0}', '{1}', {2});",
    txtStudentName.Text,
    txtParentName.Text,
    txtAge.Text);

Exactly, all parameters in the insert statment must be seperared by comma.
But you can use a better approach with parametereized query:

cmdtext = "insert into tblSTUDENT(FULLNAME, PARENTNAME, AGE) VALUES(@p1, @p2, @'3)";
cmdtext.Parameters.Add("@p1", SqlDbType.VarChar, 50).Value = txtStudentName.Text;
cmdtext.Parameters.Add("@p2", SqlDbType.VarChar, 50).Value = txtParentName.Text;
cmdtext.Parameters.Add("@p3", SqlDbType.VarChar, 50).Value = txtAgr.Text;

Parameters type must based on the type in your actual databasse. Use VarChar (and its lenght -50 in my example). If you have an integer, change the type to integer, like:

cmdtext.Parameters.Add("@p1", SqlDbType.Int).Value = int.Parse(txtStudentName.Text);

Hope it helps,
bye

thank you, both of you. it was helpful

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.