简体   繁体   中英

What is the proper syntax code in using datetimepicker in visual studio c#?

Can anyone tell me what is the proper syntax code in using datetimepicker that would be saved directly to my Microsoft sql 2005? I'm using visual studio 2008 c#.

Here is my code:

private void button4_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection("Data Source=MJ-PC\\SQLEXPRESS;Initial Catalog=Users;Integrated Security=True");
    SqlDataAdapter dad = new SqlDataAdapter();
    // SqlCommand cmd = new SqlCommand();
    // cmd.Connection = conn;
    dateTimePicker1.Format = DateTimePickerFormat.Short;

    string dateStr = Convert.ToString(dateTimePicker1.Text);
    dad.InsertCommand = new SqlCommand("INSERT INTO tblSchools (School_Name, Province, City, Brgy, Lot_Num, Area, Mem_Date_Rec, Cenro) VALUES(@School_Name, @Province, @City, @Brgy, @Lot_Num, @Area, @Mem_Date_Rec, @Cenro", conn);
    dad.InsertCommand.Parameters.Add("@School_Name", SqlDbType.VarChar).Value = textBox1.Text;
    dad.InsertCommand.Parameters.Add("@Province", SqlDbType.VarChar).Value = comboBox1.Text;
    dad.InsertCommand.Parameters.Add("@City", SqlDbType.VarChar).Value = textBox2.Text;
    dad.InsertCommand.Parameters.Add("@Brgy", SqlDbType.VarChar).Value = textBox4.Text;
    dad.InsertCommand.Parameters.Add("@Lot_Num", SqlDbType.VarChar).Value = textBox5.Text;
    dad.InsertCommand.Parameters.Add("@Area", SqlDbType.Int).Value = textBox6.Text;
    dad.InsertCommand.Parameters.Add("@Mem_Date_Rec", SqlDbType.DateTime).Value = dateTimePicker1.Value.Date;
    dad.InsertCommand.Parameters.Add("@Cenro", SqlDbType.VarChar).Value = textBox8.Text;    

    conn.Open();
    dad.InsertCommand.ExecuteNonQuery();
    conn.Close();
}

The problem here is the datetimepicker, in my sql server Mem_Date_Rec is a datetime, so whenever I try to run it and save something on my database, dad.InsertCommand.ExecuteNonQuery(); Keeps on saying "Incorrect syntax near '@Cenro'."

Can anyone help me out here please, it would be a really great help.

I feel like you try to insert your parameter to dad.InsertCommand command not cmd command.

dad.InsertCommand.Parameters.Add("@Mem_Date_Rec", SqlDbType.DateTime).Value = dateTimePicker1.Value.Date;

Because your dad.InsertCommand has a parameter called @Mem_Date_Rec , not cmd . I have no idea what is your cmd for exactly. It's useless this case. You can't add a parameter value in an SqlCommand that doesn't have any parameter definition .

Also use using statement to dispose your SqlConnection and SqlCommand like;

using(SqlConnection conn = new SqlConnection(ConnectionString))
using(SqlCommand cmd = conn.CreateCommand())
{
   //
}

If you want to write a proper syntax code, you need start reading a book, articles, blogs, examples etc..

edit

You're missing something in your SQL. Change this:

> dad.InsertCommand = new SqlCommand("INSERT INTO tblSchools
> (School_Name, Province, City, Brgy, Lot_Num, Area, Mem_Date_Rec,
> Cenro) VALUES(@School_Name, @Province, @City, @Brgy, @Lot_Num, @Area,
> @Mem_Date_Rec, @Cenro", conn);

To this

dad.InsertCommand = new SqlCommand("INSERT INTO tblSchools (School_Name, Province, City, Brgy, Lot_Num, Area, Mem_Date_Rec, Cenro) VALUES(@School_Name, @Province, @City, @Brgy, @Lot_Num, @Area, @Mem_Date_Rec, @Cenro)", conn);

INSERT INTO table (columns) values (value)

you had: INSERT INTO table (columns) values (value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM