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

时间:2015-06-25 09:06:53

标签: c# asp.net

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

这是我的代码:

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中使用3层架构。 我想从DB检索数据并添加到表中。

1 个答案:

答案 0 :(得分:0)

行。

为了简单起见,我建议您将表格替换为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中调用它

用于在数据库refer this中插入图片并检索refer this