Data not inserting into database RRS feed

  • Question

  • User481677910 posted

    Data is not inserting into database for some reason please help below is the code

    Guid newGUID = Guid.NewGuid();
    String strConnString = System.Configuration.ConfigurationManager .ConnectionStrings["userConnectionString"].ConnectionString; 
    Session["UserName"] = HttpContext.Current.User.Identity.Name;
    SqlConnection con = new SqlConnection(strConnString);
    string strQuery = "insert into DB(Guid,Id, IP, UserName, Quote_Name, PO, ) values ( @Guid, @Id,@IP, @UserName, @Quote_Name, @PO)"; 
    SqlCommand cmd = new SqlCommand(strQuery);
    cmd.Parameters.AddWithValue("@Guid", newGUID.ToString());
    cmd.Parameters.AddWithValue("@IP", Request.UserHostAddress);
    cmd.Parameters.AddWithValue("@Id", Membership.GetUser().ProviderUserKey);
    cmd.Parameters.AddWithValue("@Username", Session["UserName"]);
    cmd.Parameters.AddWithValue("@Quote_Name", QuoteName.Text);
    cmd.Parameters.AddWithValue("@PO", PO.Text);
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con; try { con.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { Response.Write(ex.Message); } finally { con.Close(); con.Dispose(); Response.Redirect ("anotherpage.aspx"); } }

    Friday, August 24, 2018 9:29 PM

All replies

  • User465171450 posted

    First, get rid of the Response.Redirect in your finally. This will at least let your Response.Write show an error, though Response.Write is really not a good way to do this at all. Try adding something in the .aspx markup, like an <asp:Literal> control so you can set it's Text property to be the exception. Without that exception it's going to be very hard to determine what is happening.

    Saturday, August 25, 2018 12:59 AM
  • User481677910 posted

    I tried but still getting error message and what I have found if textbox has more than 30 character it does not insert in the database I don't know why even I have select varchar(Max) in sql database.

    Monday, September 10, 2018 11:26 PM
  • User632428103 posted

    Hello all,

    @new2world2015 => there is something strange into your strQuery !

    looks at it : 

    insert into DB(Guid,Id, IP, UserName, Quote_Name, PO, )

    After PO there is a COMA ..Please be sure to test your T SQL inside SSMO and after trying in a web page ...

    Also, i've seen you have a guid but in table you transform the guid into a string ? why ?

    Here is it a little sample about how to create a table with a guid and how to insert it:

    id uniqueidentifier

    Tuesday, September 11, 2018 7:28 AM