Question implement the crud operations?

codify

Member
Joined
Dec 4, 2020
Messages
18
Programming Experience
1-3
Hye, everyone. So I am following this video to implement the crud operations using c#.
I had a discussing here too and I did the changes as they said! Still, the error is there! Please help me!
 

Attachments

  • DFSD.PNG
    DFSD.PNG
    78.3 KB · Views: 23
Sounds more like a SQL configuration issue rather than a C# issue. Have you tried turning on that setting that is error is complaining about?
 
Sounds more like a SQL configuration issue rather than a C# issue. Have you tried turning on that setting that is error is complaining about?
Initially, the same error "An explicit value for the identity column in table 'Patient_basicinfo' can only be specified when a column list is used and IDENTITY_INSERT is ON." was occurring in the SQL studio but then I used the "SET IDENTITY_INSERT Patient_basicinfo ON/OFF"! By this, the query was executed successfully at the SQL STUDIO but the same error is there pops in the visual studio. I have no idea what to do!
error 4.PNG
 
You turned it on on line 2, but then turned it back off with that last line. So later when you run your C# code, it's attempt to do the SQL INSERT has the setting set to off, and so you get the error.
 
You turned it on on line 2, but then turned it back off with that last line. So later when you run your C# code, it's attempt to do the SQL INSERT has the setting set to off, and so you get the error.
Removed the last line, executed the query. Still same error! ?
 
Please post your code in code tags, not a screenshot.

Anyway, there's the first part of the error there regarding the explicit column list. Even the StackOverflow thread you have has comments telling you that you should also list the columns. From the screenshot on post #1, what I can see seems to show that you only passed in the values, but not the column names.
 
I am not getting what the guys at stack overflow are saying as i am very new to c#.
Please post your code in code tags, not a screenshot.

Anyway, there's the first part of the error there regarding the explicit column list. Even the StackOverflow thread you have has comments telling you that you should also list the columns. From the screenshot on post #1, what I can see seems to show that you only passed in the values, but not the column names.

C#:
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace CRUDAPP
{
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A85V0ME\SQLEXPRESS;Initial Catalog=CRUDDATABASE;Integrated Security=True");
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetPatientRecord();
        }

        private void GetPatientRecord()
        {
          
            SqlCommand cmd = new SqlCommand("Select * from Patient_basicinfo", con);
            DataTable dt = new DataTable();

            con.Open();

            SqlDataReader sdr = cmd.ExecuteReader();
            dt.Load(sdr);
            con.Close();

            Patient_DataGrid.DataSource = dt;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            if(isvalid())
            {
                
                SqlCommand cmd = new SqlCommand("INSERT INTO Patient_basicinfo Values(@PatientCnic,@PatientContact,@PatientAddress,@Patientname,@Patientage)", con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@PatientCnic", textPatientCnic.Text);
                cmd.Parameters.AddWithValue("@PatientContact", textPatientContact.Text);
                cmd.Parameters.AddWithValue("@PatientAddress", textPatientAddress.Text);
                cmd.Parameters.AddWithValue("@Patientname", txtPatientname.Text);
                cmd.Parameters.AddWithValue("@Patientage", textPatientAge.Text);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

                MessageBox.Show("New Patient Info is succesfully saved in the database", "Saved" , MessageBoxButtons.OK, MessageBoxIcon.Error);

                GetPatientRecord();

            }
        }

        private bool isvalid()
        {
            if(txtPatientname.Text == string.Empty)
            {
                MessageBox.Show("Patient Name is Required", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
    }
}
 
It's not in the C# code. It's in the SQL statement that you are passing to SQL and/or the configuration of your SQL database. SQL in C# strings is not a C# problem, much like the way any HTML in C# strings is not a C# problem.

On line 52, you are not listing the column names. As stated in the exception, you need to list the column names. (Also also as noted in the the comments in StackOverflow, SQL Studio will let you get by without setting the column names.)

It's unclear what the final state of your database is. Is IDENTITY_INSERT on, or off by the time your C# code is executing.
 
It's not in the C# code. It's in the SQL statement that you are passing to SQL and/or the configuration of your SQL database. SQL in C# strings is not a C# problem, much like the way any HTML in C# strings is not a C# problem.

On line 52, you are not listing the column names. As stated in the exception, you need to list the column names. (Also also as noted in the the comments in StackOverflow, SQL Studio will let you get by without setting the column names.)

It's unclear what the final state of your database is. Is IDENTITY_INSERT on, or off by the time your C# code is executing.
SqlCommand cmd = new SqlCommand("INSERT INTO Patient_basicinfo Values(@PatientCnic,@PatientContact,@PatientAddress,@Patientname,@Patientage)", con);
What's wrong with this? I have written the same thing as mentioned in the video at 13:00. I don't know how to list the column names. The column names are " PatientCnic,PatientContact,PatientAddress,Patient name,Patientage which are mentioned in the line.
 
I don't know how to list the column names.
That's because you're just following a video that provides one specific example instead of doing proper research. If you did some research on SQL and read some documentation and proper tutorials instead of just watching videos then you'd learn how to write an INSERT statement. Here's a good place to start. One click on the link for the INSERT statement and there's your answer. If you rely on YouTube videos then you will learn very little. Ii suggest that you do some proper research on any area you wish to learn.
 
The column names are " PatientCnic,PatientContact,PatientAddress,Patient name,Patientage which are mentioned in the line.
No they are not mentioned. That you have named the parameters after the columns is a good thing but those parameters could be named anything or even not have names at all, so if you expect the column names to be specified then you have to specify them.
 
Back
Top Bottom