这是searchupdate_Click代码:
protected void searchupdate_Click(object sender, EventArgs e)
{
SqlConnection con = Connection.DBconnection();
{
SqlCommand com = new SqlCommand("sp_searchupdate", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@id", textstudentid.Text);
SqlDataAdapter adp = new SqlDataAdapter(com);
DataSet ds = new DataSet();
adp.Fill(ds);
txttamil.Text = ds.Tables[0].Rows[0]["Tamil"].ToString();
txtenglish.Text = ds.Tables[0].Rows[0]["English"].ToString();
txtmaths.Text = ds.Tables[0].Rows[0]["Maths"].ToString();
txtscience.Text = ds.Tables[0].Rows[0]["Science"].ToString();
txtsocialscience.Text = ds.Tables[0].Rows[0]"SocialScience"].ToString();
}
}
当我输入学生ID和搜索时,它会在textboxes
显示学生标记。
之后我想编辑和更新。
为此,我添加了上面的代码,这里是stored procedure
:
ALTER PROCEDURE sp_searchupdate
(
@id int,
@Tamil varchar(50),
@English varchar(50),
@Maths varchar(50),
@Science varchar(50),
@SocialScience varchar(50)
)
AS
IF EXISTS (SELECT * FROM studentresult WHERE id_student='@id')
begin
SELECT id_student FROM studentresult WHERE id_student='@id'
END
当我编辑标记并点击更新按钮时,它会显示错误。
这是我的输出screenshot
我可以知道,我在代码中的错误,我是.net。
的新手任何人都可以帮助我吗?
更新
ALTER PROCEDURE sp_searchupdate
(
@id int,
@Tamil Varchar (100),
@English varchar (50),
@Maths Varchar (50),
@Science Varchar (50),
@SocialScience Varchar (50)
)
AS
IF EXISTS (SELECT * FROM studentresult WHERE id_student=@id)
BEGIN
UPDATE studentresult SET Tamil = @Tamil,English = @English, Maths = @Maths,Science = @Science,SocialScience = @SocialScience WHERE id = @id
END
答案 0 :(得分:0)
首先,您必须了解当前代码的工作原理...
让我们说当你点击searchupdate
这应该做什么样的工作?
现在,基于您的代码实际上是将TextBox
设置为您检索的Database
值。
这是你所期望的吗?答案是否
由于您的功能1用于搜索而另一个用于更新....我已经分为2个按钮。
// This is First Button
protected void btnSearch_Click(object sender, EventArgs e)
{
// What does theis code here do ??
SqlConnection con = Connection.DBconnection();
{
SqlCommand com = new SqlCommand("PROCEDURE_SELECT", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@id", txtstudentid.Text.Trim());
SqlDataAdapter adp = new SqlDataAdapter(com);
DataSet ds = new DataSet();
adp.Fill(ds);
// SEt value to TextBox & make sure your value below is not Null else it will throw you null exception due to you use .ToString()
txttamil.Text = ds.Tables[0].Rows[0]["Tamil"].ToString();
txtenglish.Text = ds.Tables[0].Rows[0]["English"].ToString();
txtmaths.Text = ds.Tables[0].Rows[0]["Maths"].ToString();
txtscience.Text = ds.Tables[0].Rows[0]["Science"].ToString();
txtsocialscience.Text = ds.Tables[0].Rows[0]["SocialScience"].ToString();
}
}
// This is second Button
protected void btnUpdate_Click(object sender, EventArgs e)
{
using (SqlConnection con = Connection.DBconnection())
{
using (SqlCommand cmd = new SqlCommand("PROCEDURE_UPDATE", con))
{
cmd.CommandType = CommandType.StoredProcedure;
// Add your Parameter
cmd.Parameters.AddWithValue("@id", txtstudentid.Text.Trim());
cmd.Parameters.AddWithValue("@tamil", txttamil.Text.Trim());
cmd.Parameters.AddWithValue("@english", txtenglish.Text.Trim());
cmd.Parameters.AddWithValue("@math", txtmaths.Text.Trim());
cmd.Parameters.AddWithValue("@science", txtscience.Text.Trim());
cmd.Parameters.AddWithValue("@socialScience", txtsocialscience.Text.Trim());
con.Open();
// Execute your Query
cmd.ExecuteNonQuery();
// Clear All The Data in Current TextBOx and press search again with the ID
txttamil.Text = string.Empty;
txtenglish.Text = string.Empty;
txtmaths.Text = string.Empty;
txtscience.Text = string.Empty;
txtsocialscience.Text = string.Empty;
Response.Write("You have updated the value... Try to Search again...");
}
// UPDATE Query as per below
// IF EXISTS (SELECT * FROM studentresult WHERE id_student='@id')
// BEGIN
// UPDATE TABLE SET tamil = @tamil and so on... WHERE id = @id
// END
// ELSE
// BEGIN
// INSERT DATA HERE
// END
}
}
SQL Proc
CREATE PROCEDURE proc_Upd_Student
-- Add the parameters for the stored procedure here
@id VARCHAR(100),
@tamil VARCHAR(100),
@english VARCHAR(100),
@maths VARCHAR(100),
@science VARCHAR(100),
@socialscience VARCHAR(100)
AS
BEGIN
-- CHECK IF THE TABLE CONTAIN THE SAME ID
IF (SELECT COUNT(1) FROM TABLE WHERE id = @id) > 0
BEGIN
-- I DO UPDATE TO THE TABLE
UPDATE TABLE
SET Column = VALUE
WHERE id = @id
END
ELSE
BEGIN
-- IF A NEW ID ? THEN I DO INSERT, IF YOUR ID IS IDENTITY THEN YOU CAN SKIP IT NO NEED TO INSERT
INSERT INTO TABLE (COLUMN)
VALUE
(PARAM)
END
END
上面的商店程序适用于Update & Insert
...由于程序不同,您的Select proc
可以保留。如果要合并,请添加一个名为@Action
答案 1 :(得分:0)
由于您的SP需要参数。
您必须将预期参数传递给C#中的存储过程。如下所示,
<强> btnSearch_Click 强>
com.Parameters.AddWithValue("@Tamil", "");
com.Parameters.AddWithValue("@English", "");
com.Parameters.AddWithValue("@Maths", "");
com.Parameters.AddWithValue("@Science", "");
com.Parameters.AddWithValue("@SocialScience", "");
<强> btnUpdate_Click 强>
// Add your Parameter
cmd.Parameters.AddWithValue("@id", txtstudentid.Text);
cmd.Parameters.AddWithValue("@tamil", txttamil.Text);
cmd.Parameters.AddWithValue("@english", txtenglish.Text);
cmd.Parameters.AddWithValue("@math", txtmaths.Text);
cmd.Parameters.AddWithValue("@science", txtscience.Text);
cmd.Parameters.AddWithValue("@socialScience", txtsocialscience.Text);