Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, so i'm making a database which is for a film system. I've made the database in SQL and i've used ado.net to display information from the database in a console application. I've been using SQL CRUD to add data from the console app into the database, but im having trouble adding text to the database. I've used the parameters to do this but it comes up with error saying "Failed to convet parameter value from a String to Int32". I changed the cm.Parameters["@FilmName"].Value into cm.Parameters["@FilmName"].ToString since obviously it couldnt add text using value, but it still comes up with an error.

Heres my code, the SQL Insert command part of the code is the part that needs changing.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Database
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // set up data connection
                string cs = "Data Source=MASTER\\MASTER;Initial Catalog=FilmDB;Integrated Security=True;Pooling=False";
                SqlConnection cn = new SqlConnection(cs);
                // Set up SQL command
                SqlCommand cm = cn.CreateCommand();
                ///
                ///Sql Select Command (Read table and return data)
                ///
                cm.CommandText = "SELECT FilmName, FilmGenre FROM [dbo].[Film];";
                // Set up adapter manager
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cm;
                //Set dataset for results
                DataSet ds = new DataSet();
                ds.Clear();
                //Open Connection to database
                cn.Open();
                //Ask adapter to transfer data from table to dataset
                da.Fill(ds, "Film");
                //Set up data table
                DataTable dt = ds.Tables["Film"];
                //Read data from table rows
                foreach (DataRow dr in dt.Rows)
                {
                    Console.WriteLine(dr["FilmName"].ToString() + "\t\t\t\t" + dr["FilmGenre"].ToString());
                }
                ///
                /// SQL INSERT command
                /// 
                cs = "INSERT INTO [dbo].[Film] ( FilmID, FilmName, FilmGenre ) VALUES ( @FilmID, @FilmName, @FilmGenre );";
                cm.CommandText = cs;
                cm.Parameters.Add("@FilmID", SqlDbType.Int);
                cm.Parameters.Add("@FilmName", SqlDbType.Text);
                cm.Parameters.Add("@FilmGenre", SqlDbType.Text);
                cm.Parameters["@FilmID"].Value = 34;
                cm.Parameters["@FilmName"].Value = "Inception";
                cm.Parameters["@FilmID"].Value = "Action/Thriller";
                
                cm.ExecuteNonQuery();
                cn.Close();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                //cn.close();
            }
        }
    }
}
Posted
Updated 2-Mar-11 1:22am
v2

1 solution

Failed to convert parameter value from a String to Int32

This implies that you handing the string to SQL, but the field in the database is an Int32.

In fact, it's simple:
cm.Parameters["@FilmID"].Value = 34;
cm.Parameters["@FilmName"].Value = "Inception";
cm.Parameters["@FilmID"].Value = "Action/Thriller";
Change the last line:
cm.Parameters["@FilmID"].Value = 34;
cm.Parameters["@FilmName"].Value = "Inception";
cm.Parameters["@FilmGenre"].Value = "Action/Thriller";
 
Share this answer
 
Comments
programmer1234 2-Mar-11 7:28am    
D'oh! I really should check my code over more thoroughly. Thanks.
OriginalGriff 2-Mar-11 7:32am    
I do it all the time: I read the code I meant to write...:laugh:

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