我的代码没有错误,但仍无效(插入操作)

时间:2018-03-01 07:17:27

标签: c# asp.net database

我已经在C#的SQL数据库中开发了一个数据插入代码,但是它没有工作,它显示了"成功添加"但实际上它没有插入。 请帮帮我。我的错误是什么,或者我的代码中有什么问题?我一次又一次检查我的代码,但仍然无法找到错误。 这是我的代码:

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

public partial class Provide_Notes : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            if(Session["StaffUsername"]!=null)
            {
                lblProfilePic.Text = Session["StaffUsername"].ToString();
                string CS = ConfigurationManager.ConnectionStrings["SCDB"].ConnectionString;
                using (SqlConnection con = new SqlConnection(CS))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("SELECT Photo_Binary FROM Staff_Table WHERE Username=@SUsername",con);
                    cmd.Parameters.AddWithValue("@SUsername",lblProfilePic.Text);

                    SqlDataReader sdr = cmd.ExecuteReader();
                    if(sdr.HasRows)
                    {
                        while(sdr.Read())
                        {
                            byte[] imagedata = (byte[])sdr["Photo_Binary"];
                            string image = Convert.ToBase64String(imagedata, 0, imagedata.Length);
                            Image3.ImageUrl = "data:Img/png;base64," + image;
                        }
                    }
                }
            }
            else
            {
                Response.Redirect("index.aspx");
            }
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string CS = ConfigurationManager.ConnectionStrings["SCDB"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            con.Open();
            string filePath = FileUpload1.PostedFile.FileName;
            string fileName = Path.GetFileName(filePath);
            string ext = Path.GetExtension(fileName);
            string contentType = String.Empty;
            switch(ext)
            {
                case ".docx":
                    contentType = "application/vnd.ms-word";
                    break;
                case ".doc":
                    contentType = "application/vnd.ms-word";
                    break;
                case ".xls":
                    contentType = "application/vnd.ms-excel";
                    break;
                case ".xlsx":
                    contentType = "appliacation/vnd.ms-excel";
                    break;
                case ".pdf":
                    contentType = "application/pdf";
                    break;
            }
            if(contentType!=String.Empty)
            {
                Stream fs = FileUpload1.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(fs);
                Byte[] bytes = br.ReadBytes((Int32)fs.Length);
                SqlCommand cmd = new SqlCommand("INSERT INTO Notes VALUES (@Note_Name,@Description,@Name,@Content_Type,@Data)",con);
                cmd.Parameters.AddWithValue("@Note_Name",TextBox1.Text.Trim());
                cmd.Parameters.AddWithValue("@Description", TextBox2.Text.Trim());
                cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = fileName;
                cmd.Parameters.Add("@Content_Description", SqlDbType.NVarChar).Value = contentType;
                cmd.Parameters.Add("@Data", SqlDbType.VarBinary).Value = bytes;
                Response.Write("<script>alert('Note added !')</script>");
            }
        }
    }

    protected void btnLogout_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Session.RemoveAll();
        Response.Redirect("Logout.aspx");
    }

}

ASP Web HTML代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Provide_Notes_FY.aspx.cs" Inherits="Provide_Notes" EnableEventValidation="false" %>

<!DOCTYPE html>


<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Staff Access | Dashboard</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.6 -->
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  <!-- Ionicons -->
    <link rel="stylesheet" href="StyleSheet.css" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
  <!-- iCheck -->
  <link rel="stylesheet" href="plugins/iCheck/flat/blue.css">
  <!-- Morris chart -->
  <link rel="stylesheet" href="plugins/morris/morris.css">
  <!-- jvectormap -->
  <link rel="stylesheet" href="plugins/jvectormap/jquery-jvectormap-1.2.2.css">
  <!-- Date Picker -->
  <link rel="stylesheet" href="plugins/datepicker/datepicker3.css">
  <!-- Daterange picker -->
  <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker.css">
  <!-- bootstrap wysihtml5 - text editor -->
  <link rel="stylesheet" href="plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

</head>
<body class="hold-transition skin-blue sidebar-mini">
    <form runat="server">
<div class="wrapper">

  <header class="main-header">
    <!-- Logo -->
    <a href="adminTS.aspx" class="logo">
      <!-- mini logo for sidebar mini 50x50 pixels -->
      <span class="logo-mini"><b>S.</b>A</span>
      <!-- logo for regular state and mobile devices -->
      <span class="logo-lg"><b>Staff</b> Access</span>
    </a>
    <!-- Header Navbar: style can be found in header.less -->
    <nav class="navbar navbar-static-top">
      <!-- Sidebar toggle button-->
      <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">
        <span class="sr-only">Toggle navigation</span>
      </a>

      <div class="navbar-custom-menu">
        <ul class="nav navbar-nav">
          <!-- Messages: style can be found in dropdown.less-->
          <li class="dropdown messages-menu">
            <asp:LinkButton ID="btnLogout" runat="server" OnClick="btnLogout_Click" CausesValidation="false">
              Logout <i class="fa fa-sign-out"></i>

            </asp:LinkButton>

          </li>
          <!-- Control Sidebar Toggle Button -->
          <li>
            <a href="#" data-toggle="control-sidebar"><i class="fa fa-gears"></i></a>
          </li>
        </ul>
      </div>
    </nav>
  </header>
  <!-- Left side column. contains the logo and sidebar -->
  <aside class="main-sidebar">
    <!-- sidebar: style can be found in sidebar.less -->
    <section class="sidebar">
      <!-- Sidebar user panel -->
      <div class="user-panel">
        <div class="pull-left image">
          <!--<img src="dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">-->
           <asp:Image ID="Image3" runat="server" CssClass="img-circle" />
        </div>
        <div class="pull-left info">
         <asp:Label ID="lblProfilePic" runat="server" Text=""></asp:Label>
          <a href="#"><i class="fa fa-circle text-success"></i> Online</a>
        </div>
      </div>
      <!-- search form -->
      <form action="#" method="get" class="sidebar-form">

      </form>
      <!-- /.search form -->
      <!-- sidebar menu: : style can be found in sidebar.less -->
      <ul class="sidebar-menu">
        <li class="header">MAIN NAVIGATION</li>
        <li class="active treeview">
          <a href="adminTS.aspx" id="Staff_Control">
            <i class="fa fa-dashboard"></i><span>Staff Control
                                            </span>

          </a>
          <!--<ul class="treeview-menu">
            <li class="active"><a href="index.html"><i class="fa fa-circle-o"></i> Dashboard v1</a></li>
            <li><a href="index2.html"><i class="fa fa-circle-o"></i> Dashboard v2</a></li>
          </ul>-->
        </li>
        <li id="li" class="treeview">
          <a href="#">
            <i class="fa fa-user"></i>
            <span>View HOD</span>
          </a>
        </li>
        <li>
          <a href="pages/widgets.html">
            <i class="fa fa-users"></i> <span>Staff</span>

              <span class="pull-right-container">
              <i class="fa fa-angle-left pull-right"></i>

            </span>
          </a>
            <ul class="treeview-menu">
                <li><a href="#"><i class="fa fa-circle-o"></i>First Year</a></li>
                <li><a href="#"><i class="fa fa-circle-o"></i>Second Year</a></li>
                <li><a href="#"><i class="fa fa-circle-o"></i>Third Year</a></li>
        </ul>
        <li class="treeview">
          <a href="#">
            <i class="fa fa-info-circle"></i>
            <span>Student Details</span>
            <span class="pull-right-container">
              <i class="fa fa-angle-left pull-right"></i>
            </span>
          </a>
          <ul class="treeview-menu">

            <li><a href="pages/charts/morris.html"><i class="fa fa-circle-o"></i> First Year</a></li>
            <li><a href="pages/charts/flot.html"><i class="fa fa-circle-o"></i> Second Year</a></li>
            <li><a href="pages/charts/inline.html"><i class="fa fa-circle-o"></i>Third Year</a></li>
          </ul>
        </li>
          <li class="treeview">
              <a href="">
                  <i class="fa fa-edit"></i>
                  <span>Publish CT Result</span>
                  <span class="pull-right-container">
                      <i class="fa fa-angle-left pull-right"></i>
                  </span>
              </a>
              <ul class="treeview-menu">
                  <li><a href="#"><i class="fa fa-circle-o"></i>First Year</a></li>
                  <li><a href="#"><i class="fa fa-circle-o"></i>Second Year</a></li>
                  <li><a href="#"><i class="fa fa-circle-o"></i>Third Year</a></li>
              </ul>
          </li>
          <li class="treeview">
              <a href="#">
                  <i class="fa fa-table"></i>
                  <span>Attendance</span>
                  <span class="pull-right-container">
                      <i class="fa fa-angle-left pull-right"></i>
                  </span>
              </a>
              <ul class="treeview-menu">
                  <li><a href="#"><i class="fa fa-circle-o"></i>First Year</a></li>
                  <li><a href="#"><i class="fa fa-circle-o"></i>Second Year</a></li>
                  <li><a href="#"><i class="fa fa-circle-o"></i>Third Year</a></li>
              </ul>
          </li>
          <li class="treeview">
              <a href="#">
                  <i class="fa fa-book"></i>
                  <span>Provide Notes</span>
                  <span class="pull-right-container">
                      <i class="fa fa-angle-left pull-right"></i>
                  </span>
              </a>
              <ul class="treeview-menu">
                  <li><a href="#"><i class="fa fa-circle-o"></i>First Year</a></li>
                  <li><a href="#"><i class="fa fa-circle-o"></i>Second Year</a></li>
                  <li><a href="#"><i class="fa fa-circle-o"></i>Third Year</a></li>
              </ul>
          </li>
          <li class="treeview">
              <a href="#">
                  <i class="fa fa-bell"></i>
                  <span>Provide Notice</span>

              </a>
          </li>
          <li class="treeview">
              <a href="#">
                  <i class="fa fa-phone"></i>
                  <span>Contact Parents</span>
                  <span class="pull-right-container">
                      <i class="fa fa-angle-left pull-right"></i>
                  </span>
              </a>
              <ul class="treeview-menu">
                  <li><a href="#"><i class="fa fa-circle-o"></i>First Year</a></li>
                  <li><a href="#"><i class="fa fa-circle-o"></i>Second Year</a></li>
                  <li><a href="#"><i class="fa fa-circle-o"></i>Third Year</a></li>
              </ul>
          </li>

          <li class="treeview">
          <a href="#">
            <i class="fa fa-question"></i>
            <span>Forum Discussion</span>



          </a>

        </li>





    </section>
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <!--<h1>
        Dashboard
        <small>Control panel</small>
      </h1>-->

    </section>

    <!-- Main content -->
    <section class="content">


        <div class="container">
            <div>
                <h2>
                <asp:Label ID="lbl1" runat="server" Text="First Year"></asp:Label>
                    </h2>
            </div>
    <div>
        <div class="input-group">
            <span class="input-group-addon">Note</span>
            <asp:TextBox ID="TextBox1" runat="server" CssClass="form-control" placeholder="Note Name" Text=""></asp:TextBox>
            </div>
        <br />
        <div class="input-group">
            <span class="input-group-addon">Description</span>
        <asp:TextBox ID="TextBox2" runat="server" CssClass="form-control" placeholder="Note Description" TextMode="MultiLine" Text=""></asp:TextBox>
            </div>
        <br />
        <div style="color:cornflowerblue"></div>
        <asp:FileUpload ID="FileUpload1" runat="server" CssClass="btn btn-primary" />
        <br />
        <asp:LinkButton ID="LinkButton1" runat="server" CssClass="btn btn-primary" OnClick="LinkButton1_Click">Upload <span class="fa fa-upload"></span></asp:LinkButton>
    </div>
            </div>

     </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  <footer class="main-footer">
    <div class="pull-right hidden-xs">
      <b>Version</b> 2.3.8
    </div>
    <strong>Copyright &copy; 2014-2016 <a href="http://almsaeedstudio.com">Almsaeed Studio</a>.</strong> All rights
    reserved.
  </footer>

  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
    <!-- Create the tabs -->
    <ul class="nav nav-tabs nav-justified control-sidebar-tabs">
      <li><a href="#control-sidebar-home-tab" data-toggle="tab"><i class="fa fa-home"></i></a></li>
      <li><a href="#control-sidebar-settings-tab" data-toggle="tab"><i class="fa fa-gears"></i></a></li>
    </ul>
    <!-- Tab panes -->
    <div class="tab-content">
      <!-- Home tab content -->
      <div class="tab-pane" id="control-sidebar-home-tab">
        <h3 class="control-sidebar-heading">Recent Activity</h3>
        <ul class="control-sidebar-menu">
          <li>
            <a href="javascript:void(0)">
              <i class="menu-icon fa fa-birthday-cake bg-red"></i>

              <div class="menu-info">
                <h4 class="control-sidebar-subheading">Langdon's Birthday</h4>

                <p>Will be 23 on April 24th</p>
              </div>
            </a>
          </li>
          <li>
            <a href="javascript:void(0)">
              <i class="menu-icon fa fa-user bg-yellow"></i>

              <div class="menu-info">
                <h4 class="control-sidebar-subheading">Frodo Updated His Profile</h4>

                <p>New phone +1(800)555-1234</p>
              </div>
            </a>
          </li>
          <li>
            <a href="javascript:void(0)">
              <i class="menu-icon fa fa-envelope-o bg-light-blue"></i>

              <div class="menu-info">
                <h4 class="control-sidebar-subheading">Nora Joined Mailing List</h4>

                <p>nora@example.com</p>
              </div>
            </a>
          </li>
          <li>
            <a href="javascript:void(0)">
              <i class="menu-icon fa fa-file-code-o bg-green"></i>

              <div class="menu-info">
                <h4 class="control-sidebar-subheading">Cron Job 254 Executed</h4>

                <p>Execution time 5 seconds</p>
              </div>
            </a>
          </li>
        </ul>
        <!-- /.control-sidebar-menu -->

        <h3 class="control-sidebar-heading">Tasks Progress</h3>
        <ul class="control-sidebar-menu">
          <li>
            <a href="javascript:void(0)">
              <h4 class="control-sidebar-subheading">
                Custom Template Design
                <span class="label label-danger pull-right">70%</span>
              </h4>

              <div class="progress progress-xxs">
                <div class="progress-bar progress-bar-danger" style="width: 70%"></div>
              </div>
            </a>
          </li>
          <li>
            <a href="javascript:void(0)">
              <h4 class="control-sidebar-subheading">
                Update Resume
                <span class="label label-success pull-right">95%</span>
              </h4>

              <div class="progress progress-xxs">
                <div class="progress-bar progress-bar-success" style="width: 95%"></div>
              </div>
            </a>
          </li>
          <li>
            <a href="javascript:void(0)">
              <h4 class="control-sidebar-subheading">
                Laravel Integration
                <span class="label label-warning pull-right">50%</span>
              </h4>

              <div class="progress progress-xxs">
                <div class="progress-bar progress-bar-warning" style="width: 50%"></div>
              </div>
            </a>
          </li>
          <li>
            <a href="javascript:void(0)">
              <h4 class="control-sidebar-subheading">
                Back End Framework
                <span class="label label-primary pull-right">68%</span>
              </h4>

              <div class="progress progress-xxs">
                <div class="progress-bar progress-bar-primary" style="width: 68%"></div>
              </div>
            </a>
          </li>
        </ul>
        <!-- /.control-sidebar-menu -->

      </div>
      <!-- /.tab-pane -->
      <!-- Stats tab content -->
      <div class="tab-pane" id="control-sidebar-stats-tab">Stats Tab Content</div>
      <!-- /.tab-pane -->
      <!-- Settings tab content -->
      <div class="tab-pane" id="control-sidebar-settings-tab">
        <form method="post">
          <h3 class="control-sidebar-heading">General Settings</h3>

          <div class="form-group">
            <label class="control-sidebar-subheading">
              Report panel usage
              <input type="checkbox" class="pull-right" checked>
            </label>

            <p>
              Some information about this general settings option
            </p>
          </div>
          <!-- /.form-group -->

          <div class="form-group">
            <label class="control-sidebar-subheading">
              Allow mail redirect
              <input type="checkbox" class="pull-right" checked>
            </label>

            <p>
              Other sets of options are available
            </p>
          </div>
          <!-- /.form-group -->

          <div class="form-group">
            <label class="control-sidebar-subheading">
              Expose author name in posts
              <input type="checkbox" class="pull-right" checked>
            </label>

            <p>
              Allow the user to show his name in blog posts
            </p>
          </div>
          <!-- /.form-group -->

          <h3 class="control-sidebar-heading">Chat Settings</h3>

          <div class="form-group">
            <label class="control-sidebar-subheading">
              Show me as online
              <input type="checkbox" class="pull-right" checked>
            </label>
          </div>
          <!-- /.form-group -->

          <div class="form-group">
            <label class="control-sidebar-subheading">
              Turn off notifications
              <input type="checkbox" class="pull-right">
            </label>
          </div>
          <!-- /.form-group -->

          <div class="form-group">
            <label class="control-sidebar-subheading">
              Delete chat history
              <a href="javascript:void(0)" class="text-red pull-right"><i class="fa fa-trash-o"></i></a>
            </label>
          </div>
          <!-- /.form-group -->
        </form>
      </div>
      <!-- /.tab-pane -->
    </div>
  </aside>
  <!-- /.control-sidebar -->
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->

<!-- jQuery 2.2.3 -->
<script src="plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script>

        $(document).ready(function () {

            $("#HOD_Panel").click(function () {

                $("#Add_HOD").load("AddHOD/ahod.aspx");
                });
        });
        $(document).ready(function () {
            $("#HOD_Panel").click(function () {
                $("#Remove_HOD").load("AddHOD/RemoveHOD.aspx");
            });
        });

    </script>
<!-- jQuery UI 1.11.4 -->
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
<script>
  $.widget.bridge('uibutton', $.ui.button);
</script>
<!-- Bootstrap 3.3.6 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- Morris.js charts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="plugins/morris/morris.min.js"></script>
<!-- Sparkline -->
<script src="plugins/sparkline/jquery.sparkline.min.js"></script>
<!-- jvectormap -->
<script src="plugins/jvectormap/jquery-jvectormap-1.2.2.min.js"></script>
<script src="plugins/jvectormap/jquery-jvectormap-world-mill-en.js"></script>
<!-- jQuery Knob Chart -->
<script src="plugins/knob/jquery.knob.js"></script>
<!-- daterangepicker -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
<script src="plugins/daterangepicker/daterangepicker.js"></script>
<!-- datepicker -->
<script src="plugins/datepicker/bootstrap-datepicker.js"></script>
<!-- Bootstrap WYSIHTML5 -->
<script src="plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js"></script>
<!-- Slimscroll -->
<script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="plugins/fastclick/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/app.min.js"></script>
<!-- AdminLTE dashboard demo (This is only for demo purposes) -->
<script src="dist/js/pages/dashboard.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="dist/js/demo.js"></script>
        </form>
</body>
</html>

表设计:

ID int primary key identity,
Note_Name nvarchar(max) not null,
Description nvarchar(max) not null,
Name nvarchar(max),
Content_Type nvarchar(max),
Data varbinary(max)

1 个答案:

答案 0 :(得分:0)

因为您没有致电ExecuteNonQuery() menthod:

SqlCommand cmd = new SqlCommand("INSERT INTO Notes VALUES (@Note_Name,@Description,@Name,@Content_Type,@Data)",con);
cmd.Parameters.AddWithValue("@Note_Name",TextBox1.Text.Trim());
cmd.Parameters.AddWithValue("@Description", TextBox2.Text.Trim());
cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = fileName;
cmd.Parameters.Add("@Content_Description", SqlDbType.NVarChar).Value = contentType;
cmd.Parameters.Add("@Data", SqlDbType.VarBinary).Value = bytes;

int recordsAffected = cmd.ExecuteNonQuery();