سؤال

I m sorry if this question asked before, i am new to c# i am working with ms access database i am storing date to ms access date field...

please check what is the error My error string is following:

failed to convert parameter value from a string to a timespan

The code is:

private void button1_Click(object sender, EventArgs e)
    {
        //create connection 
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=posv.accdb";
        conn.Open();
        string Expanse_Name = expanse_name.Text;
        string Expanse_Cost = expanse_cost.Text;
        string Expanse_Date = expanse_date.Value.ToString("m/d/Y");
        OleDbCommand cmd = new OleDbCommand("INSERT INTO expanses (Expanse_Name, Expanse_Cost,Expanse_Date) VALUES (@Expanse_Name, @Expanse_Cost,@Expanse_Date)", conn);

        if(conn.State == ConnectionState.Open){

            cmd.Parameters.Add("@Expanse_Name", OleDbType.VarChar, 20).Value = Expanse_Name;
            cmd.Parameters.Add("@Expanse_Cost", OleDbType.UnsignedInt, 20).Value = Expanse_Cost;
            cmd.Parameters.Add("@Expanse_Date", OleDbType.VarChar, 20).Value = Expanse_Date.;

            try {

                cmd.ExecuteNonQuery();
                MessageBox.Show("Expanse Added Success fully!");

            }catch(OleDbException exps){

                MessageBox.Show(exps.Message);
                conn.Close();

            } // end try


        } //end conn state

    } // end save function
هل كانت مفيدة؟

المحلول

You need to specify the right datatype for the db variable. It depends what datatype you've chosen in db. So best approach is look at the respective datatype in the database and change it accordingly.

try this:

OleDbConnection conn = new OleDbConnection();
    conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=posv.accdb";
    conn.Open();
    string Expanse_Name = expanse_name.Text;
    string Expanse_Cost = expanse_cost.Text;
    string Expanse_Date = expanse_date.Value.ToString("m/d/Y");
    OleDbCommand cmd = new OleDbCommand("INSERT INTO expanses (Expanse_Name, Expanse_Cost,Expanse_Date) VALUES (@Expanse_Name, @Expanse_Cost,@Expanse_Date)", conn);

    if(conn.State == ConnectionState.Open){

        cmd.Parameters.Add("@Expanse_Name", OleDbType.VarChar, 20).Value = Expanse_Name;
        cmd.Parameters.Add("@Expanse_Cost", OleDbType.UnsignedInt, 20).Value = Expanse_Cost;
        cmd.Parameters.Add("@Expanse_Date", OleDbType.DBTimeStamp, 20).Value = Expanse_Date.;

        try {

            cmd.ExecuteNonQuery();
            MessageBox.Show("Expanse Added Success fully!");

        }catch(OleDbException exps){

            MessageBox.Show(exps.Message);
            conn.Close();

        } // end try


    } //end conn state

} 

نصائح أخرى

Change the ToString format on the line line:

string Expanse_Date = expanse_date.Value.ToString("HH:mm:ss.fff");

I think the Expanse_Date should be keep in DateTime and the parameter type for it should be OleDbType.DBTimeStamp. (And please use using blocks.)

    private void button1_Click(object sender, EventArgs e)
    {
        using (var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=posv.accdb"))
        {
            conn.Open();

            var Expanse_Name = expanse_name.Text;
            var Expanse_Cost = expanse_cost.Text;
            var Expanse_Date = expanse_date.Value;
            using (var cmd = new OleDbCommand("INSERT INTO expanses (Expanse_Name, Expanse_Cost,Expanse_Date) VALUES (@Expanse_Name, @Expanse_Cost,@Expanse_Date)", conn))
            {
                cmd.Parameters.Add("@Expanse_Name", OleDbType.VarChar, 20).Value = Expanse_Name;
                cmd.Parameters.Add("@Expanse_Cost", OleDbType.UnsignedInt, 20).Value = Expanse_Cost;
                cmd.Parameters.Add("@Expanse_Date", OleDbType.DBTimeStamp, 20).Value = Expanse_Date;

                try
                {
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Expanse Added Success fully!");
                }
                catch (OleDbException exps)
                {
                    MessageBox.Show(exps.Message);
                    conn.Close();
                }
            }
        }
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top