I have a login form using ASP .Net and i want put another variable data into DB but source code has compiled to dll files so i write a small script
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Login.ascx.cs" Inherits="Login.Web" %>
<%@ import Namespace="System.Data.Common"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<script runat="server">
protected void btnLoginButton_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Server=(local);Database=Test;Integrated Security=True;");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Information(Info1,Info2) values(@Info1,@Info2)",con);
cmd.Parameters.AddWithValue("@Info1", Info1.Text);
cmd.Parameters.AddWithValue("@Info2", Info2.Text);
int i = cmd.ExecuteNonQuery();
}
</script>
into the Login.aspx file. When I run, “Info1” and “Info2” sent success to DB but Login Event didn’t work
What did i wrong.
Note: Login.ascx.cs has compiled to Login.dll and i can’t modify it.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I think the problem is btnLoginButton_Click is defined in two places, the first one is inside “Login.Web” and the second one in your aspx.
So when you click btnLoginButton it just calls method which is in the aspx,. So it never reaches the event which is inside compiled assembly
So the solution would be change click event to call different click event where you can call base click event
<asp:Button ID="btnLoginButton " runat="server" Text="Button" OnClick="btnLoginButton2_Click" />
<script runat="server">
protected void btnLoginButton2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Server=(local);Database=Test;Integrated Security=True;");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Information(Info1,Info2) values(@Info1,@Info2)",con);
cmd.Parameters.Add("@Info1", SqlDbType.VarChar).Value = Info1.Text;
cmd.Parameters.Add("@Info1", SqlDbType.VarChar).Value = Info2.Text;
int i = cmd.ExecuteNonQuery();
btnLoginButton_Click(sender, e);
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0