Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my aspx
ASP.NET
<tr>
            <td>input</td>
            <td>
                <input type="text" runat="server" id="input" name="input" value="" style="width: 250px" />
            </td>
            <td>
                <asp:Button ID="Button1"  style="text-align: right;" align="right"  runat="server" Text="Submit" BorderStyle="Solid" OnClick="btnSubmit_Click" />
           </td>
</tr>


my c#
C#
protected string r_input = string.Empty;

my page load
C#
protected void Page_Load(object sender, EventArgs e)
  {
     if (Request.Params["input"] != null) r_input = Request.Params["input"];
  }

my submit button
C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
    string connectionStringDB = "server=PC00093-LimKY-;database=master;uid=sa;password=abc135;";
    using (SqlConnection connection = new SqlConnection(connectionStringDB))
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO countrydetail (ID,Name) VALUES (@id,@Name)");
        cmd.CommandType = CommandType.Text;
        cmd.Connection = connection;
        cmd.Parameters.AddWithValue("@ID", 1);
        cmd.Parameters.AddWithValue("@Name", r_input);
        connection.Open();
        cmd.ExecuteNonQuery();
    }


After i input something into the textbox i click the submit button.I check my db,only ID successfully insert into database,Name Column is empty

What I have tried:

i tried many times,still cannot work....need help.
Posted
Updated 19-Jun-16 16:19pm

1 solution

That would be because your protected string r_input is not set.
First thing you need to understand is the Page Lifecycle - refer: MSDN - ASP.Net Page Life Cycle Overview[^]

Use your debugger & put a break point in btnSubmit_Click event.
Check what the value of r_input is (an empty string) & then look at the value of your input, somewhat confusingly named "input" - it will have whatever value the user has entered.

Kind Regards
 
Share this answer
 
Comments
KyLim0211 19-Jun-16 22:23pm    
yes i debugged it,value is empty,why?

whats wrong?
if (Request.Params["input"] != null) r_input = Request.Params["input"];
an0ther1 20-Jun-16 0:04am    
Because Request.Params is not intended to get the value of the input field.
Firstly, change the name (the id) to something that makes sense eg: txtUserName instead of "input"
Then fire up the page & put your breakpoint in the btnSubmitClick Event Handler.
Once the breakpoint is hit, go to the Immediate window in your debugger & type ? txtUserName.Text
This is how you should be getting the value of your Text field not through the Request.Params.
You would actually find that Request.Params("input") is null as the actual key name is determined based on the client id, not the server id. You can find the Client side ID but loading the page to a browser & selecting View Source.
This can be seen by doing the following;
In your Page_Load enter the following;
System.Collections.Specialized.NameValueCollection col = Request.Params;
Create a break-point on this line.
When the line is hit, press the F11 key to step to the next line of code
If you use the Locals debugger window, you will see the client side id of the text field, not the server side id.
If you were to enter the client side name you would get the text field value but you do not need to, you have created the input & can therefore access it in your code behind by simply using txtUserName.Text

Kind Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900