Click here to Skip to main content
16,004,969 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have inputted test.cid,cdt,cc1,cc2,cc3 thru a sqldatareader and it works fine.

I now want to "INSERT INTO" the data one row at a time.

I am getting errors with test.cid[i],test.cdt[i],test.cc1[i],test.cc2[i],test.cc3[i]

How do I do that?

MDFtoMDF.cs

C#
public class MDFtoMDF : IDisposable
{
    public int cid { get; set; }
    public string cdt { get; set; }
    public string cc1 { get; set; }
    public string cc2 { get; set; }
    public string cc3 { get; set; }
    public void Dispose()
    {
        Dispose(true);
    }

    private void Dispose(bool disposable)
    {
        if (disposable)
        {
            // Free resources
        }
        // Prevents the destructor to be called if dispose is called.
        GC.SuppressFinalize(this);
    }

}

MDFtoMDF.aspx.cs
C#
    protected void Button1_Click(object sender, EventArgs e)
{
    //ArrayList list = new ArrayList();
    using (MDFtoMDF test = new MDFtoMDF())
    {
        Int16 n = 0;
        String connStr = ConfigurationManager.ConnectionStrings["From"].ConnectionString;
        String cmdStr = "SELECT * FROM [Table2];";
        try
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
                {
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        dr.Read();
                        n += 1;
                        test.cid = n + 3;
                        test.cdt = (dr[1].ToString());
                        test.cc1 = (dr[2].ToString());
                        test.cc2 = (dr[3].ToString());
                        test.cc3 = (dr[4].ToString());
                    }
                    conn.Close();
                    cmd.Dispose();
                    conn.Dispose();
                }
            }
        }
        catch (Exception ex)
        {
            TextBox1.Text = ex.Message;
        }
        using (MDFtoMDF test = new MDFtoMDF())
    {
        for (int i = 0; i <= n-1; i++)
        {
            int16 dataid = test.cid[i];
            string datadatetime = test.cdt[i];
            string datacol1 = test.cc1[i];
            string datacol2 = test.cc2[i];
            string datacol3 = test.cc3[i];
            //SQL INSERT INTO
         }
    }
Posted
Comments
[no name] 6-Sep-14 8:54am    
This is why you were told to get a book and go through it. What errors? We can't see your monitor. What data do you really expect to see in a brand new never been used instance of your MDFtoMDF class? cid is not array but you are trying to us it as if it were. While the string code here will probably work why use a string for one character? Why are you closing and disposing of you Connection and Command objects that are enclosed within using blocks but not for your reader?

1 solution

Try This :

Quote:

while (dr.Read())
{
n += 1;
test.cid = n + 3;
test.cdt = (dr[1].ToString());
test.cc1 = (dr[2].ToString());
test.cc2 = (dr[3].ToString());
test.cc3 = (dr[4].ToString());
}
 
Share this answer
 

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