i want to insert rows one by one into a database in the sql server 2005.

the problem is that i have tried all possible but the rows just dont get added to the database, i get an exception

on executing this code, i get this exception

Unhandled Exception: System.IndexOutOfRangeException: An SqlParameter with ParameterName 'msg' is not contained by this SqlParameterCollection.

here is the code -

SqlConnection thisConnection = new SqlConnection(@"Integrated Security=SSPI;Initial Catalog=sec;Workstation ID=3035-HEADSTRONG;");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
StreamReader sr= new StreamReader(@"e:\fast.txt");
string ss=null;
int i=1;
ss=sr.ReadLine();
Console.WriteLine(ss);
    while(ss!=null)
        {
        i++;
        thisCommand.Parameters["msg"].Value=ss;
        thisCommand.Parameters["id"].Value=i;
        // -----------i have tried this too--------
                //thisCommand.CommandText = "INSERT INTO fix (msg,id) VALUES (ss,i)";
        //thisCommand.ExecuteNonQuery();
        ss=sr.ReadLine();

    }

Ok first things first. You did close the connection right?

2nd of all. You might wanna clear your parameters after you enter each row.

These are the two observations that I can make from a cursory glance at your code. I'd like it if you could try out these two things and give a feedback

i tried both of your suggestions but i am getting the same exception again---

Unhandled Exception: System.IndexOutOfRangeException: An SqlParameter with ParameterName 'msg' is not contained by this SqlParameterCollection.

i have checked the table- both the names of the parameters are right.

i got it...

here's the code

using System;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Odbc;
using System.Data.Common;
using System.Data;
using System.IO;

namespace db
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    /// 

    class Class1
    {

        static int i=1;
        static string ss=null;
        static string t="IOI";
        static string str=null;
        static SqlCommand thisCommand;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Main(string[] args)
        {
SqlConnection thisConnection = new SqlConnection(@"Integrated Security=SSPI;Initial Catalog=sec;Workstation ID=3035-HEADSTRONG;");
        thisConnection.Open();
        StreamReader sr= new StreamReader(@"e:\fast.txt");
        str="insert into fix(msg,id,type) values(@msg,@id,@type)";
                ss=sr.ReadLine();
                while(ss!=null)
                {
                    i++;
            thisCommand= new SqlCommand(str,thisConnection);
            thisCommand.Parameters.Add("@msg",ss);
            thisCommand.Parameters.Add("@id",i);
            thisCommand.Parameters.Add("@type",t);
                        thisCommand.ExecuteNonQuery();
                    ss=sr.ReadLine();
                }
            thisCommand.CommandText="select * from fix";
        SqlDataReader sdr = thisCommand.ExecuteReader();
                while (sdr.Read())
                {
            Console.WriteLine("\t{0}\t{1}", sdr["msg"], sdr["id"]);
                    Console.WriteLine();
                }
                sdr.Close();
        }

    }
}

I have a form to insert data into a sql data base.But I don't remember the code to be written at the back of the insert button ("on click button") using c#

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.