Many times, we are used to leaving the initialization process of the database connection to Page_Load. In fact, this has advantages and disadvantages. The advantage is that this method is very practical when encountering unilateral problems. The disadvantage is that when encountering multilateral problems, This is not very useful in this situation! For 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,@state,@country)";
SqlCommand mySqlCom = new SqlCommand(myinsertCmd, mySqlCon); //Initialization command call
//Achieve 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>Record added</b><br/>";
mySqlCom.Connection.Close();
Pub_Id.Text = "";
Pub_Name.Text = "";
City.Text= "";
State.Text = "";
Country.Text = "";
BindGrid();
}
//Sub 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>
<h2>Add a new publisher:</h2>
<br/>
Issuer ID should start with 99 and contain 4 digits<br/>
Issuer 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" />
Country:
<asp:textbox id="Country" runat="server" />
<br/>
<br/>
<asp:button Text="Submit" OnClick="AddPublisher" runat="server" ID="Button1" /><br/>
<span id="Message" runat="server" />
<br/>
<asp:DataGrid id="dgMyGrid" runat="server" />
This example seems to be fine at first glance, and no error is reported during debugging. However, after adding data to the generated page, an error will be reported when submitting, saying that the attributes do not match. Something like that. What is the cause? In fact, this is caused by the initialization process when the page is loaded, but there is a problem here that I have never been able to figure out, that is, since the DB instance has been initialized during the page initialization process, it should be possible to directly generate the application. , but it seems not! It is still necessary to put the initialization process into a specific function to achieve it! See below:
<<script language="C#" runat="server">
protected void Page_Load(Object Src,EventArgs E)
{
//The IF statement is used directly during page loading. In fact, nothing is added!
if (!IsPostBack)
BindGrid();
}
public void AddPublisher(Object sender, EventArgs E)
{
string strprovider="server=localhost;uid=sa;pwd=sa;database=pubs"; //Construct the initialization process
SqlConnection mySqlCon=new SqlConnection(strprovider);
string myinsertCmd ="insert into publishers ( pub_id, pub_name, city, state, country ) values (@pubid,@pubname,@city,@state,@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>Record added</b><br>";
mySqlCom.Connection.Close();
Pub_Id.Text = "";
Pub_Name.Text = "";
City.Text= "";
State.Text = "";
Country.Text = "";
BindGrid();
}
When calling the public void BindGrid() sub-function, the DB connection must also be initialized.
{
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>
<h2>Add a new publisher:</h2>
<br>
Issuer ID should start with 99 and contain 4 digits<br>
Issuer 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" />
Country:
<asp:textbox id="Country" runat="server" />
<br>
<br>
<asp:button Text="Submit" OnClick="AddPublisher" runat="server" ID="Button1" /><br>
<span id="Message" runat="server" />
<br>
<asp:DataGrid id="dgMyGrid" runat="server" />
</form>>
After such modification, we can achieve the increase of data in a real sense! But I also tried deleting and updating the data. When the page was loaded, it was directly assigned the initialization DB attribute, but there was no problem. I don’t know the reason. If anyone knows, please give me a heads up! Thanks:)