|
I have a Table.It is containing 4 column RegionID, MapFillColor, MapLineColor, MapCoordinates.
I want to run insert statement if the record is new when button press. But if the record already exist it should update the record REGIONID is primary Key in my Table. I donot know where i should put my function which check first the record exist if so then run update else insert statement.
Here is my Code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=ZAFAR-PC\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("Insert into Region(RegionID,MapFillColor, MapLineColor, MapCoordinates) values( '" + RegionID_txt + "','" + MapFillColor_txt + "','" + MapLineColor_txt + "','" + MapCoordinates_txt + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
|
|
|
|
|
The T-SQL statement looks like this:
if(select count(*) from tablename where columnname='value') = 0<br />
begin<br />
insert into tablename values('value1','value2','value3','value4'.....)<br />
else<br />
Update tablename set columnvalue='newvalue' where regionID='value'<br />
end
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
Since the T-SQL has already been given, I'll only add this:
don't put the SQL statement directly in the code; use a stored procedure instead. If the need arises to alter the SQL statement, it can be done at the stored procedure level without altering the code itself.
Tim
|
|
|
|
|
Typically,am new in stored procedures and have been trying to do the following.
To copy/move data in Payments Table to CustomerAccounts.The id in payment is CashId and want to Copy just the data when i pass a specific CashID.Here are my sample stored Procedures.
the Source of Data is "PaYments"
Destinatin is "CustomerAccounts".Any assistance is greatly appreciated.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 1
CREATE procedure Payments_CustomerAccounts
DECLARE @CatInsert varchar(2000),@CashID int,@Pay_Date date,@Amount money,@DrCr varchar(20), @PaymentModeId int,@BranchID int,@DeptID int
SET @CatInsert = 'INSERT INTO CUSTOMERACCOUNTS (ClientID,[Date],AmountPaid,[Cr/Dr],PaymentModeID,BranchID,DeptID) SELECT ' + CONVERT(varchar,@CashID) + ', Pay_Date,AmountPaid,PaymentModeID,BranchID,DeptID From PAYMENTS Where ClientID IN(' + @CashID + ')'
insert Into CUSTOMERACCOUNTS
([ClientID],[Date],[AmountPaid],[Cr/Dr],[PaymentModeID],[BarnchID],[DeptId]) VALUES(@CashId,@Pay_Date,@Amount,@[Cr/Dr],[@PaymentModeID],[BranchID],[DeptID])
-----------------2
Create Procedure INSERT_INTO_CUSTOMERACCOUNTS
Declare @CashID int
SELECT (cashID,Pay_Date,Amount,PaymentModeID,BranchID,DeptID)
From ACCOUNTS.PAYMENTS
Where CAshID=@CashID
INSERT INTO CUSTOMERACCOUNTS([ClientID],[Date],[AmountPaid],[PaymentModeID],[BranchID],[DeptId])VALUES(@CashID,@Pay_Date,Amount,PaymentModeId,BranchID,DeptID)
|
|
|
|
|
If I understood the problem correctly, in the first proc, instead of declaring @CashID as a variable, pass it as a parameter to procedure. After that a single INSERT INTO ... SELECT ... FROM ... WHERE ... will be sufficient.
Refer to INSERT (Transact-SQL)[^] and CREATE PROCEDURE (Transact-SQL)[^]
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Hi,
CRecordSet::Update( ) is throwing the following exception.
Error:The cursor does not include the table being modified or the table is not updatable through the cursor
I donot know what could be the solution.
I am currently using SQL-Server 2005 ,VC++6.0 . Any help would be appreciated.
Thanks
|
|
|
|
|
|
This user is a habitual cross-poster. This message is in THREE forums.
Blog link to be reinstated at a later date.
|
|
|
|
|
hi ,
i make simple method that retrieve data from tables this is easy but my problem happen when i retrieve data from table that have relationship with other table, i make class to set value into properties when i read and bind it in Gridview.
i make for each table class that contains to properties and if i make all table in one class it work so i want to find solution to it with do this.
Tables
Create Table Catgerories (
CatgId int PRIMARY KEY,
Catgname nvarchar(50)
)
Create Table StudentInfo(
stId int PRIMARY KEY,
Stname nvarchar(50),
CatgId int REFERENCES Catgerories (CatgId)
)
create PROCEDURE dbo.StoredProcedure2
AS
SELECT StudentInfo.*, Catgerories.Catgname
FROM Catgerories INNER JOIN
StudentInfo ON Catgerories.CatgId = StudentInfo.CatgId
RETURN
Code:
public class CatgBL
{
int cid = 0;
public int Cid
{
get { return cid; }
set { cid = value; }
}
string catgName;
public string CatgName
{
get { return catgName; }
set { catgName = value; }
}
}
public class MyStudent
{
int stid;
public int Stid
{
get { return stid; }
set { stid = value; }
}
string sname;
public string Sname
{
get { return sname; }
set { sname = value; }
}
int catgid;
public int Catgid
{
get { return catgid; }
set { catgid = value; }
}
}
SqlConnection myCon = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\TestDb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
public ArrayList RetriveStdentInfo()
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = myCon;
cmd.CommandText = "dbo.StoredProcedure2";
cmd.CommandType = CommandType.StoredProcedure;
myCon.Open();
IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
ArrayList sequence = new ArrayList();
ArrayList seqCat = new ArrayList();
ArrayList seqStudent = new ArrayList();
while (reader.Read())
{
MyStudent std = new MyStudent();
CatgBL cat = new CatgBL();
std.Stid = reader.GetInt32(0);
std.Sname = reader.GetString(reader.GetOrdinal("stname"));
std.Catgid = reader.GetInt32(reader.GetOrdinal("Catgid"));
cat.CatgName = reader.GetString(reader.GetOrdinal("CatgName"));
seqCat.Add(cat);
seqStudent.Add(std);
sequence.AddRange(seqStudent);
sequence.AddRange(seqCat);
}
return sequence;
}
|
|
|
|
|
I'm not sure if I understood you correctly, but I think you have two choices.
1. when looping through rows, create a new object for category only when the primary key changes. Because you have joined the data when fetching, you will have the same category multiple times.
2. fetch categories and students separately.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Thanks for you,may be i understand some things but i have some lack in ur soltion , i do this changes in code but the problem is still
public ArrayList RetriveStdentInfo()
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = myCon;
cmd.CommandText = "dbo.StoredProcedure2";
cmd.CommandType = CommandType.StoredProcedure;
myCon.Open();
IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
ArrayList sequence = new ArrayList();
ArrayList seqCat = new ArrayList();
ArrayList seqStudent = new ArrayList();
while (reader.Read())
{
CatgBL cat = new CatgBL();
cat.CatgName = reader.GetString(reader.GetOrdinal("CatgName"));
seqCat.Add(cat);
seqCat.Add(cat);
}
while (reader.Read())
{
MyStudent std = new MyStudent();
std.Stid = reader.GetInt32(0);
std.Sname = reader.GetString(reader.GetOrdinal("stname"));
std.Catgid = reader.GetInt32(reader.GetOrdinal("Catgid"));
seqStudent.Add(std);
}
sequence.AddRange(seqCat);
sequence.AddRange(seqStudent);
return sequence;
}
|
|
|
|
|
That's not quite what I meant. Something like this:
int prevCatId = 0;
while (reader.Read())
{
MyStudent std = new MyStudent();
std.Stid = reader.GetInt32(0);
std.Sname = reader.GetString(reader.GetOrdinal("stname"));
std.Catgid = reader.GetInt32(reader.GetOrdinal("Catgid"));
if (prevCatID != reader.GetInt32(reader.GetOrdinal("Catgid")))
{
CatgBL cat = new CatgBL();
cat.CatgName = reader.GetString(reader.GetOrdinal("CatgName"));
seqCat.Add(cat);
prevCatID = reader.GetInt32(reader.GetOrdinal("Catgid"));
}
seqStudent.Add(std);
sequence.AddRange(seqStudent);
sequence.AddRange(seqCat);
}
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Thanks again but the problem is still running
Object does not match target type
when i make debugging code the result is right but the problem appear in binding
StudentDAL std = new StudentDAL();
DataGrid1.DataSource = std.RetriveStdentInfo();
DataGrid1.DataBind();
|
|
|
|
|
I'm not sure if DataGrid can handle two dimensional arrays. Since you return an arraylist which contains arraylists, it can be problematic.
I also noticed that you add the same elements several times. That can also cause problems. Move the following lines outside the read loop:
sequence.AddRange(seqStudent);
sequence.AddRange(seqCat);
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
|
The situation in the article is a little bit different. When you setup an array of entities, all entities have properties, so now you have used two dimensions (for example if student is a row and name property is a column).
What kind of layout are you trying to achieve in DataGrid? Something like the following?
PersonID Name Age
1 John 32
2 Kendra 20
3 Steve 29
...
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
|
Okay,
then you have modify your method so that you return an arraylist that contains only instances of one class. Now you return an arraylist that contains nested arraylists and I don't believe it will work.
If your layout should be like that, my original reply is misleading. You should combine CatgBL and Student classes to one class only. After that you can create instances of that class to an arraylist and then return the list and display it.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Thanks for u to be interested to help me to correct code to work well but i say when i post the question " if i make all table in one class it work" and what motivate me to find solution and make each classes are separated is that when i make all attributes in one class this make problem if i want to display the data of any table only without display other so if i do this when i add object, the other attributes which i skip from class give default values to skipped values by zero so this force me to make one class for the state of joining when i want to display record that have a relation with other table and make one when i want to display the data of one table. ,thanks again for u and may Allah bless u for u effort.
|
|
|
|
|
Basically there is nothing wrong if you keep the data in two separate classes like in the first post. The problem is mainly in DataGrid.
Perhaps you could use more powerful DataGridView instead. With that control you could create a combo box column for category so you wouldn't have problems with empty foreign key properties. This way you could have a combobox for user to select what category he wants to use for a person and if category is selected, it will be shown.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
I Make some changes in code and data displayed but not displayed in normal form but each data in column displayed in row and no header text to columns.and the question also if i make another method to update data , my work is to do changes in destination array and compare the id that hold in array with the record in db and apply changes or wat?
public ArrayList RetriveStdentInfo()
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = myCon;
cmd.CommandText = "dbo.StoredProcedure2";
cmd.CommandType = CommandType.StoredProcedure;
myCon.Open();
IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
ArrayList seqCat = new ArrayList();
ArrayList seqStudent = new ArrayList();
<code>ArrayList destnation = new ArrayList();</code>
int prevCatId = 0;
while (reader.Read())
{
MyStudent std = new MyStudent();
std.Stid = reader.GetInt32(0);
std.Sname = reader.GetString(reader.GetOrdinal("stname"));
std.Catgid = reader.GetInt32(reader.GetOrdinal("Catgid"));
if (prevCatId != reader.GetInt32(reader.GetOrdinal("Catgid")))
{
CatgBL cat = new CatgBL();
cat.CatgName = reader.GetString(reader.GetOrdinal("CatgName"));
seqCat.Add(cat);
prevCatId = reader.GetInt32(reader.GetOrdinal("Catgid"));
}
seqStudent.Add(std);
}
<code> foreach ( MyStudent var in seqStudent)
{
destnation.Add((int)var.Catgid);
destnation.Add((string)var.Sname);
destnation.Add((int)var.Stid);
}
foreach (CatgBL var in seqCat)
{
destnation.Add((int)var.Cid);
destnation.Add((string)var.CatgName);
}
return destnation;</code>
}
|
|
|
|
|
|
you can use constraint on C# code to joid two tables
|
|
|
|
|
but why ? and what is the benefit if i do ?
|
|
|
|
|
can you explain your problems
|
|
|
|