Asp. The problem of using data processing to insert datum in net
Source: Internet
Author: User
asp.net| Insert | data | Problem many times, we will be accustomed to the database connection initialization process to Page_Load to do, in fact, there are advantages and disadvantages, the advantage is a unilateral issue, this method is very practical, the disadvantage is to encounter a multilateral problem, on the kind of situation this is not very useful! For example, the following example:
For Example:
<script language= "C #" runat= "Server" >
SqlConnection Mysqlcon;
protected void Page_Load (Object Src,eventargs E)
{
Mysqlcon=new SqlConnection ("Server=localhost;uid=sa;pwd=sa;database=pubs"); Initialization process
if (! IsPostBack)
Bindgrid ();
}
public void Addpublisher (Object sender, EventArgs E)
{
String myinsertcmd = "INSERT into publishers (pub_id, pub_name, city, State, country) VALUES (@pubid, @pubname, @city, @sta TE, @country) ";
SqlCommand mysqlcom = new SqlCommand (Myinsertcmd, Mysqlcon); Initialization command Call
Implement matching
MYSQLCOM.PARAMETERS.ADD (New SqlParameter ("@pubid", SqlDbType.Char, 4));
mysqlcom.parameters["@pubid"]. Value = Pub_id.text;
MYSQLCOM.PARAMETERS.ADD (New SqlParameter ("@pubname", SqlDbType.VarChar, 40));
mysqlcom.parameters["@pubname"]. Value = Pub_name.text;
MYSQLCOM.PARAMETERS.ADD (New SqlParameter ("@city", SqlDbType.VarChar, 20));
mysqlcom.parameters["@city"]. Value = City.text;
MYSQLCOM.PARAMETERS.ADD (New SqlParameter ("@state", SqlDbType.Char, 2));
mysqlcom.parameters["@state"]. Value = State.text;
MYSQLCOM.PARAMETERS.ADD (New SqlParameter ("@country", SqlDbType.VarChar, 30));
mysqlcom.parameters["@country"]. Value = Country.text;
Open db
MySqlCom.Connection.Open ();
Mysqlcom.executenonquery ();
message.innerhtml = "<b> added record </b><br/>";
MySqlCom.Connection.Close ();
Pub_id.text = "";
Pub_name.text = "";
City.text= "";
State.text = "";
Country.text = "";
Bindgrid ();
}
Child function call
public void Bindgrid ()
{
SqlDataAdapter mysqlcom = new SqlDataAdapter ("select * from publishers where pub_id like ' 99% '", Mysqlcon);
DataSet myds = new DataSet ();
Mysqlcom.fill (myDS, "publishers");
Dgmygrid.datasource = myDS. tables["Publishers"]. DefaultView; Dgmygrid.databind ();
}
</script>
<br/>
The publisher ID should begin with 99 and contain 4 digits <br/>
Publisher ID:
<asp:textbox id= "pub_id" runat= "Server"/> Name:
<asp:textbox id= "pub_name" runat= "Server"/>
City:
<asp:textbox id= "City" runat= "Server"/>
<br/>
Province:
<asp:textbox id= "state" runat= "Server"/>
Countries:
<asp:textbox id= "Country" runat= "Server"/>
<br/>
<br/>
Such an example at the beginning appeared to be no problem, debugging also did not complain, but the generated page after the submission of data will be submitted to the error, say what attributes do not support such words. What is the cause of it! In fact, this is the initialization process in the page load, but there is a problem I have not been able to figure out that since the page initialization process has been initialized to the DB instance, according to the truth should be able to directly generate the application Ah, but it seems not! Or to the initialization of the process into a specific function to achieve! Look below:
<<script language= "C #" runat= "Server" >
protected void Page_Load (Object Src,eventargs E)
{
The page is loaded directly using the IF statement, in fact, do not add!
if (! IsPostBack)
Bindgrid ();
}
public void Addpublisher (Object sender, EventArgs E)
{
String strprovider= "Server=localhost;uid=sa;pwd=sa;database=pubs"; Constructing the initialization process
SqlConnection mysqlcon=new SqlConnection (Strprovider);
String myinsertcmd = "INSERT into publishers (pub_id, pub_name, city, State, country) VALUES (@pubid, @pubname, @city, @stat E, @country) ";
SqlCommand mysqlcom = new SqlCommand (Myinsertcmd,mysqlcon); Initialization process Implementation
MYSQLCOM.PARAMETERS.ADD (New SqlParameter ("@pubid", SqlDbType.Char, 4));
mysqlcom.parameters["@pubid"]. Value = Pub_id.text;
MYSQLCOM.PARAMETERS.ADD (New SqlParameter ("@pubname", SqlDbType.VarChar, 40));
mysqlcom.parameters["@pubname"]. Value = Pub_name.text;
MYSQLCOM.PARAMETERS.ADD (New SqlParameter ("@city", SqlDbType.VarChar, 20));
mysqlcom.parameters["@city"]. Value = City.text;
MYSQLCOM.PARAMETERS.ADD (New SqlParameter ("@state", SqlDbType.Char, 2));
mysqlcom.parameters["@state"]. Value = State.text;
MYSQLCOM.PARAMETERS.ADD (New SqlParameter ("@country", SqlDbType.VarChar, 30));
mysqlcom.parameters["@country"]. Value = Country.text;
MySqlCom.Connection.Open ();
Mysqlcom.executenonquery ();
message.innerhtml = "<b> added record </b><br>";
MySqlCom.Connection.Close ();
Pub_id.text = "";
Pub_name.text = "";
City.text= "";
State.text = "";
Country.text = "";
Bindgrid ();
}
The DB connection is also initialized when the public void Bindgrid () child function is called
{
String strprovider= "Server=dev;uid=sa;pwd=pauperboyboy;database=pubs";
SqlConnection mysqlcon=new SqlConnection (Strprovider);
SqlDataAdapter mysqlcom = new SqlDataAdapter ("select * from publishers where pub_id like ' 99% '", Mysqlcon);
DataSet myds = new DataSet ();
Mysqlcom.fill (myDS, "publishers");
Dgmygrid.datasource = myDS. tables["Publishers"]. DefaultView;
Dgmygrid.databind ();
}
</script>
<br>
The publisher ID should begin with 99 and contain 4 digits <br>
Publisher ID:
<asp:textbox id= "pub_id" runat= "Server"/> Name:
<asp:textbox id= "pub_name" runat= "Server"/>
City:
<asp:textbox id= "City" runat= "Server"/>
<br>
Province:
<asp:textbox id= "state" runat= "Server"/>
Countries:
<asp:textbox id= "Country" runat= "Server"/>
<br>
<br>
After this modification, we can achieve in the real sense of the increase in data! But I also tried to delete and update the data, in the page load directly assigned to its initialization DB properties but there is no problem, do not know what the reason, know the person to mention a wake up Oh! Thanks:)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.