Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
plz help me....m getting problem with this code...
in my database there is a table with 2 columns and their data types are as follows
name varchar(50)
date datetime
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection("Data Source=AHSAN-PC\\SQLEXPRESS; Initial Catalog=waf; Integrated Security=SSPI;");
                con.Open();
                MessageBox.Show("Connection Established Successfully with Database", "Database Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                SqlCommand cmd = new SqlCommand("insert into loginfo values('" + textBox1.Text + "',  ' " + dateTimePicker1.Value.ToShortDateString()+ " ' ", con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Insertion successfully done");
            }
            catch (Exception e1)
            {
                MessageBox.Show("error:" + e1.Message);
            }

        }
    }
}
Posted
Comments
uspatel 17-Sep-11 6:00am    
specify your errors!
Member 11045357 7-Sep-14 1:54am    
but, how to via stored procedure?

I think problem in the formate of value that you are inserting in database.
use dateTimePicker1.Value instead of
dateTimePicker1.Value.ToShortDateString().
or
use
Convert.ToDateTime(dateTimePicker1.Value.ToShortDateString());
 
Share this answer
 
Remove the spaces around the date:

C#
SqlCommand cmd = new SqlCommand("insert into loginfo values('" + textBox1.Text + "',  '" + dateTimePicker1.Value.ToShortDateString()+ "' ", con);


Try this if it still has problems :

C#
dateTimePicker1.Value.ToString("yyyy-MM-dd")
 
Share this answer
 
C#
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conm"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter adp = new SqlDataAdapter();
    DataSet ds = new DataSet();

 try
        {


 cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            // cmd.CommandText = "update ReglaCategorizacion set Ordenejecucion='" + lblOExecution.Text + "'where IdRegla='" + lblPreviousRegla.Text + "' ";
  cmd.CommandText = "insert into UserDetail(RoleID,FirstName,LastName)";
            cmd.Parameters.AddWithValue("@RoleID", Rdb_UserType.SelectedValue);
            cmd.Parameters.AddWithValue("@FirstName", txt_name.Text);
            cmd.Parameters.AddWithValue("@LastName", txt_Surname.Text);
           
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Response.Redirect("~/ErroPage.aspx?a=" + ex.Message);

        }
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection("Data Source=AHSAN-PC\\SQLEXPRESS; Initial Catalog=waf; Integrated Security=SSPI;");
con.Open();
MessageBox.Show("Connection Established Successfully with Database", "Database Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
SqlCommand cmd = new SqlCommand("insert into loginfo values('" + textBox1.Text + "', ' " + dateTimePicker1.Value.ToShortDateString()+ " ' ", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Insertion successfully done");
}
catch (Exception e1)
{
MessageBox.Show("error:" + e1.Message);
}

}
}
}
 
Share this answer
 
C#
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conm"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter adp = new SqlDataAdapter();
    DataSet ds = new DataSet();

 try
    {
 cmd.Connection = conn;
 cmd.CommandType = CommandType.Text;            
 cmd.CommandText = "insert into UserDetail(RoleID,FirstName,LastName)";
 cmd.Parameters.AddWithValue("@RoleID", Rdb_UserType.SelectedValue);
 cmd.Parameters.AddWithValue("@FirstName", txt_name.Text);
 cmd.Parameters.AddWithValue("@LastName", txt_Surname.Text);
           
  conn.Open();
  cmd.ExecuteNonQuery();
  }
  catch (Exception ex)
  {
  Response.Redirect("~/ErroPage.aspx?a=" + ex.Message);
  }
 
Share this answer
 
v2

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