Вопрос

I am getting a URL from PIC 32 controller using it's post function.

which is like, www.example.com/Default.aspx?x=12&y=23

What I want to do is that when I get the URL, I want to store the values of x and y into SQL Server.

I have launched a .aspx using the IIS server on my system. The coding of the aspx page is..

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
    <title>Sample Configuration Page</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="style1">
            <tr>
                <td>IP Address:</td>
                <td>
                    <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>MAC Address:</td>
                <td>
                    <asp:TextBox ID="TxtUserName" runat="server"></asp:TextBox>
                </td>
            </tr>

        </table>
    </div>
    <asp:Button ID="Button1" runat="server" Text="Save" 
                             onclick="Button1_Click" />
    </form>
</body>
</html>

Please guide me with this.

The data will be stored in the database with following C# code,

     SqlConnection conn = new SqlConnection(GetConnectionString());
            string sql = "INSERT INTO tblRegistration1 (IP, MAC) VALUES "
                        + " (@IP_Address,@MAC_Address)";

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter[] param = new SqlParameter[6];
                //param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
                param[0] = new SqlParameter("@IP_Address", SqlDbType.VarChar, 50);
                param[1] = new SqlParameter("@MAC_Address", SqlDbType.VarChar, 50);

                param[0].Value = IP_Address;
                param[1].Value = MAC_Address;

                for (int i = 0; i < param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }

                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Insert Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                conn.Close();
            }
 public string GetConnectionString()
    {
        //sets the connection string from your web config file "ConnString" is the name of your Connection String
        return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
    }

Everything is fine while using it with the computer browser. But the post coming from the PIC32 is not executing. I dont know how do i proceed with the URL coming back to my IIS server which contains IP and MAC details. In my application I m having these data from PIC32 plat from as well computers.

I hope I have made my self clear.

Это было полезно?

Решение

To get a variable form URL use

Request.QueryString["variable"];

So you can use SqlDataSource to establish SQL Connection or SqlConnection

SqlDataSource

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:Conn %>'InsertCommand="INSERT INTO [TableName] ([x], [y]) VALUES (@x, @y)" >
            <InsertParameters>
                <asp:QueryStringParameter Name="x" Type="Int16"></asp:Parameter>
                <asp:QueryStringParameter Name="y" Type="Int16"></asp:Parameter>
            </InsertParameters>
</asp:SqlDataSource>

SqlConnection

public void insertData()
        {
            using (SqlConnection con = new SqlConnection(conString))
            {
                con.Open();
                try
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO TableName(x, y) VALUES(@x, @y)", con))
                    {
                        cmd.Parameters.Add(new SqlParameter("x", Request.QueryString["x"]));
                        cmd.Parameters.Add(new SqlParameter("y", Request.QueryString["y"]));
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception Ex)
                {
                    Console.WriteLine("Unable To Save Data. Error - " + Ex.Message);
                }
            }
        }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top