Hi muhammad12,
Please refer below Sample.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<table>
<tr>
<td>Name:</td>
<td><asp:TextBox runat="server" ID="txtName"></asp:TextBox></td>
</tr>
<tr>
<td>Country:</td>
<td><asp:TextBox runat="server" ID="txtCountry"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Button Text="Save" ID="btnSave" runat="server" OnClick="Save" /></td>
</tr>
</table>
<hr />
<asp:Panel runat="server" ID="pnlDetails">
<table>
<tr>
<td>Name:</td>
<td><asp:Label runat="server" ID="lblName"></asp:Label></td>
</tr>
<tr>
<td>Country:</td>
<td><asp:Label runat="server" ID="lblCountry"></asp:Label></td>
</tr>
</table>
</asp:Panel>
<hr />
<asp:Button Text="Print" ID="btnPrint" runat="server" OnClientClick="PrintPanel()" />
<script type = "text/javascript">
function PrintPanel() {
var panel = document.getElementById("<%=pnlDetails.ClientID %>");
var printWindow = window.open('', '', 'height=400,width=800');
printWindow.document.write('<html><head><title></title>');
printWindow.document.write('</head><body >');
printWindow.document.write(panel.innerHTML);
printWindow.document.write('</body></html>');
printWindow.document.close();
setTimeout(function () {
printWindow.print(PrintPanel);
}, 500);
return false;
}
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Save(object sender, EventArgs e)
{
string name = txtName.Text;
string country = txtCountry.Text;
this.lblName.Text = name;
this.lblCountry.Text = country;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Customers (Name, Country) VALUES (@Name, @Country)",con))
{
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim name As String = txtName.Text
Dim country As String = txtCountry.Text
Me.lblName.Text = name
Me.lblCountry.Text = country
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Customers (Name, Country) VALUES (@Name, @Country)", con)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Country", country)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
Screenshot
Print
Output