Hi nauna,
Check this example. Now please take its reference and correct your code.
HTML
<table>
<tr>
<td>
Name
</td>
<td>
<asp:TextBox runat="server" ID="txtName" />
</td>
</tr>
<tr>
<td>
Country
</td>
<td>
<asp:TextBox runat="server" ID="txtCountry" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button Text="Save" runat="server" OnClick="Save" />
</td>
</tr>
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<style type="text/css">
.message
{
width: 100%;
position: absolute;
top: 10px;
left: 50%;
z-index: 100000;
padding: 0;
font-size: 15px;
margin: 0 auto;
}
</style>
<script type="text/javascript">
function ShowMessage(message, messagetype) {
var cssclass;
switch (messagetype) {
case 'Success':
cssclass = 'alert-success'
break;
case 'Error':
cssclass = 'alert-danger'
break;
case 'Warning':
cssclass = 'alert-warning'
break;
default:
cssclass = 'alert-info'
}
$('body').append('<div id="alert_div" style="margin: 0 10%; -webkit-box-shadow: 3px 4px 6px #999;width:50%" class="alert fade in '
+ cssclass + '"><a href="#" class="close" data-dismiss="alert" aria-label="close">×</a><strong>'
+ '<span>' + message + '</span></div>');
}
</script>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Save(object sender, EventArgs e)
{
int i = 0;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "INSERT INTO Customers VALUES(@Name,@Country)";
SqlCommand cmd = new SqlCommand(query);
SqlConnection con = new SqlConnection(conString);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim());
try
{
con.Open();
i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
ShowMessage("Record submitted successfully", MessageType.Success);
}
}
catch (Exception ex)
{
}
}
public enum MessageType { Success, Error, Info, Warning };
protected void ShowMessage(string Message, MessageType type)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), "ShowMessage('" + Message + "','" + type + "');", true);
}
VB.Net
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer = 0
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "INSERT INTO Customers VALUES(@Name,@Country)"
Dim cmd As SqlCommand = New SqlCommand(query)
Dim con As SqlConnection = New SqlConnection(conString)
cmd.Connection = con
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim())
cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim())
Try
con.Open()
i = cmd.ExecuteNonQuery()
con.Close()
If i > 0 Then
ShowMessage("Record submitted successfully", MessageType.Success)
End If
Catch ex As Exception
End Try
End Sub
Public Enum MessageType
Success = 0
[Error] = 1
Info = 2
Warning = 3
End Enum
Protected Sub ShowMessage(ByVal Message As String, ByVal type As MessageType)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), System.Guid.NewGuid().ToString(), "ShowMessage('" & Message & "','" & type & "');", True)
End Sub
Screenshot