|
|
|
(Web app)ถ้าจะเช็คว่าเลข id ที่จะนำเข้า database ไม่ตรงกับ id ที่มีอยู่ใน database นี่เช็คยังไงหรอครับ |
|
|
|
|
|
|
|
Code (C#)
void btnSave_Click(Object sender , EventArgs e)
{
int intNumRows;
strSQL = "SELECT COUNT(*) FROM customer WHERE CustomerID = '"+ this.txtCustomerID.Text +"' ";
objCmd = new SqlCommand(strSQL, objConn);
intNumRows = Convert.ToInt32(objCmd.ExecuteScalar());
if(intNumRows > 0)
{
this.pnlAdd.Visible = false;
this.lblStatus.Visible = true;
this.lblStatus.Text = "CustomerID already exists.";
}
else
{
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
" VALUES " +
" ('" + this.txtCustomerID.Text + "','" + this.txtName.Text + "','" + this.txtEmail.Text + "', " +
" '" + this.txtCountryCode.Text + "','" + this.txtBudget.Text + "','" + this.txtUsed.Text + "')";
objCmd = new SqlCommand();
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
this.pnlAdd.Visible = false;
try
{
objCmd.ExecuteNonQuery();
this.lblStatus.Text = "Record Inserted";
this.lblStatus.Visible = true;
}
catch (Exception ex)
{
this.lblStatus.Visible = true;
this.lblStatus.Text = "Record can not insert Error ("+ ex.Message +")";
}
}
}
|
|
|
|
|
Date :
2015-04-23 17:00:14 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อ้าว VB.Net
Code (VB.NET)
Sub btnSave_Click(sender As Object, e As EventArgs)
Dim intNumRows As Integer
strSQL = "SELECT COUNT(*) FROM customer WHERE CustomerID = '"& Me.txtCustomerID.Text &"' "
objCmd = New SqlCommand(strSQL, objConn)
intNumRows = objCmd.ExecuteScalar()
IF intNumRows > 0 Then
Me.pnlAdd.Visible = False
Me.lblStatus.Visible = True
Me.lblStatus.Text = "CustomerID already exist."
Else
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
" VALUES " & _
" ('" & Me.txtCustomerID.Text & "','" & Me.txtName.Text & "','" & Me.txtEmail.Text & "', " & _
" '" & Me.txtCountryCode.Text & "','" & Me.txtBudget.Text & "','" & Me.txtUsed.Text & "')"
objCmd = New SqlCommand
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
Me.pnlAdd.Visible = False
Try
objCmd.ExecuteNonQuery()
Me.lblStatus.Text = "Record Inserted"
Me.lblStatus.Visible = True
Catch ex As Exception
Me.lblStatus.Visible = True
Me.lblStatus.Text = "Record can not insert Error ("& ex.Message &")"
End Try
End IF
|
|
|
|
|
Date :
2015-04-23 17:00:39 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|