.NETGURU
Insert records whose values increment
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-sqlclient' list.


Jon Le
-- Moved from [aspngdata] to [ngfx-sqlclient] by Let the good times roll <Click here to reveal e-mail address> --

I was wondering if someone can help me with the following code, which
is behind a web form.

String insertCmd = "insert into dbo.ar_gl_mid_lvl_code_new values
(@major_payor," +
" @sub_payor, @patclass, @mid_lvl_code)";
            
// Initialize the SqlCommand with the new SQL string
// and the connection information.
SqlCommand myCommand = new SqlCommand(insertCmd, myConnection);

int StartValue = Convert.ToInt32(this.txtMidCodeStartValue.Text);
int EndValue = Convert.ToInt32(this.txtMidCodeEndValue.Text);
string Mcode = this.ddlMRMC.SelectedItem.Value;
            
int MajorPayor = Convert.ToInt32 (this.txtMajorPayorNo.Text);
int SubPayor = Convert.ToInt32 (this.txtSubPayorNo.Text);
char PatClass = Convert.ToChar (this.ddlIO.SelectedItem.Text);
            
// Create new parameters for the SqlCommand object and
// initialize them to the input-form field values.
myCommand.Parameters.Add(new SqlParameter("@major_payor",SqlDbType.Int,
2));
myCommand.Parameters.Add(new SqlParameter("@sub_payor",SqlDbType.Int,
2));
myCommand.Parameters.Add(new SqlParameter("@patclass",SqlDbType.Char,
2));
myCommand.Parameters.Add(new
SqlParameter("@mid_lvl_code",SqlDbType.VarChar, 10));

int n;
for (n = StartValue; n <= EndValue; n++)
{

/*Right now, this logic just inserts one record, the last instance of
"n". I would like to insert all records up to the last instance of
"n". How can I do that? I have done this with a dropdownlist control
because it has an "add" method. Does the SqlCommand object have a
similiar method? How can I implement it?
*/
    
myCommand.Parameters["@major_payor"].Value = MajorPayor;
myCommand.Parameters["@sub_payor"].Value = SubPayor;
myCommand.Parameters["@patclass"].Value = PatClass;
myCommand.Parameters["@mid_lvl_code"].Value = Mcode +
(Convert.ToString(n).PadLeft(8,'0'));

}
            
//open db connection, execute, close
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();    
}

Thank you in advance for your help.

__________________________________________________
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com

Reply to this message...
 
    
Calhoun, Dot Net Guruhew
All you should have to do is put the=20

"myCommand.Connection.Open();" before your for loop and

"myCommand.ExecuteNonQuery();" inside your for loop. This will execute
the insert for each item in the loop.

-----Original Message-----
From: Jon Le [mailto:Click here to reveal e-mail address]=20
Sent: Friday, July 26, 2002 5:19 PM
To: ngfx-sqlclient
Subject: [ngfx-sqlclient] Insert records whose values increment

-- Moved from [aspngdata] to [ngfx-sqlclient] by Let the good times roll
<Click here to reveal e-mail address> --

I was wondering if someone can help me with the following code, which
is behind a web form. =20

String insertCmd =3D "insert into dbo.ar_gl_mid_lvl_code_new values
(@major_payor," +
" @sub_payor, @patclass, @mid_lvl_code)";
        =09
// Initialize the SqlCommand with the new SQL string=20
// and the connection information.
SqlCommand myCommand =3D new SqlCommand(insertCmd, myConnection);

int StartValue =3D Convert.ToInt32(this.txtMidCodeStartValue.Text);
int EndValue =3D Convert.ToInt32(this.txtMidCodeEndValue.Text);
string Mcode =3D this.ddlMRMC.SelectedItem.Value;
        =09
int MajorPayor =3D Convert.ToInt32 (this.txtMajorPayorNo.Text);
int SubPayor =3D Convert.ToInt32 (this.txtSubPayorNo.Text);
char PatClass =3D Convert.ToChar (this.ddlIO.SelectedItem.Text);
        =09
// Create new parameters for the SqlCommand object and
// initialize them to the input-form field values.
myCommand.Parameters.Add(new SqlParameter("@major_payor",SqlDbType.Int,
2));
myCommand.Parameters.Add(new SqlParameter("@sub_payor",SqlDbType.Int,
2));
myCommand.Parameters.Add(new SqlParameter("@patclass",SqlDbType.Char,
2));
myCommand.Parameters.Add(new
SqlParameter("@mid_lvl_code",SqlDbType.VarChar, 10));

int n;
for (n =3D StartValue; n <=3D EndValue; n++)
{

/*Right now, this logic just inserts one record, the last instance of
"n". I would like to insert all records up to the last instance of
"n". How can I do that? I have done this with a dropdownlist control
because it has an "add" method. Does the SqlCommand object have a
similiar method? How can I implement it?
*/
=09
myCommand.Parameters["@major_payor"].Value =3D MajorPayor;
myCommand.Parameters["@sub_payor"].Value =3D SubPayor;
myCommand.Parameters["@patclass"].Value =3D PatClass;
myCommand.Parameters["@mid_lvl_code"].Value =3D Mcode +
(Convert.ToString(n).PadLeft(8,'0'));

}
        =09
//open db connection, execute, close
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();=09
}

Thank you in advance for your help.

__________________________________________________
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com
| [ngfx-sqlclient] member Click here to reveal e-mail address =3D YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-sqlclient.asp =3D JOIN/QUIT

Reply to this message...
 
 
System.Convert
System.Data.SqlClient.SqlCommand
System.Data.SqlClient.SqlParameter
System.Data.SqlDbType




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification