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

Last Reply 28 days ago By micah

Posted one month 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();
            }
        }

 

Posted 28 days ago
micah says:
http://localhost:4463/UserImage/

This is not the valid image path. So first pass the proper image path and replace the below code

string img = (SharePost.Row.FindControl("Image2") as System.Web.UI.WebControls.Image).ImageUrl.Split('/')[2];
string img2 = (SharePost.Row.FindControl("Image1") as System.Web.UI.WebControls.Image).ImageUrl.Split('/')[2];

 With

string img = (SharePost.Row.FindControl("Image2") as System.Web.UI.WebControls.Image).ImageUrl.Split('/')[(SharePost.Row.FindControl("Image2") as System.Web.UI.WebControls.Image).ImageUrl.Split('/').Length - 1];
string img2 = (SharePost.Row.FindControl("Image1") as System.Web.UI.WebControls.Image).ImageUrl.Split('/')[(SharePost.Row.FindControl("Image1") as System.Web.UI.WebControls.Image).ImageUrl.Split('/').Length - 1];

 

I agree, here is the link: http://e-iceblue.com/free-api