使用c#Asp.NET将数据插入数据库后,在表中显示插入的数据

本文关键字:数据 插入 显示 数据库 c#Asp NET 使用 | 更新日期: 2023-09-27 17:59:42

我需要用户何时提交数据,成功提交后,这些数据将显示在表中。

这是我的代码:

mission.aspx:

<div class="col-md-6 bannerimagefile">
  <label for="heading" accesskey="T">
    <span class="required">*</span> Heading
  </label>
  <asp:TextBox ID="TextBox1" runat="server"  size="30" value="" ></asp:TextBox>
  <asp:CustomValidator ID="CustomValidator1" runat="server"
           ErrorMessage="have to fill at least 1 field"
           ControlToValidate="TextBox1"
           ClientValidationFunction="doCustomValidate"
           ValidateEmptyText="true" ></asp:CustomValidator>
  <label for="insertimage" accesskey="B">
    <span class="required">*</span> Insert Image
  </label>
  <asp:FileUpload runat="server" class="filestyle" data-size="lg" name="insertimage" id="insertimage" />
  <asp:CustomValidator ID="CustomValidator2" runat="server"
          ErrorMessage="have to fill at least 1 field"
          ControlToValidate="insertimage"
          ClientValidationFunction="doCustomValidate"
          ValidateEmptyText="true" ></asp:CustomValidator>
  <label for="bannerimage" accesskey="V">
    <span class="required">*</span> View Image
  </label>
  <div style="padding-bottom:10px;">
    <img src="images/resource/me.jpg" border="0" name="bannerimage" style="width:70px; height:70px;">
  </div>
  <div class="clear"></div>
</div>
<div class="col-md-6">
  <label for="description" accesskey="D">
    <span class="required">*</span> Description
  </label>
  <asp:TextBox ID="TextBox2" runat="server" name="description" cols="40" multi="" Rows="7" TextMode="MultiLine"></asp:TextBox>
  <asp:CustomValidator ID="CustomValidator3" runat="server"
           ErrorMessage="have to fill at least 1 field"
           ControlToValidate="TextBox2"
           ClientValidationFunction="doCustomValidate"
           ValidateEmptyText="true" ></asp:CustomValidator>
  <asp:Button  runat="server" Text="Submit" class="submit" id="submit" onclick="submit_Click" />
</div>
</div>
</div>
</div>
<table class="table table-striped table-bordered margin-top-zero">
  <colgroup>
    <col class="col-md-1 col-sm-1">
    <col class="col-md-4 col-sm-4">
    <col class="col-md-2 col-sm-2">
    <col class="col-md-4 col-sm-4">
    <col class="col-md-1 col-sm-1">
  </colgroup>
  <thead>
    <tr>
      <th>Sl. No</th>
      <th>Heading</th>
      <th>Image</th>
      <th>Description</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Mission</td>
      <td></td>
      <td></td>
      <td>
        <a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-success" data-original-title="Edit">
          <i class="fa fa-edit"></i>
        </a>
        <a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-danger" data-original-title="Delete">
          <i class="fa fa-times"></i>
        </a>
      </td>
    </tr>
  </tbody>
</table>

mission.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObject;
using BusinessLogic;
namespace ODIYA_Doctor_Admin
{
    public partial class missionvision : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void submit_Click(object sender, EventArgs e)
        {
            missionBO objMissionBo = new missionBO();
            objMissionBo.heading = TextBox1.Text.Trim();
            if (insertimage.HasFile)
            {
                int length = insertimage.PostedFile.ContentLength;
                byte[] imgbyte = new byte[length];
                HttpPostedFile img = insertimage.PostedFile;
                img.InputStream.Read(imgbyte, 0, length);
                objMissionBo.image = imgbyte;
            }
            objMissionBo.description = TextBox2.Text.Trim();
            missionvissionBL objMissionBL = new missionvissionBL();
            string action = "insert";
            var result = objMissionBL.insertMissionData(objMissionBo,action);
            if (result == 1)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "Data has been Inserted", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "Data could not inserted", true);
            }
        }
    }
}

missionBL.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessObject;
using DataAccess;
namespace BusinessLogic
{
    public class missionvissionBL
    {
        public int insertMissionData(missionBO objMissionBo,string action)
        {
            try
            {
                missionDL objMissionDL = new missionDL();
                int result = 0;
                if (action == "insert")
                {
                    result = objMissionDL.insertMissionData(objMissionBo, action);
                }
                return result;
            }
            catch
            {
                throw;
            }
        }
    }
}

missionDL.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessObject;
using GenClassLibrary;
namespace DataAccess
{
   public class missionDL
    {
        SqlConnection con = new SqlConnection(CmVar.convar);
        GenClass ob = new GenClass();
        public int insertMissionData(missionBO objMissionBo,string action)
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("odMissionVission", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Heading", objMissionBo.heading);
                cmd.Parameters.AddWithValue("@Description", objMissionBo.description);
                cmd.Parameters.AddWithValue("@Image", objMissionBo.image);
                cmd.Parameters.AddWithValue("@StatementType", action);
                cmd.Parameters.Add("@flag", SqlDbType.Int).Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                int Result = (int)cmd.Parameters["@flag"].Value;
                cmd.Dispose();
                return Result;
            }
            catch
            {
                throw;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
    }
}

我在c#ASP.NET中使用三层体系结构。我想从数据库和加载项表中检索数据。

使用c#Asp.NET将数据插入数据库后,在表中显示插入的数据

好。

为了简化,我建议你把你的桌子换成GridView

插入记录后,在插入代码下方的aspx.cs文件中写入此代码

  DataTable dt = new DataTable();
        using (var con = new SqlConnection("Your-Connection-string-here"))
        {
            using (var cmd = new SqlCommand("select * from your-table", con)
            {
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
            }
        }
        your-grid.DataSourse = null;
        your-grid.DataSourse = dt;
        your-grid.DataBind();

当您使用N-Tier时,您可以将此代码放在单独的层中,并在aspx.cs中调用它

要在数据库中插入图像,请参阅此并检索引用此的图像