Insert........ Records

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 4
Reputation: mca.mdebnath is an unknown quantity at this point 
Solved Threads: 0
mca.mdebnath mca.mdebnath is offline Offline
Newbie Poster

Insert........ Records

 
0
  #1
22 Days Ago
Hope for a fruitful reply....................

Enter a number into a textbox(like 5 or 10 or something like that.

Then that number of rows are inserted into the table ..."Master"( id,serial_no,password) .

Like if i enter 10 into that textbox and submit then inserted 10 number of rows into a table ...

Plz Help me ... i have to done it by asp.net using c#.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 217
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 31
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training
 
0
  #2
22 Days Ago
You can follow the below way:
  1. //open your connection here
  2. for(int i=0; i<Convert.ToInt(TextBox1.Text);i++)
  3. {
  4. // insert HERE
  5. }
  6. //close your connection here
Last edited by mail2saion; 22 Days Ago at 2:21 am.
MARK AS SOLVED if its help you.

REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: mca.mdebnath is an unknown quantity at this point 
Solved Threads: 0
mca.mdebnath mca.mdebnath is offline Offline
Newbie Poster
 
0
  #3
21 Days Ago
Thanks 4 ur fruitful Reply But it gives some error when it insert more than 1 times ..................in a table.
Code:

  1.  
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. using System.IO;
  13. using System.Data.SqlClient;
  14.  
  15. public partial class PinGeneration : System.Web.UI.Page
  16. {
  17. ADOALL aa;
  18. private SqlConnection sconn;
  19. private SqlCommand scomm;
  20. private SqlDataReader sdr;
  21. MemoryStream ms = new MemoryStream();
  22. SqlParameter spfull_no,spserial_no,sppassword;
  23. string strCommText = "insert into Pin_Generate (id_no, serial_no, password) values(@pfull_no, @pserial_no, @ppassword)";
  24. string strCommText2 = "select last_id from Temp_ID";
  25. public PinGeneration()
  26. {
  27. sconn = new SqlConnection();
  28. scomm = new SqlCommand();
  29.  
  30. sconn.ConnectionString = ConfigurationManager.ConnectionStrings["MLMSqlServer"].ConnectionString;
  31. scomm.CommandType = CommandType.Text;
  32. // scomm.CommandText = strCommText1.Trim();
  33. scomm.CommandText = strCommText2.Trim();
  34. scomm.Connection = sconn;
  35.  
  36. spfull_no = new SqlParameter();
  37. spfull_no.SqlDbType = SqlDbType.VarChar;
  38. spfull_no.ParameterName = "pfull_no";
  39. spfull_no.Direction = ParameterDirection.Input;
  40.  
  41. spserial_no = new SqlParameter();
  42. spserial_no.SqlDbType = SqlDbType.VarChar;
  43. spserial_no.ParameterName = "pserial_no";
  44. spserial_no.Direction = ParameterDirection.Input;
  45.  
  46. sppassword = new SqlParameter();
  47. sppassword.SqlDbType = SqlDbType.VarChar;
  48. sppassword.ParameterName = "ppassword";
  49. sppassword.Direction = ParameterDirection.Input;
  50.  
  51.  
  52. }
  53.  
  54. protected void Page_Load(object sender, EventArgs e)
  55. {
  56. lbldate.Text = DateTime.Now.ToString();
  57.  
  58. try
  59. {
  60. if (sconn.State == ConnectionState.Closed)
  61. {
  62. sconn.Open();
  63.  
  64. sdr = scomm.ExecuteReader();
  65. sdr.Read();
  66.  
  67. string a = sdr["last_id"].ToString();
  68. int b = Int32.Parse(a);
  69. int c = b + 1;
  70. lblstatement.Text = "FMS00".ToString() + b;
  71. Label3.Text = c.ToString();
  72.  
  73.  
  74. lblprint_id.Text = "FMS00".ToString() + c;
  75. sconn.Close();
  76.  
  77. }
  78. }
  79. catch (Exception ex)
  80. {
  81. Response.Write(ex.ToString());
  82. }
  83.  
  84.  
  85. }
  86. private int Randam_Number(int min,int max)
  87. {
  88. Random randam = new Random();
  89. return randam.Next(min,max);
  90. }
  91.  
  92. protected void btnsubmit_Click(object sender, EventArgs z)
  93. {
  94.  
  95.  
  96. Randam_Number(14,100);
  97. int number = 3490219;
  98. lblserial_no.Text=number.ToString();
  99. lblpassword.Text = number.ToString();
  100.  
  101.  
  102. int i;
  103. for (i = 1; i <= Convert.ToInt32(txtpin_no.Text); i++)
  104. {
  105. getNum();
  106.  
  107. spfull_no.Value = lblprint_id.Text;
  108. spserial_no.Value = lblserial_no.Text;
  109. sppassword.Value = lblpassword.Text;
  110.  
  111. aa = new ADOALL(strCommText, spfull_no, spserial_no, sppassword);
  112.  
  113.  
  114.  
  115. int x = 0;
  116. try
  117. {
  118. x = aa.ExecuteStatements();
  119. }
  120. catch (SqlException sqlex)
  121. {
  122. Response.Write(sqlex.ToString());
  123. x = 0;
  124. }
  125. if (x > 0)
  126. {
  127. lblmsg.Text = "Successfully Created";
  128.  
  129. }
  130. else
  131. {
  132. lblmsg.Text = "Please Try Again";
  133. }
  134. update();
  135.  
  136. }
  137.  
  138. }
  139.  
  140.  
  141. private void getNum()
  142. {
  143. if (sconn.State == ConnectionState.Closed)
  144. {
  145.  
  146. sconn.Open();
  147.  
  148. scomm.CommandType = CommandType.Text;
  149. string strCommText2 = "select last_id from Temp_ID";
  150. scomm.CommandText = strCommText2.Trim();
  151. scomm.Connection = sconn;
  152. sdr = scomm.ExecuteReader();
  153. sdr.Read();
  154. string a = sdr["last_id"].ToString();
  155. int b = Int32.Parse(a);
  156. int c = b + 1;
  157. lblstatement.Text = "FMS00".ToString() + b;
  158. Label3.Text = c.ToString();
  159. lblprint_id.Text = "FMS00".ToString() + c;
  160. sconn.Close();
  161. }
  162. }
  163.  
  164.  
  165. private void update()
  166. {
  167. try
  168. {
  169. if (sconn.State == ConnectionState.Closed)
  170. {
  171. sconn.Open();
  172. scomm.CommandText = "UPDATE Temp_ID SET last_id='" + Label3.Text + "'"; // WHERE last_id=@last_id";
  173.  
  174.  
  175. scomm.Connection = sconn;
  176. scomm.ExecuteNonQuery();
  177.  
  178. sconn.Close();
  179. }
  180. }
  181. catch (SqlException e)
  182. {
  183. Response.Write(e.ToString());
  184. }
  185. }
  186.  
  187.  
  188.  
  189. protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  190. {
  191.  
  192. }
  193. protected void lblogout_Click(object sender, EventArgs e)
  194. {
  195. FormsAuthentication.SignOut();
  196. Response.Redirect("super_admin_login");
  197. }
  198.  
  199.  
  200.  
  201. }

ADOALL CLASS:




  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Data.SqlClient;
  11.  
  12. /// <summary>
  13. /// Summary description for ADOALL
  14. /// </summary>
  15. public class ADOALL
  16. {
  17. private SqlConnection sconn;
  18. private SqlCommand scomm;
  19. private SqlDataReader sdr;
  20.  
  21. public ADOALL(string strCommText, params SqlParameter[] spCollection)
  22. {
  23. //
  24. // TODO: Add constructor logic here
  25. //
  26. sconn = new SqlConnection();
  27. scomm = new SqlCommand();
  28.  
  29. sconn.ConnectionString = ConfigurationManager.ConnectionStrings["MLMSqlServer"].ConnectionString;
  30. scomm.CommandType = CommandType.Text;
  31. scomm.CommandText = strCommText.Trim();
  32.  
  33. scomm.Connection = sconn;
  34. scomm.Parameters.AddRange(spCollection); // Error occurs here... It
  35.  
  36.  
  37.  
  38. }
  39. public bool ConnOpen()
  40. {
  41. if (sconn.State == ConnectionState.Closed)
  42. {
  43. try
  44. {
  45. sconn.Open();
  46. return true;
  47. }
  48. catch (Exception)
  49. {
  50. return false;
  51. }
  52. }
  53. else
  54. {
  55. return true;
  56. }
  57. }
  58.  
  59. public bool ConnClose()
  60. {
  61. if (sconn.State == ConnectionState.Open)
  62. {
  63. try
  64. {
  65. sconn.Close();
  66. return true;
  67. }
  68. catch (Exception)
  69. {
  70. return false;
  71. }
  72. }
  73. else
  74. {
  75. return true;
  76. }
  77. }
  78.  
  79. public bool GetReader()
  80. {
  81. if (ConnOpen())
  82. {
  83. sdr = scomm.ExecuteReader();
  84. if (sdr.Read())
  85. {
  86. ConnClose();
  87. return true;
  88. }
  89. else
  90. {
  91. ConnClose();
  92. return false;
  93. }
  94. }
  95. else
  96. {
  97. return false;
  98. }
  99.  
  100. }
  101.  
  102. public int ExecuteStatements()
  103. {
  104. if (ConnOpen())
  105. {
  106. int x = scomm.ExecuteNonQuery();
  107. ConnClose();
  108. return x;
  109. }
  110. return 0;
  111. }
  112.  
  113.  
  114. public SqlDataReader getSqlReader()
  115. {
  116. if (ConnOpen())
  117. {
  118. sdr = scomm.ExecuteReader(CommandBehavior.CloseConnection);
  119. return sdr;
  120. }
  121. else
  122. {
  123. return null;
  124. }
  125. }
  126. public object getSqlScaler()
  127. {
  128. if (ConnOpen())
  129. {
  130. object ser = scomm.ExecuteScalar();
  131. sconn.Close();
  132. return ser;
  133.  
  134. }
  135. else
  136. {
  137. return null;
  138. }
  139. }
  140.  
  141.  
  142. }

Plz Sir, Give me a Solution... []
Last edited by peter_budo; 19 Days Ago at 9:16 am. Reason: Missing code tags on second code sample
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 217
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 31
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training
 
0
  #4
19 Days Ago
Its a bit time consuming to read your code. But i can give you one tricks & hope it will resolve your problem.

Wrap all insert related code under loop. I mean within the loop open connection execute statement then close connection. So that for 2nd iteration connection will open again then execute & close so on.

If you can can insert multiple statement in this way then you can optimize your code later.
MARK AS SOLVED if its help you.

REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC