简体   繁体   中英

Inserting NULL values into mdf table in Windows Forms

I cannot figure out what I am doing wrong here, in the debugger when I set breakpoints the strings all have string values in them, but when it comes time to insert them into the table, nothing gets put in.

the table is defined as follows

CREATE TABLE [dbo].[tblMissEvents] (
[EventID]          INT            IDENTITY (1, 1) NOT NULL,
[EventTime]        NVARCHAR (MAX) NULL,
[MIS_Event_Action] VARCHAR (50)   NULL,
[Bogey_Type]       VARCHAR (50)   NULL,
PRIMARY KEY CLUSTERED ([EventID] ASC)

);

my code:

        string date = DateTime.Now.ToString();
        string MISaction = text1.Text;
        string BogeyType = text2.Text;

        try
        {
            con = new SqlConnection();
            con.ConnectionString =
                @"data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\MissileEvents.mdf;integrated security=True;MultipleActiveResultSets=True;Connect Timeout=30";
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText =
                "INSERT into tblMissEvents (EventTime, MIS_Event_Action, Bogey_Type) Values (@date, @MISaction, @BogeyType)";  
            cmd.Parameters.AddWithValue("@date", date);
            cmd.Parameters.AddWithValue("@MISaction", MISaction);
            cmd.Parameters.AddWithValue("@BogeyType", BogeyType);
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        con.Close();

Any thoughts?? I don't get an exception, just no entry into my table.

edit for Arshad:

        con = new SqlConnection();
        con.ConnectionString =
            @"data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\MissileEvents.mdf;integrated security=True;MultipleActiveResultSets=True;Connect Timeout=30";
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText =
            "INSERT into tblMissEvents (MIS_Event_Action, Bogey_Type) Values (@date, @MISaction, @BogeyType)";  
        cmd.Parameters.AddWithValue("@date", date);
        cmd.Parameters.AddWithValue("@MISaction", MISaction);
        cmd.Parameters.AddWithValue("@BogeyType", BogeyType);
        cmd.Connection = con;
        cmd.ExecuteNonQuery();

UPDATE:: The code was fine, it was making a copy of the DB in my bin\\Debug directory and thats where all the data was going. Probably something with my connection string setup. Thanks all!

You can pass null value also as :

 cmd.CommandText =
            "INSERT into tblMissEvents (EventTime, MIS_Event_Action, Bogey_Type)
                                Values (@eventTime, @MISaction, @BogeyType)";  
        cmd.Parameters.AddWithValue("@eventTime", DBNull.Value);          
        cmd.Parameters.AddWithValue("@MISaction", MISaction);
        cmd.Parameters.AddWithValue("@BogeyType", BogeyType);

Update 1

cmd.CommandText =
        "INSERT into tblMissEvents (EventTime,MIS_Event_Action, Bogey_Type) 
                                Values 
                                   (@date, @MISaction, @BogeyType)"; 

Update 2

int RowUpdated=cmd.ExecuteNonQuery();
MessageBox.Show("Number of row updated"+RowUpdated);

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