Search

Saturday, December 20, 2014
0 comments

How to insert data into multiple table in ASP.NET

12:00 AMSaturday, December 20, 2014
I have two table, such as complaint table and status table. Now, i want to add data in both table through same SqlCommand class instance. Now, first to prepare the Connection using SqlConnection class, after that design the query for first table after inserting the data, we can add data into other table.



You can check the complete.

Source Code

<table style="width:100%;">
    <tr>
        <td class="style3">
            Complaint against To</td>
    </tr>
    <tr>
        <td class="style1">
            Name :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pname" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
                ControlToValidate="pname" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Business&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pbusiness" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" 
                ControlToValidate="pbusiness" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Address&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="padd" runat="server" Height="61px" Width="248px" 
                TextMode="MultiLine" ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
                ControlToValidate="padd" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Phone Number&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pnumber" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" 
                ControlToValidate="pnumber" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Faulty Proff&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pfproff" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" 
                ControlToValidate="pfproff" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Faulty Proff Image&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:FileUpload ID="pproffimage" runat="server" />
        </td>
    </tr>
    <tr>
        <td class="style1">
            Review Against&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="preview" runat="server" Height="55px" Width="248px" 
                TextMode="MultiLine" ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" 
                ControlToValidate="preview" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Fir Number (if any)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pfir" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server" 
                ControlToValidate="pfir" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
</table>
<p>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
</p>
<p>
    <asp:Label ID="Label1" runat="server"></asp:Label>
</p>

<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" 
    ValidationGroup="cr" />

Design View Of Source code

Complaint form

Complete Business Logic Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class UserControl_complaint : System.Web.UI.UserControl
{
    string savefile = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string un=System.Guid.NewGuid ().ToString ();

        if (pproffimage.HasFile)
        {
            pproffimage.SaveAs(Server.MapPath("~/images/" + pproffimage.FileName));
            savefile = "~/images/" + pproffimage.FileName;

        }
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "insert into complaint(Party_Name,Business,Address,Faulty_proff,Image,Review,Fir_Numer,Consumer_Id,date,uniquet) values(@cuoname,@cuofname,@cuodob,@cuoadd,@cuoimage,@cuophone,@cuoemail,@cuoph,@cuodate,@cuoun)";
        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@cuoname",pname.Text);
        cmd.Parameters.AddWithValue("@cuofname",pbusiness.Text);
        cmd.Parameters.AddWithValue("@cuodob", padd.Text);
        cmd.Parameters.AddWithValue("@cuoadd",pfproff .Text);
        cmd.Parameters.AddWithValue("@cuoimage", savefile);
        cmd.Parameters.AddWithValue("@cuophone",preview.Text);
        cmd.Parameters.AddWithValue("@cuoemail", pfir .Text);
        cmd.Parameters.AddWithValue("@cuoph",Session["cons_id"].ToString());
        cmd.Parameters.AddWithValue("@cuodate", DateTime.Now.ToString ());
         cmd.Parameters.AddWithValue("@cuoun", un);
        int a = cmd.ExecuteNonQuery();
        if (a > 0)
        {
            Label1.Text = "Data Inserted Sucessfully";
            Label1.ForeColor = System.Drawing.Color.Green;
        }
        cmd.Parameters.Clear();

        cmd.CommandText = "insert into status(status,Complaint_Id,consumer_id) values(@stname,@comid,@cunsid)";
        cmd.Parameters.AddWithValue("@stname", "pending");
        cmd.Parameters.AddWithValue("@comid",un);
        cmd.Parameters.AddWithValue("@cunsid", Session["cons_id"].ToString());
        cmd.ExecuteNonQuery();

    }
}
Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top