2015-06-25 109 views
0

我需要當用戶提交數據,並在成功提交後,這些數據將顯示在表中。用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中檢索數據並添加到表中。

+0

哪個表?而這段代碼只會插入記錄,你試圖顯示什麼?我無法爲你寫完整的代碼。粘貼你的努力,我會糾正 – Imad

+0

@ Imadoddin:你可以給這個想法或只寫一個值。 – satya

+0

我的理解是,你只是插入記錄在數據庫中,並希望顯示在gridview?我對嗎? – Imad

回答

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層,您可以將此代碼放在單獨的圖層中並在aspx.cs中調用它

For in在數據庫refer this中停留圖像並檢索該圖像refer this

+0

可以根據我的表格自定義gridview。 ? – satya

+0

Ofcourse ....... – Imad

+0

@ Imadoddin:好的..我做到了這一點,但在GridView中,我得到的圖像路徑,但我需要在那個特定的地方使用該路徑顯示圖像,我將如何做到這一點。 – satya

相關問題