2
Answers

How to save large data in Database from c# Windows App

Photo of Suraj Kumbhar

Suraj Kumbhar

8y
309
1
hello guys,
I am working on ERP project which is being developed in C# Windows Form Application. It is multi-tier application which has UI,Business,Data Access, Data & property layer. When I try to save data like 4 items at a time. It's taking 2.5 seconds time to execute layer in folowing order.
UI --> Business --> DataAccess .
But when it comes to Data Layer it takes more than 6-7 seconds for completing saving process. I am optimizing code which has done by my seniors. I have attatched a sample code file which consist a Save function in Data layer. Please go through it and suggest what code changes should I do to work it faster.
Thanks & Regards,
Suraj 

Attachment: samplecode.rar

Answers (2)

0
Photo of Riddhi Valecha
NA 3.1k 186.9k 11y
String strConnString = ConfigurationManager.ConnectionStrings["EMPLOYEEConnectionString2"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
               

You have not assigned the connection string to SQLConnection Object.


0
Photo of Vishal Gilbile
NA 13.9k 2.9m 12y
Hello Friend,
                The issue lies over here.

you access the connection string value from web.config file

   String strConnString = ConfigurationManager.ConnectionStrings["EMPLOYEEConnectionString2"].ConnectionString


You initialized your SqlConnection object

 SqlConnection con = new SqlConnection();

and in the btnSave click event

you have not set the SqlConnection ConnectionString property

what you need to do is inside btnSave Click event

con.ConnectionString=strConnString;

and your issue will be solved.
0
Photo of Riddhi Valecha
NA 3.1k 186.9k 12y
Hi...

Can you share the project/website??

It becomes difficult here to explain.
0
Photo of Jignesh Trivedi
NA 61.3k 14.2m 12y

hi,

this error might due to wrong Connection string in web config.
can you please share it with us.

else verified your config with following example
<connectionStrings>
    <add name="EMPLOYEEConnectionString2" connectionString="Data Source=databaseserverName;user id=sa;password=password;Initial Catalog=databaseName;"/>
  </connectionStrings>

hope this will help you.

0
Photo of Satyapriya Nayak
NA 53k 8m 12y
public partial class j : System.Web.UI.Page
{
// SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EMPLOYEEConnectionString2"].ConnectionString);
int intcount;
SqlCommand cmd;
string sqlstr;
int count;
String strConnString = ConfigurationManager.ConnectionStrings["EMPLOYEEConnectionString2"].ConnectionString;





protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "JOINMAX";
cmd.Parameters.Add("@NAME", SqlDbType.VarChar).Value = txtName.Text.Trim();
cmd.Parameters.Add("@EMAIL", SqlDbType.VarChar).Value =txtEMAIL.Text.Trim();
cmd.Parameters.Add("@PHONENO", SqlDbType.VarChar).Value =txtPHONE.Text.Trim();
cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;






cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
string message = (string)cmd.Parameters["@ERROR"].Value;
Label1.Text = "Record inserted successfully";
}
catch (Exception ex)
{
throw ex;
}
finally
{

con.Close();
con.Dispose();
}
0
Photo of gaurav singh
NA 47 14.3k 12y
you can use this code it would be help  to you....


 SqlConnection cn = new SqlConnection();
       cn.ConnectionString = ConfigurationManager.ConnectionStrings["EMPLOYEEConnectionString2"].ConnectionString.ToString();
        cn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "JOINMAX";
        cmd.Parameters.Add("@NAME", SqlDbType.VarChar).Value = "Gaurav";
        cmd.Parameters.Add("@EMAIL", SqlDbType.VarChar).Value = "@gmail.com";
        cmd.Parameters.Add("@PHONENO", SqlDbType.VarChar).Value = "9711182587";
        cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
        cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;

        cmd.Connection = cn;
        try
        {
           
            cmd.ExecuteNonQuery();
            string message = (string)cmd.Parameters["@ERROR"].Value;
            string msg = "Record inserted successfully";
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {


            cn.Close();
            cn.Dispose();
        }