Hi muhammad12,
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 contents = document.getElementById('pnlDetails').innerHTML;
var frame1 = document.createElement('iframe');
frame1.name = "frame1";
frame1.style.position = "absolute";
frame1.style.top = "-1000000px";
document.body.appendChild(frame1);
var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
frameDoc.document.open();
frameDoc.document.write('<html><head>');
frameDoc.document.write('</head><body>');
frameDoc.document.write(contents);
frameDoc.document.write('</body></html>');
frameDoc.document.close();
window.frames["frame1"].focus();
window.frames["frame1"].print();
document.body.removeChild(frame1);
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) Handles Me.Load
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
Database