简体   繁体   English

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

[英]Display inserted data in a table after inserting into Database using c# Asp.NET

I need when user will submit data and after it will submitted successfully those data will display in a table. 我需要用户何时提交数据,成功提交之后,这些数据将显示在表格中。

Here is my code: 这是我的代码:

mission.aspx : 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 : 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 : 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 : 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();
            }
        }
    }
}

I am using 3-tier architecture in c# ASP.NET. 我在c#ASP.NET中使用3层体系结构。 I would like to retrieve data from DB and add in table. 我想从数据库中检索数据并添加到表中。

OK. 好。

I would suggest you to replace your table with GridView for simplicity 为了简单起见,我建议您将表格替换为GridView

and

Just after you insert record, write this code in aspx.cs file just below insertion code 插入记录后,将此代码写入插入代码下方的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();

as you are using N-Tier, you can put this code in separate layer and call it in aspx.cs 当您使用N-Tier时,可以将此代码放在单独的层中,并在aspx.cs中调用它

For inserting image in database refer this and to retrieve that refer this 要在数据库中插入图像, 请参考此内容 ,要检索该内容, 请参考此内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM