locked
insertion and update not performed at a time in gridview? RRS feed

  • Question

  • User-1026236167 posted

    hello sir firstly i tell u i have two issues 

    1) when i used script manager and update panel only two textbox data is filled from the gridview edit button but i used image as well image path does not filled to source file upload option when clicked on link button edit 

    2)i used script manager as well as update panel if i remove this or comment it then the insertion operation can done successsfully with showed the image in the gridview and update on edit button gridview does not fill to the source textboxes but (2nd) if i used script manager and update panel insertion can be performed but image cant be showed in gridview edit button updation performed     

    please execute them

    here is my code

    cs

    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.IO;
    using System.Configuration;


    namespace WebApplication14
    {
    public partial class WebForm69 : System.Web.UI.Page
    {
    SqlCommand cmd = new SqlCommand();
    SqlConnection con = new SqlConnection();
    string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();

    public void EstablishConnection(string storeprocedure)
    {
    con.ConnectionString = connection;
    cmd.Connection = con;
    cmd.Connection.Open();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = storeprocedure;
    }

    public void CloseConnection()
    {
    cmd.Connection.Close();
    cmd.Connection.Dispose();
    con.Close();
    }


    public void FillGridview()
    {
    SqlDataAdapter adp = new SqlDataAdapter("select * from test_image", connection);
    DataTable DT = new DataTable();
    adp.Fill(DT);
    Gv1.DataSource = DT;
    Gv1.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    FillGridview();

    }

    }


    protected void BT_submit_Click(object sender, EventArgs e)
    {
    //string filename = Path.GetFileName(fileupload.PostedFile.FileName);

    //fileupload.SaveAs(Server.MapPath("~/img/" + filename));
    if (fileupload.HasFiles)
    {


    string fileName = Path.GetFileName(fileupload.PostedFile.FileName);
    string filePath = "~/img/" + fileName;

    fileupload.PostedFile.SaveAs(Server.MapPath(filePath));

    EstablishConnection("sp_test_image");
    cmd.Parameters.Add("@first_name", SqlDbType.NVarChar, 100).Value = tb_name.Text;
    cmd.Parameters.Add("@last_name", SqlDbType.NVarChar, 100).Value = tb_last.Text;
    cmd.Parameters.Add("@Remarks", SqlDbType.VarChar, 100).Value = "1";
    cmd.Parameters.Add("@date_time", SqlDbType.VarChar, 100).Value = System.DateTime.Now.ToString();
    cmd.Parameters.Add("@Status", SqlDbType.VarChar, 100).Value = "1";
    cmd.Parameters.Add("@By_whom", SqlDbType.VarChar, 100).Value = Session["username"].ToString();
    cmd.Parameters.Add("@image", SqlDbType.NVarChar, 255).Value = filePath;
    FillGridview();

    try { cmd.ExecuteNonQuery(); }

    catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); }

    CloseConnection();


    }
    else
    {
    EstablishConnection("sp_test_image");
    cmd.Parameters.Add("@first_name", SqlDbType.NVarChar, 100).Value = tb_name.Text;
    cmd.Parameters.Add("@last_name", SqlDbType.NVarChar, 100).Value = tb_last.Text;
    cmd.Parameters.Add("@Remarks", SqlDbType.VarChar, 100).Value = "1";
    cmd.Parameters.Add("@date_time", SqlDbType.VarChar, 100).Value = System.DateTime.Now.ToString();
    cmd.Parameters.Add("@Status", SqlDbType.VarChar, 100).Value = "1";
    cmd.Parameters.Add("@By_whom", SqlDbType.VarChar, 100).Value = Session["username"].ToString();
    cmd.Parameters.Add("@image", SqlDbType.NVarChar, 255).Value = "null";


    FillGridview();

    try { cmd.ExecuteNonQuery(); }

    catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); }
    CloseConnection();

    }
    }


    protected void BT_update_Click(object sender, EventArgs e)
    {
    string fileName = Path.GetFileName(fileupload.PostedFile.FileName);
    string filePath = "~/img/" + fileName;

    fileupload.PostedFile.SaveAs(Server.MapPath(filePath));

    EstablishConnection("sp_test_image1");
    cmd.Parameters.Add("@first_name", SqlDbType.NVarChar, 100).Value = tb_name.Text;
    cmd.Parameters.Add("@last_name", SqlDbType.NVarChar, 100).Value = tb_last.Text;
    cmd.Parameters.Add("@image", SqlDbType.VarChar, 255).Value = filePath;
    cmd.Parameters.Add("@tbl_id", SqlDbType.Decimal, 30).Value = HiddenField1.Value;

    try { cmd.ExecuteNonQuery(); }
    catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); }


    CloseConnection();
    FillGridview();
    Response.Redirect(Request.Url.AbsoluteUri);
    }

    //protected void Gv1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    // Gv1.PageIndex = e.NewPageIndex;
    // FillGridview();
    //}

    protected void Button1_Click(object sender, EventArgs e)
    {
    LinkButton btn = (LinkButton)sender;
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;
    tb_name.Text = gvr.Cells[0].Text;
    tb_last.Text = gvr.Cells[1].Text;

    HiddenField1.Value = Gv1.DataKeys[gvr.RowIndex].Values[0].ToString();


    BT_update.Visible = true;
    BT_submit.Visible = false;
    }


    }
    }

    aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="WebForm69.aspx.cs" Inherits="WebApplication14.WebForm69" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="server">

    <div style="margin-top:30px; margin-left:20px">

    <asp:HiddenField ID="HiddenField1" runat="server" />



    <h1><strong style="color:black">Test Language</strong></h1>
    <hr />

    <table>

    <tr>
    <td><span style="color:black">First Name</span><br />
    <asp:TextBox ID ="tb_name" runat="server" Width="200px" /><br />


    </td>

    <td><span style="margin-left:25px; color:black">Last Name<br /></span>
    <span style="margin-left:25px"><asp:TextBox ID="tb_last" runat="server" Width="200px" /></span> <br />

    </td></tr>
    <tr>
    <td>
    <br /><asp:FileUpload ID="fileupload" runat="server" BackColor="lightblue" />
    </td>
    </tr>

    <tr>

    <td><br />
    <asp:Button ID="BT_submit" runat="server" Text="Submit" style="border-radius:5px" Height="35px" Width="80px" OnClick="BT_submit_Click" BackColor="#3366CC" BorderColor="#3366CC" ForeColor="White" />
    <asp:Button ID="BT_update" runat="server" Text="Update" style="border-radius:5px" Height="35px" Width="80px" OnClick="BT_update_Click" BackColor="#3366CC" BorderColor="#3366CC" ForeColor="White" />
    </td>

    </tr>
    </table>
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>

    <asp:GridView runat="server" ID="Gv1" AutoGenerateColumns="false" Width="60%" DataKeyNames="tbl_id" >
    <Columns>
    <asp:BoundField DataField ="first_name" HeaderText ="First Name" ItemStyle-HorizontalAlign="Center" />
    <asp:BoundField DataField ="last_Name" HeaderText ="Last Name" />
    <asp:ImageField DataImageUrlField="image" HeaderText="Image" ControlStyle-Height="95" ControlStyle-Width="170" ItemStyle-HorizontalAlign="Center">
    </asp:ImageField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="Button1" runat="server" Text="Edit" OnClick="Button1_Click" ></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>


    </ContentTemplate>
    </asp:UpdatePanel>

    </div>
    </asp:Content>

    Thursday, July 2, 2020 12:21 PM

All replies

  • User348806598 posted

    When using an update panel you should use ScriptManager.RegisterStartupScript

    ScriptManager.RegisterStartupScript(UpdatePanelID,UpdatePanelID.GetType(), "alert('Hello World')", CloseWindow, true);
    Thursday, July 2, 2020 12:30 PM
  • User-1026236167 posted

    sir how to apply this code line in my code please execute them?

    Friday, July 3, 2020 5:22 AM
  • User-939850651 posted

    Hi prabhjot1313,

    <tr>
    <td><span style="color:black">First Name</span><br />
    <asp:TextBox ID ="tb_name" runat="server" Width="200px" /><br />
    </td>
    <td><span style="margin-left:25px; color:black">Last Name<br /></span>
    <span style="margin-left:25px"><asp:TextBox ID="tb_last" runat="server" Width="200px" /></span> <br />
    </td>
    </tr> <tr> <td> <br /><asp:FileUpload ID="fileupload" runat="server" BackColor="lightblue" /> </td> </tr>


    If I read correctly, your code is filled with only two textboxes, and no controls for displaying pictures ( Code marked in yellow ). You should add Image control to display it.

    protected void Button1_Click(object sender, EventArgs e)
    {
    LinkButton btn = (LinkButton)sender;
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;
    tb_name.Text = gvr.Cells[0].Text;
    tb_last.Text = gvr.Cells[1].Text;
    
    HiddenField1.Value = Gv1.DataKeys[gvr.RowIndex].Values[0].ToString();
    
    BT_update.Visible = true;
    BT_submit.Visible = false;
    }

    In this method, you also need to set a image url to Image control. 

    Best regards,

    Xudong Peng

    Friday, July 3, 2020 9:44 AM