0

I'm having a problem with SQL. I'm new to C# so I don't really know what to do.

Here's the code:

public static void addMemo()
    {
       schedule sch = (schedule)Application.OpenForms["schedule"];
       using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\_data\schedData.mdf;Integrated Security=True"))
        {
            SqlCommand CmdSql = new SqlCommand("INSERT INTO schTable (evnName, evnDate, evnTime, evnStatus) VALUES (@evnName, @evnDate, @evnTime, @evnStatus)", conn);

            conn.Open();
            CmdSql.Parameters.AddWithValue("@evnName", sch.txtName.Text);
            CmdSql.Parameters.AddWithValue("@evnDate", sch.txtDate.Text);
            CmdSql.Parameters.AddWithValue("@evnTime", sch.txtTime.Text);
            CmdSql.Parameters.AddWithValue("@evnStatus", "Upcoming");
            CmdSql.ExecuteNonQuery();

            sch.lblNotifier.Text = sch.txtName.Text + " Added.";
            conn.Close();
        }

    }

Everything works fine, there are no errors. But when I looked into the table, there was nothing. I cannot see my inputs. Can someone help me?

EDIT - Here I have a working code that I made before in VB.Net I'm trying to use this instead but I'm stuck with the Tables part. (I'm a noob at c#)

Private ConStr As New String("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\data_\database.mdf;Integrated Security=True")

    Dim MyNewRow As DataRow = SqlDatTbl.NewRow()
    SqlDatTbl.Rows.Add(MyNewRow)
    SqlRowPos = SqlDatTbl.Rows.Count - 1

    SqlCon.ConnectionString = ConStr
    SqlCon.Open()

    SqlDatAdp = New SqlDataAdapter("Select * from  Events", SqlCon)
    SqlCmbBld = New SqlCommandBuilder(SqlDatAdp)
    SqlDatAdp.Fill(SqlDatTbl)

    If SqlDatTbl.Rows.Count <> 0 Then

        SqlDatTbl.Rows(SqlRowPos)("Name") = txtName.Text
        SqlDatTbl.Rows(SqlRowPos)("Date") = txtDate.Text
        SqlDatTbl.Rows(SqlRowPos)("Time") = txtTime.Text
        SqlDatTbl.Rows(SqlRowPos)("Status") = "Upcoming"

        SqlDatAdp.Update(SqlDatTbl)
        MsgBox("New Event Added")

    End If
    SqlCon.Close()

2 Answers2

0

When working with an attached database and that database is being shown in Solution Explorer there is a property, "Copy to output directory" where the default setting is "Copy Always". So if you were to assign a variable to ExecuteNonQuery and the results show a value higher than 0 then you will need to set "Copy to Output Directory" to "Do not Copy" and then manually copy the database into the Bin\Debug or Bin\Release folder or set "Copy to Output Directory" to "Copy if Newer" which means unlike "Copy always" this value will only copy the database to the app folder if you make any changes to the database in the project folder. I have a article with samples and explanations for you to try out MSDN Working with Copy to Output Directory.

Here is the default setting enter image description here

If ExecuteNonQuery returns 0 then the above is not the issue.

Karen Payne
  • 4,341
  • 2
  • 14
  • 31
  • I'm sorry but I still have the same problem. Anyway thank you. – Jose Bonifacio Jan 29 '16 at 14:24
  • Was Copy to Output Directory setup as described or not? Please elaborate. What was returned by ExcuteNonQuery ? – Karen Payne Jan 29 '16 at 14:31
  • I'm getting a 'SqlException was Unhandled' Error if I selected "Do not copy" but the problem is still in there if "Copy if newer" is selected. – Jose Bonifacio Jan 29 '16 at 14:39
  • When selecting "Don not copy" the next time you build the project the database is deleted from the Bin\Debug folder so as I indicated in my reply here and in the MSDN sample you need to copy the database via Windows Explorer to the Bin\Debug folder else you get the error as you just had e.g. UnHandled exception. If what I offered does not help I have nothing more to add as I see no other reason for you to have this issue. – Karen Payne Jan 29 '16 at 14:43
  • I tried using the .mdf file outside the bin/debug folder and it worked. – Jose Bonifacio Jan 29 '16 at 14:57
0

Have you checked to make sure that sch.txtName.Text and sch.txtDate.Text and sch.txtTime.Text actually have values in them? Insert a breakpoint on conn.Open(); and then if you mouse over sch.txtName.Text, sch.txtDate.Text, and sch.txtTime.Text in your code, then it should show a little tooltip with information about the field you are mousing over. Here's an image showing what it looks like for me with a chunk of some random code. The popup has an arrow on the left side that opens up if you mouse over it. That'll give you more information about the variable you are looking at. If your text values are empty, then it has nothing to write to the database. Depending on how the database is configured, that could mean that it doesn't write anything at all or that it writes empty fields.

Since you said you are new, if you don't know how to add a breakpoint, you can add one in visual studio by right clicking on the line you want to put the breakpoint at and then under the breakpoints sub-menu, click on Insert breakpoint. Now when you run your code in debug mode and it gets to that point, it will stop and let you see what's going on.

J Miller
  • 51
  • 1
  • 7