Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Iam having master table(MasterTable) below
ID StatusID GenderID TypeID createdBy
1 2 1 2 1

Iam having excel data as below

Status Gender Type
Assigned Male Individual
WIP Female Corporate


How do I upload the date into table(MasterTable)

Subtables

1)Status master

ID Desc
1 Assigned
2 WIP

2)GenderMaster

ID Desc
1 Male
2 Female

3)Type Master

ID Desc
1 Individual
2 Corporate


Please help..
Posted

1 solution

Read the excel data to a datatable.
Now, you can create a sp like-
SQL
CREATE PROC YourProc
(
   @Status VARCHAR(50),
   @Gender VARCHAR(50),
   @Type VARCHAR(50)
)
AS
BEGIN
   --Get Status ID
   DECLARE @StatusID INT 
   IF(EXISTS(SELECT ID FROM StatusMaster WHERE Desc=@Status))
   BEGIN
     --check if the status already exists then get the id
     SELECT @StatusID=ID FROM StatusMaster WHERE Desc=@Status
   END
   ELSE
   BEGIN
     --else insert a new entry and get the inserted id
     INSERT INTO StatusMaster VALUES(@Status)
     SELECT @StatusID = IDENT_CURRENT(‘StatusMaster’)
   END

   --Get Gender ID
   DECLARE @GenderID INT 
   IF(EXISTS(SELECT ID FROM GenderMaster WHERE Desc=@Gender))
   BEGIN
     SELECT @GenderID =ID FROM GenderMaster WHERE Desc=@Gender
   END
   ELSE
   BEGIN
     INSERT INTO GenderMaster VALUES(@Gender)
     SELECT @GenderID = IDENT_CURRENT(‘GenderMaster’)
   END

   --Get Type ID
   DECLARE @TypeID INT 
   IF(EXISTS(SELECT ID FROM TypeMaster WHERE Desc=@Type))
   BEGIN
     SELECT @TypeID =ID FROM TypeMaster WHERE Desc=@Type
   END
   ELSE
   BEGIN
     INSERT INTO TypeMaster VALUES(@Type)
     SELECT @TypeID = IDENT_CURRENT(‘TypeMaster’)
   END

   --insert data to master table
   INSERT INTO MasterTable (StatusID, GenderID, TypeID, createdBy)
   VALUES(@StatusID,@GenderID,@TypeID,'')
END


Call this for each row of your datatable.

Hope, it helps :)
Update:
As per OP's request here is the code for ado.net.
C#
foreach(DataRow dr in yourDatatable.Rows)
{
    using (SqlConnection conn = new SqlConnection("Server=YourSQLServerInstanceName;DataBase=YourDBName;UserId=YourUserId;Password=YourPassword")) // replace with your connection string
    {
       conn.Open();
       SqlCommand cmd  = new SqlCommand("YourProc", conn);
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.Parameters.Add(new SqlParameter("@Status", dr["Status"].ToString()));
       cmd.Parameters.Add(new SqlParameter("@Gender", dr["Gender"].ToString()));
       cmd.Parameters.Add(new SqlParameter("@Type", dr["Type"].ToString()));
       cmd.ExecuteNonQuery();
    }
}


This should do your job. If not, please let me know :)
 
Share this answer
 
v2
Comments
v2vinoth 9-Jul-15 6:18am    
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", OleDbcon);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
ds = new DataSet();
objAdapter1.Fill(ds);
Dt = ds.Tables[0];
I stored the excel into above datatable...I created a procedure as you mentioned.
Then how will I call the sp and insert the data..
Suvendu Shekhar Giri 9-Jul-15 6:48am    
Please check the updated solution.
v2vinoth 10-Jul-15 2:00am    
Yes its working but I want to show error as any null values in excel
Suvendu Shekhar Giri 10-Jul-15 2:09am    
That's purely validation stuff.
In the loop before executing the stored proc, do the validation like-
if(dr["Status"].ToString().Trim().Equals(string.Empty))
{
//show message
continue; // skip the current entry in the loop
}
v2vinoth 10-Jul-15 2:17am    
Hi Thanks for you quick update..Please check this once is correct
if (dr["ApplicationType"].ToString().Trim().Equals(string.Empty))
{
Response.Write("Unable insert Null value in the column ApplicationType");
continue; // skip the current entry in the loop
}
else
{
cmd.Parameters.Add(new SqlParameter("@ApplicationType", dr["ApplicationType"].ToString()));
cmd.Parameters.Add(new SqlParameter("@Title", dr["Title"].ToString()));
cmd.Parameters.Add(new SqlParameter("@FirstName", dr["FirstName"].ToString()));
cmd.Parameters.Add(new SqlParameter("@LastName", dr["LastName"].ToString()));
cmd.Parameters.Add(new SqlParameter("@Gender", dr["Gender"].ToString()));
con.Open();
bool success = Convert.ToBoolean(cmd.ExecuteScalar());
//object obj = cmd.ExecuteScalar();
if (success)
{

System.Web.HttpContext.Current.Response.Write("<script language=\"javaScript\">" + "alert('Updated!');" + "window.location.href='QuickLead.aspx';" + "<" + "/script>");
}
else
{

ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Correct the excel data and Re upload!!');", true);
}
con.Close();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900