Reputation: 878
I have this ASPX code:
<form name="AddArticle" action="Register.aspx" method="post">
<b>
title :<br /></b><input id="Text1" type="text" name="ArticleTitle"/><p></p>
<b>
date: <br /></b><input id="Text2" type="text" name="ArticleDate"/><p></p>
<b>
author : <br /></b><input id="Text3" type="text" name="ArticleAuthor"/><p></p>
<b>
text: <br /></b> <textarea rows="10" cols="60" name="ArticleBody"></textarea>
<br />
<input id="Reset1" type="reset" value="clean" />
<input id="Submit1" type="submit" value="send" /></form>
And I have this ASPX.C# code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ArticleTitle = "", ArticleBody = "", ArticleAuthor = "", ArticleDate = "";
if (IsPostBack)
{
try
{
ArticleTitle = Request.Form["ArticleTitle"].ToString();
ArticleDate = Request.Form["ArticleDate"].ToString();
ArticleAuthor = Request.Form["ArticleAuthor"].ToString();
ArticleBody = Request.Form["ArticleBody"].ToString();
string dpath = Server.MapPath(@"App_Data") + "/MySite.mdb";
string connectionstring = @"Data source='" + dpath + "';Provider='Microsoft.Jet.OLEDB.4.0';";
OleDbConnection con = new OleDbConnection(connectionstring);
string QuaryString = string.Format("insert into tblArticles(ArticleTitle,ArticleBody, ArticleAuthor, PostDate) values ('{0}','{1}','{2}','{3}')", ArticleTitle, ArticleBody, ArticleAuthor, ArticleDate);
OleDbCommand cmd = new OleDbCommand(QuaryString, con);
con.Open();
cmd.ExecuteNonQuery();
}
catch
{
Response.Redirect("Default.aspx");
}
}
}
}
My quastion is why when I click on the "send" button, the informatim from the form (title, author...) is not inserted to the DB? How can I fix it? Wish for help, thanks.
Upvotes: 0
Views: 403
Reputation: 304
i think you should write a button click event to invoke the function:
.cs
protected void Button1_Click(object sender, EventArgs e)
{
ArticleTitle = Request.Form["ArticleTitle"].ToString();
ArticleDate = Request.Form["ArticleDate"].ToString();
ArticleAuthor = Request.Form["ArticleAuthor"].ToString();
ArticleBody = Request.Form["ArticleBody"].ToString();
string dpath = Server.MapPath(@"App_Data") + "/MySite.mdb";
string connectionstring = @"Data source='" + dpath + "';Provider='Microsoft.Jet.OLEDB.4.0';";
OleDbConnection con = new OleDbConnection(connectionstring);
string QuaryString = string.Format("insert into tblArticles(ArticleTitle, PostDate) values ('{0}','{1}','{2}','{3}')", ArticleTitle, ArticleBody, ArticleAuthor, ArticleDate);
OleDbCommand cmd = new OleDbCommand(QuaryString, con);
con.Open();
cmd.ExecuteNonQuery();
}
.aspx:
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
use StoredProcedure: connectString at web.config:
<add name="CGB_GlobalInfoConnectionString" connectionString="Data Source=127.0.0.1;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa;Password=11111" providerName="System.Data.SqlClient" />
.cs:
SqlConnection connection = new SqlConnection(DataManager.DBConnectString);
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = connection;
cmd.CommandText = "Insert_AddSomething";
cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = userid;
connection.Open();
cmd.ExecuteNonQuery();
}
Upvotes: 1
Reputation: 885
The best way for this using a button click event but in your case. You can use AutoPostBack attribute for each element:
<asp:TextBox ID="ArticalTitle" runat="server" Text="Button" AutoPostBack="true"/>
When user change the focus on this element(press TAB etc.) The code will postback the value in the textbox. But in this case, your values pass one by one so you need to store those values before trying the write them to your DB or you can use a panel control and place a dummy button outside of the panel control for change'in focus:
<asp:Panel ID="Panel1" runat="server" AutoPostBack="true">
<asp:TextBox ID="TextBox1" Text="asd" runat="server" />
<asp:TextBox ID="TextBox2" Text="asd" runat="server" />
</asp:Panel>
<asp:Button ID="Button1" runat="server" Text="Button" />
When user click the Button panel control autopostback and you can capture the values in textboxes.
Hope this helps.
Upvotes: 1