|
|
|
อยากทราบวิธีนำข้อมูลใน textbox ไปใส่ในฐานข้อมูลครับ เกี่ยวกับออฟเจค |
|
|
|
|
|
|
|
Code (C#)
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="C#" Debug="true" EnableEventValidation="true" %>
<script runat="server">
SqlConnection objConn;
SqlCommand objCmd;
String strSQL;
public void Page_Load(object sender, EventArgs e)
{
String strConnString;
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=cartoon;Max Pool Size=400;Connect Timeout=6000;";
objConn = new SqlConnection(strConnString);
objConn.Open();
if (!Page.IsPostBack)
{
BindData();
}
}
void BindData()
{
String strSQL;
strSQL = "select top (1)CusID from Customer order by CusID desc";
SqlDataReader dtReader;
objCmd = new SqlCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
dtReader.Close();
dtReader = null;
}
protected void Button2_Click(object sender, EventArgs e)
{
//*** Pub ***//
TextBox txtTextBox1 = this.TextBox1.Text;
//*** SetProductID ***//
TextBox txtTextBox2 = this.TextBox2.Text;
//*** SetProductName ***//
TextBox txtTextBox3 = this.TextBox3.Text;
//*** Detail ***//
TextBox txtTextBox4 = this.TextBox4.Text;
//*** Author ***//
TextBox txtTextBox5 = this.TextBox5.Text;
strSQL = "update Customer set Name='" + txtTextBox1.Text + "',SurName='" + txtTextBox2.Text + "',Address ='" + txtTextBox3.Text + "',Email='" + txtTextBox4.Text + "',Phone='" + txtTextBox5.Text + "' where CusID = (select top(1)CusID from Customer order by CusID desc)";
objCmd = new SqlCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
//Response.Write("á¡éä¢" + txtSetProductID.Text + " àÃÕºÃéÍÂáÅéÇ");
}
</script>
<html>
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>Cartoon2ndHand</title>
<style type="text/css">
body {
background-image: url(images/bg.gif);
background-repeat: repeat;
}
a {
font-size: 10px;
}
.style9
{
height: 31px;
}
.style10
{
height: 26px;
}
-->
</style></head>
<body><form id="form1" runat="server">
<table width="961" height="1281" border="0" align="center">
<tr>
<td width="1024" height="1277" valign="top"><table width="955" height="1275" border="0">
<tr>
<td width="949" height="132"><div align="center"><img src="images/header edit.jpg" width="934" height="126" /></div></td>
</tr>
<tr>
<td class="style10"><table height="35" border="0" align="center"
style="width: 949px">
<tr align="center">
<td width="173" class="style9"><a href="default.aspx"><img src="images/Tab home.jpg" width="150" height="30" border="0" /></a></td>
<td width="150" class="style9"><a href="page1.aspx"><img src="images/Tab product light.jpg" width="150" height="30" border="0"></a></td>
<td width="150" class="style9"><a href="page2.aspx"><img src="images/Tab Buy the book..jpg" width="150" height="30" border="0"></a></td>
<td width="150" class="style9"><a href="page3.aspx"><img src="images/Tab How to order.jpg" width="150" height="30" border="0"></a></td>
<td width="150" class="style9"><a href="page4.aspx"><img src="images/Tab Contact us.jpg" width="150" height="30" border="0"></a></td>
<td width="222" class="style9"><asp:HyperLink id="hplLogout" NavigateURL="Logout.aspx" runat="server"><img src="images/logout.jpg" width="150" height="30"></asp:HyperLink></td>
</tr>
</table></td>
</tr>
<tr>
<td height="1097" valign="top"><table width="949" height="1095" border="0">
<tr>
<td width="180" height="1091" valign="top"><table width="180" height="500" border="1" align="left" cellpadding="0" cellspacing="0">
<tr>
<td width="204">
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
</table></td>
<td width="759" valign="top"><p><img src="images/managepurchase.jpg" width="700" height="35"><br />
<br />
<br />ª×èÍ
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text=" "></asp:Label>
<br />
<br />
¹ÒÁÊ¡ØÅ <asp:TextBox ID=" " runat="server" Width="192px"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<br />
·ÕèÍÂÙè <asp:TextBox ID=" " runat="server" Height="58px" Width="319px"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<br />
<br />
E-Mail <asp:TextBox ID=" " runat="server"></asp:TextBox>
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
<br />
<br />
àºÍÃìâ·ÃÈѾ·ì <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:Label ID="Label5" runat="server" Text=" "></asp:Label>
</p>
<p> </p>
<p> <br />
<br />
<asp:Button ID="Button2" runat="server" Text="Â×¹Âѹ¡ÒÃÊÑ觫×éÍ"
PostBackUrl="~/page5-2-3.aspx" onclick="Button2_Click" />
</p>
</td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>
คิดว่าเป็นแถวๆบันทัดที่ 40 น่ะครับ เกี่ยวกับ object ของ textbox
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), C#
|
ประวัติการแก้ไข 2011-05-11 13:21:23
|
|
|
|
|
Date :
2011-05-11 13:15:09 |
By :
ekarit |
View :
1019 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อันที่จริงคุณก็เรียก this.TextBox1.Text ได้เลยน่ะครับ ไม่ต้องประกาศ new ขึ้นมาอีกครับ
|
|
|
|
|
Date :
2011-05-11 14:08:07 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แก้เป็น
strSQL = "update Customer set Name='" +This.TextBox1.Text + "',SurName='" + This.TextBox2.Text + "',Address ='" + This.TextBox3.Text + "',Email='" + This.TextBox4.Text + "',Phone='" + This.TextBox5.Text + "' where CusID = (select top(1)CusID from Customer order by CusID desc)";
ก็ยังใช้ไม่ได้ครับ Error Objeck referrence not set to an instance of object
|
|
|
|
|
Date :
2011-05-11 14:24:39 |
By :
ekarit |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this ตัวพิมพ์เล็กครับ
|
|
|
|
|
Date :
2011-05-11 14:46:30 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้แล้วครับ ขอบคุณครับ
|
|
|
|
|
Date :
2011-05-11 16:17:36 |
By :
aekarits |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|