Get image name from image Path from FormView and save in Database using C# in ASP.Net

Last Reply 11 months ago By micah

Posted one year ago

 

protected void btnSharenow_Click(object sender, EventArgs e)
  
    {
        string modelId = (SharePost.Row.FindControl("lblModalId") as Label).Text;
        string userName = (SharePost.Row.FindControl("lblUserName") as Label).Text;
        string fuserName = (SharePost.Row.FindControl("lblfUserName") as Label).Text;
        string contentPost = (SharePost.Row.FindControl("lblpost") as Label).Text;
        string comment = (SharePost.Row.FindControl("txtcommentshare") as TextBox).Text;
       string img = (SharePost.Row.FindControl("img") as Image).;

Also i need to insert these correctly but i dont know how to do that

 

private void InsertDatabase(string userName, string fuserName, string contentPost, DateTime sendDate, string modelId, string comment)
    {
        using (SqlConnection con = new SqlConnection(constrr))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO USERPost VALUES(@UserName,@FriendUserName,@ContentPost,,@ImageName1,@SendDate,@ShareId,@Comments,@FriendImageName,@null,@null)", con))
            {
               // cmd.Parameters.AddWithValue("@ShareId", modelId);
                cmd.Parameters.AddWithValue("@UserName", username = this.Page.User.Identity.Name);
                cmd.Parameters.AddWithValue("@FriendUserName", fuserName);
                cmd.Parameters.AddWithValue("@ContentPost", contentPost);
                cmd.Parameters.AddWithValue("@SendDate", sendDate);
                cmd.Parameters.AddWithValue("@ShareId", modelId);
                cmd.Parameters.AddWithValue("@Comments", comment);
                cmd.Parameters.AddWithValue("@FriendImageName", img2);
                cmd.Parameters.AddWithValue("@TotalCount", );
                cmd.Parameters.AddWithValue("@ADStatus", );
                cmd.Parameters.AddWithValue("@ImageName1", umg);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

 

Results 1 - 5 of 15 123
Posted one year ago

Hi micah,

C#

string img = (SharePost.Row.FindControl("img") as Image).;

Replace above code with below.

  string img = (SharePost.Row.FindControl("img") as Image).ImageUrl;

 


Posted one year ago

what of this

private void InsertDatabase(string userName, string fuserName, string contentPost, DateTime sendDate, string modelId, string comment)
    {
        using (SqlConnection con = new SqlConnection(constrr))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO USERPost VALUES(@UserName,@FriendUserName,@ContentPost,,@ImageName1,@SendDate,@ShareId,@Comments,@FriendImageName,@null,@null)", con))
            {
               // cmd.Parameters.AddWithValue("@ShareId", modelId);
                cmd.Parameters.AddWithValue("@UserName", username = this.Page.User.Identity.Name);
                cmd.Parameters.AddWithValue("@FriendUserName", fuserName);
                cmd.Parameters.AddWithValue("@ContentPost", contentPost);
                cmd.Parameters.AddWithValue("@SendDate", sendDate);
                cmd.Parameters.AddWithValue("@ShareId", modelId);
                cmd.Parameters.AddWithValue("@Comments", comment);
                cmd.Parameters.AddWithValue("@FriendImageName", img2);
                cmd.Parameters.AddWithValue("@TotalCount", );
                cmd.Parameters.AddWithValue("@ADStatus", );
                cmd.Parameters.AddWithValue("@ImageName1", umg);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

the problem i have with this code above it that those rows that does not have data to insert is causing error when trying to insert

it tells me number of supplies not match the number of columns. So i decided to insert null into those rows. do you know how to handel that?


Posted one year ago

Hi micah,

 C#

private void InsertDatabase(string userName, string fuserName, string contentPost, DateTime sendDate, string modelId, string comment)
    {
        using (SqlConnection con = new SqlConnection(constrr))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO USERPost VALUES(@UserName,@FriendUserName,@ContentPost,,@ImageName1,@SendDate,@ShareId,@Comments,@FriendImageName,@null,@null)", con))
            {
               // cmd.Parameters.AddWithValue("@ShareId", modelId);
                cmd.Parameters.AddWithValue("@UserName", username = this.Page.User.Identity.Name);
                cmd.Parameters.AddWithValue("@FriendUserName", fuserName);
                cmd.Parameters.AddWithValue("@ContentPost", contentPost);
                cmd.Parameters.AddWithValue("@SendDate", sendDate);
                cmd.Parameters.AddWithValue("@ShareId", modelId);
                cmd.Parameters.AddWithValue("@Comments", comment);
                cmd.Parameters.AddWithValue("@FriendImageName", img2);
                cmd.Parameters.AddWithValue("@TotalCount", );
                cmd.Parameters.AddWithValue("@ADStatus", );
                cmd.Parameters.AddWithValue("@ImageName1", umg);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

Replace above code with below.

private void InsertDatabase(string userName, string fuserName, string contentPost, DateTime sendDate, string modelId, string comment)
{
    using (SqlConnection con = new SqlConnection(constring))
    {
        using (SqlCommand cmd = new SqlCommand("INSERT INTO USERPost VALUES(@UserName,@FriendUserName,@ContentPost,@ImageName1,@SendDate,@ShareId,@Comments,@FriendImageName)", con))
        {
            // cmd.Parameters.AddWithValue("@ShareId", modelId);
            cmd.Parameters.AddWithValue("@UserName", username = this.Page.User.Identity.Name);
            cmd.Parameters.AddWithValue("@FriendUserName", fuserName);
            cmd.Parameters.AddWithValue("@ContentPost", contentPost);
            cmd.Parameters.AddWithValue("@ImageName1", umg);
            cmd.Parameters.AddWithValue("@SendDate", sendDate);
            cmd.Parameters.AddWithValue("@ShareId", modelId);
            cmd.Parameters.AddWithValue("@Comments", comment);
            cmd.Parameters.AddWithValue("@FriendImageName", img2);
            cmd.Parameters.AddWithValue("@TotalCount", DBNull.Value);
            cmd.Parameters.AddWithValue("@ADStatus", DBNull.Value);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

 


Posted one year ago Modified on one year ago

error

Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 101:        string contentPost = (SharePost.Row.FindControl("lblpost") as Label).Text;
Line 102:        string comment = (SharePost.Row.FindControl("txtcommentshare") as TextBox).Text;
Line 103:        string img = (SharePost.Row.FindControl("Img") as Image).ImageUrl;
Line 104:
Line 105:

 html markup

 <img src='<%#getSRCfuser(Container.DataItem)%>' ID="Img" class="media-object img-rounded animated fadeInDown" style=" width:45px; height:45px; "  data-toggle="tooltip" title="Profile Photo"/>

 


Posted one year ago Modified on one year ago

it still didnt work, still same error.

please let me pase the whole code, i call users post from datalist to formview in modal popup, then insert that post of the user into another table, i have done that and its working. my challenge is to also fetch the image of the user in that post and insert it along side with the post. see example here

http://www.aspforums.net/Threads/161293/Insert-Bootstrap-Modal-Popup-data-into-Database-using-C-in-ASPNet/

this is the codes

html

 

<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
    <script type='text/javascript'>
        function openModal() {
            $('#myModal').modal('show');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel ID="updResult" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:DataList ID="GetMergedAll" runat="server" DataKeyName="CategoryID" OnItemCommand="GetMergedAll_ItemCommand1">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"CategoryName") %>'
                        Font-Bold="True " ForeColor="" Font-Size="" />
                    </h4>
                    <asp:Label ID="lblpost" runat="server" Text='<%# Eval("Description")%>' Font-Names="Comic Sans MS"
                        Font-Bold="False" Font-Strikeout="False" ForeColor="#333333">
                    </asp:Label>
                    <asp:Button ID="btnClick" Text="Click" runat="server" CommandName="SharePost" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"CategoryID") %>'
                        OnClick="btnClick_Click" />
                </ItemTemplate>
            </asp:DataList>
        </ContentTemplate>
    </asp:UpdatePanel>
    <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        &times;</button>
                    <h4 class="modal-title">
                        Modal Header</h4>
                </div>
                <div class="modal-body">
                    <asp:UpdatePanel ID="UpdatePanel2model" runat="server">
                        <ContentTemplate>
                            <p>
                            </p>
                            <br />
                            <asp:FormView ID="SharePost" runat="server" CssClass=" table table-bordered table-striped table-hover">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtComment" runat="server" placeholder="Add Comment... " TextMode="MultiLine"
                                        CssClass="form-control" Height="80px"></asp:TextBox>
                                    <!-- Conversations are loaded here -->
                                    <div class="">
                                        <!-- Message. Default to the left -->
                                        <div class="" style="margin-bottom: 1px; margin-left: 8px; margin-top: 8px; margin-right: 8px">
                                            <div class=" " style="">
                                                <ul class="media-list">
                                                    <li class="media">
                                                        <div class="media-body">
                                                            <h5 class="media-heading">
                                                                <asp:Label ID="lblCategoryId" runat="server" Text='<%#Eval("CategoryID")%>' Font-Bold="True "
                                                                    ForeColor="#666666" />
 <a  style="margin-bottom:2px;">
   
           
                                       <img src='<%#getSRCfuser(Container.DataItem)%>'  class="media-object img-rounded animated fadeInDown" style=" width:45px; height:45px; "  data-toggle="tooltip" title="Profile Photo"/>

                                              
                           
                                                        
    </a>
                                                                <asp:Label ID="lblCategoryName" runat="server" Text='<%#Eval("CategoryName")%>' Font-Bold="True "
                                                                    ForeColor="#666666" />
                                                                <br />
                                                                <asp:Label ID="lblDescription" runat="server" Text='<%#Eval("Description")%>' Font-Size="X-Small"
                                                                    Font-Bold="True" ForeColor="Gray" />
                                                                <div class="clearfix">
                                                                </div>
                                                            </h5>
                                                            <div class=" col-lg-12" style="margin-top: 2px; margin-bottom: 2px">
                                                            </div>
                                                            <!-- Nested media object -->
                                                        </div>
                                                    </li>
                                                </ul>
                                            </div>
                                        </div>
                                        <!--/.direct-chat-messages-->
                                        <!-- Contacts are loaded here -->
                                    </div>
                                    <!-- /.box-body -->
                                    <asp:Button ID="btnSubmit" type="button" Text="Submit" OnClick="OnSubmit" class="btn btn-default"
                                        runat="server" />
                                </ItemTemplate>
                            </asp:FormView>
                        </ContentTemplate>
                        <Triggers>
                            <asp:PostBackTrigger ControlID="GetMergedAll" />
                        </Triggers>
                    </asp:UpdatePanel>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">
                        Close</button>
                </div>
            </div>
        </div>
    </div>
    </form>
</body>

code

 

System.Text.StringBuilder sb = new System.Text.StringBuilder();
private string constring = ConfigurationManager.ConnectionStrings["Constring"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = GetData();
            ViewState["DataTable"] = dt;
            GetMergedAll.DataSource = dt;
            GetMergedAll.DataBind();
        }
    }
}
 
private static DataTable GetData()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("CategoryID", typeof(int)), new DataColumn("CategoryName"), new DataColumn("Description") });
    dt.Rows.Add(1, "Category 1", "Description 1");
    dt.Rows.Add(2, "Category 2", "Description 2");
    dt.Rows.Add(3, "Category 3", "Description 3");
    dt.Rows.Add(4, "Category 4", "Description 4");
    return dt;
}
 
protected void btnClick_Click(object sender, EventArgs e)
{
    ClientScript.RegisterStartupScript(this.GetType(), "Pop", "openModal();", true);
}
 
protected void GetMergedAll_ItemCommand1(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "SharePost")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        DataTable dt = (DataTable)ViewState["DataTable"];
        IEnumerable<DataRow> query = from i in dt.AsEnumerable()
                                        where i.Field<int>("CategoryID").Equals(index)
                                        select i;
        DataTable detailTable = query.CopyToDataTable();
        SharePost.DataSource = detailTable;
        SharePost.DataBind();
    }
}
 
protected void OnSubmit(object sender, EventArgs e)
{
    string modelId = (SharePost.Row.FindControl("lblCategoryId") as Label).Text;
    string userName = (SharePost.Row.FindControl("lblCategoryName") as Label).Text;
    string description = (SharePost.Row.FindControl("lblDescription") as Label).Text;
    string contentPost = (SharePost.Row.FindControl("txtComment") as TextBox).Text;
 string img = (SharePost.Row.FindControl("Img") as System.Web.UI.WebControls.Image).ImageUrl;
    DateTime sendDate = DateTime.Now;
    InsertDatabase(modelId, userName, description, contentPost, sendDate);
    sb.Append(@"<script type='text/javascript'>");
    sb.Append("$(function () {");
    sb.Append(" $('#myModal').modal('hide');});");
    sb.Append("</script>");
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ModelScript", sb.ToString(), false);
}
 
private void InsertDatabase(string modelId, string userName, string description, string contentPost, DateTime sendDate)
{
    using (SqlConnection con = new SqlConnection(constring))
    {
        using (SqlCommand cmd = new SqlCommand("INSERT INTO SharePost VALUES(@ModelId,@userName,@contentPost,@description,@sendDate)", con))
        {
            cmd.Parameters.AddWithValue("@ModelId", modelId);
            cmd.Parameters.AddWithValue("@userName", userName);
            cmd.Parameters.AddWithValue("@contentPost", contentPost);
            cmd.Parameters.AddWithValue("@description", description);
            cmd.Parameters.AddWithValue("@sendDate", sendDate);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

 

 error

 

 

 

Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 101:        string contentPost = (SharePost.Row.FindControl("lblpost") as Label).Text;
Line 102:        string comment = (SharePost.Row.FindControl("txtcommentshare") as TextBox).Text;
Line 103:        string img = (SharePost.Row.FindControl("Img") as System.Web.UI.WebControls.Image).ImageUrl;
Line 104:
Line 105:

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html