locked
Im trying to insert a row into my database using javascript c# asp.net but its not working, pls help RRS feed

  • Question

  • User980339261 posted

    my chat.aspx page

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Chat.aspx.cs" Inherits="Chat" %>

    <html>
    <head runat="server">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

    <script src="../Scripts/jquery-1.5.2.js" type="text/javascript"></script>

    </head>
    <body>
    <form id="form1" runat="server">
    <div class="container-fluid">
    <div class="col-lg-4 col-md-6 col-sm-12">
    <div id="chatpage">
    <div class="cht1">
    <div class="chhdr">
    <asp:ImageButton ID="imghdr" runat="server" CssClass="chtbimg" />
    <h5><asp:Label ID="chtnm" runat="server" Text="" CssClass="chtblbl" ></asp:Label></h5>

    </div>
    <div class="chpr">

    </div>
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="30000">
    </asp:Timer>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:Panel ID="Panel1" CssClass="chtpnl" runat="server"></asp:Panel>
    </ContentTemplate>
    </asp:UpdatePanel>
    </div>
    <div class="cht2">
    <asp:Label ID="lblOutput" runat="server" Text="Label"></asp:Label>
    <asp:TextBox runat="server" id="txtmsg" CssClass="chttxt" placeholder="Enter message here" ></asp:TextBox>
    <asp:TextBox runat="server" id="txtrmid" class="chttxt" Visible="false" TextMode="MultiLine" ></asp:TextBox>
    <asp:TextBox runat="server" id="txtsid" class="chttxt" Visible="false" TextMode="MultiLine" ></asp:TextBox>
    <asp:TextBox runat="server" id="txtdate" class="chttxt" Visible="false" TextMode="MultiLine" ></asp:TextBox>
    <input type="button" id="btnsend" class="chtbtn" value="Send" onclick="insert.aspx();" />


    </div>
    </div>
    </div>
    <div class="col-lg-8 col-md-6 hidden-sm"></div>

    </div>

    </form>
    <script type="text/javascript" >
    function insert() {
    var rmid = document.getElementById( '<%=txtrmid.ClientID%>').value;
    var msg = document.getElementById('<%=txtmsg.ClientID%>').value;
    var sid = document.getElementById('<%=txtsid.ClientID%>').value;
    var date = document.getElementsById('<%=txtdate.ClientID%>').value;

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", "insert.aspx?rmid="+rmid+"&msg="+msg+"&sid="+sid+"&dt="+date, false);
    xmlhttp.send(null);


    }
    </script>

    </body>
    </html>

    my insert.aspx.cs code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.AspNet.Identity;
    using System.IO;
    using System.Data;
    using System.Data.SqlClient;

    public partial class css_insert : System.Web.UI.Page
    {
    SqlConnection con = new SqlConnection(@"MyconnectionString");
    string msg;
    string rmid;
    string sid;
    string dt;
    protected void Page_Load(object sender, EventArgs e)
    {
    rmid = Request.QueryString["rmid"].ToString();
    msg = Request.QueryString["msg"].ToString();
    sid = Request.QueryString["sid"].ToString();
    dt = Request.QueryString["dt"].ToString();
    DateTime dtvalue = Convert.ToDateTime(dt,
    System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);

    SqlCommand cmd = con.CreateCommand() ;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText =("INSERT INTO Message VALUES(@Roomid, @Messages, @Sid, @Timestamp)");
    cmd.Parameters.AddWithValue("@Roomid", rmid.ToString());
    cmd.Parameters.AddWithValue("@Messages", msg.ToString());
    cmd.Parameters.AddWithValue("@Sid", sid.ToString());
    cmd.Parameters.AddWithValue("@Timestamp", dtvalue);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();

    }

    please assist

    Sunday, April 5, 2020 6:58 AM

All replies

  • User288213138 posted

    Hi ebikeye,

    <input type="button" id="btnsend" class="chtbtn" value="Send" onclick="insert.aspx();" />

    var date = document.getElementsById('<%=txtdate.ClientID%>').value;

    I tested your code and found some mistakes.

    First the insert method should be onclick = "insert ()".

    And when you get the date, there is an extra 's' in getElementById.

    Please try to use F12 to view and debug your code. 

    More information about how to use the F12 Developer Tools you can refer to this link:

    https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/hh968260(v%3Dvs.85)

    Best regards,

    Sam

    Monday, April 6, 2020 8:40 AM