Please good people, i need your help. I having been trying to solve this problem, but no way. what is the best method to insert data so that this error of data truncation will be avoided in C#/asp.net. Here is what i have been trying to do:

public class BuildTable
{
	public static string BuildmyTable(string Regno)
	{
        int counter = 1;
        using (DataTable dtReciept = fetchtopicData(counter))
        {

           
           
                DataTable dtReciept2 = fetchRecieptData(Regno);
                string connStr = ConfigurationManager.ConnectionStrings["ProBookDatabase.mdf"].ConnectionString;
                SqlConnection conn = new SqlConnection(connStr);
                conn.Open();
                string query = "INSERT INTO SwitchData(FullName,RegNumber,Department,TopicID,Session,LecturerName,StaffID,Topic,ProblemDefinition,SkillsRequired,MaxNOfPerson) VALUES(@TopicID,@Session,@LecturerName,@StaffID,@Topic,@ProblemDefinition,@SkillsRequired,@MaxNOfPerson,@FullName,@RegNumber,@Department)";

                SqlCommand cmd = new SqlCommand(query, conn);

                cmd.Parameters.AddWithValue("@FullName", dtReciept2.Rows[0]["FullName"]);
                cmd.Parameters.AddWithValue("@RegNumber", Regno.Trim());
                cmd.Parameters.AddWithValue("@Department", dtReciept2.Rows[0]["Department"]);
                cmd.Parameters.AddWithValue("@Session", dtReciept.Rows[0]["Session"]);
                cmd.Parameters.AddWithValue("@TopicID", dtReciept.Rows[0]["TopicID"]);
                cmd.Parameters.AddWithValue("@LecturerName", dtReciept.Rows[0]["LecturerName"]);
                cmd.Parameters.AddWithValue("@StaffID", dtReciept.Rows[0]["StaffID"]);
                cmd.Parameters.AddWithValue("@Topic", dtReciept.Rows[0]["Topic"]);
                cmd.Parameters.AddWithValue("@ProblemDefinition", dtReciept.Rows[0]["ProblemDefinition"]);
                cmd.Parameters.AddWithValue("@SkillsRequired", dtReciept.Rows[0]["SkillsRequired"]);
                cmd.Parameters.AddWithValue("@MaxNOfPerson", dtReciept.Rows[0]["MaxNOfPerson"]);
                int id = Convert.ToInt32(cmd.ExecuteScalar());
                return Regno;


            
        }

    }
    private static DataTable fetchtopicData(int counter)
    {
        string connStr = ConfigurationManager.ConnectionStrings["ProBookDatabase.mdf"].ConnectionString;
        const string query = "Select * from Project_File Where Id = @Id";
        DataTable Recieptresult = new DataTable();
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                cmd.Parameters.Add("@Id", SqlDbType.NVarChar).Value = counter;
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    Recieptresult.Load(dr);
                }
            }
        }
        return Recieptresult;
    }

    private static DataTable fetchRecieptData(string Regno)
    {
        string connStr = ConfigurationManager.ConnectionStrings["ProBookDatabase.mdf"].ConnectionString;
        const string query = "Select FullName,RegNumber,Department from StudentRegister Where RegNumber = @RegNumber";
        DataTable Recieptresult = new DataTable();
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                cmd.Parameters.Add("@RegNumber", SqlDbType.NVarChar).Value = Regno;
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    Recieptresult.Load(dr);
                }
            }
        }
        return Recieptresult;
    }
}

But it will be giving me error

Member Avatar for stbuchok

Use the proper size data type for your columns. Check the size of all values going into the database and what size each of your columns are set to.

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.