2 ضمیمه
درخواست راهنمایی در خصوص ارتباط با stored procedure ایجاد شده
سلام دوستان؛
در ابتدا این تصویری از بانکی هستش که ایجاد کردم:
ضمیمه 77364
تصویر بانک
حالا من در قسمت تعریف stored procedure طبق شکل زیر یک SP ایجاد کردم با نام GetTitlebyID:
ضمیمه 77365
تصویر SP
حالا در قسمت کد نویسی به شکل زیر عمل کردم؛ ولی راستش رو بخواین هر عددی رو که توی textbox وارد می کنم همون رو نشون میده و اون چیزی که من میخوام رو انجام نمیده! میشه بگید ایراد کار از کجاست؟
protected void BtnShow_Click(object sender, EventArgs e)
{
conObj = new SqlConnection("Data Source=df;Initial Catalog=ee;User Id=sa;Password=sasasa;");
SqlCommand cmd = new SqlCommand("GetTitlebyID", conObj);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ID", TextBox1.Text);
conObj.Open();
object tn=cmd.ExecuteScalar();
int t;
t=(int)tn;
Response.Write(t);
conObj.Close();
}
البته شاید علت به این دلیل باشه که من با ()response.write میخوام نتایج رو نشون بدم ولی حتی وقتی که بعنوان مثال می خوام تعداد رکوردها رو نیز که با این دستور قابل مشاهده ست ببینم بازهم بنظر ایراد میگیره!
ممنون میشم راهنماییم کنید.
نقل قول: درخواست راهنمایی در خصوص ارتباط با stored procedure ایجاد شده
نقل قول: درخواست راهنمایی در خصوص ارتباط با stored procedure ایجاد شده
سلام.
هنگام پاس دادن پارامتر آن را به int تبدیل کن
نقل قول: درخواست راهنمایی در خصوص ارتباط با stored procedure ایجاد شده
سلام
شما در دستور select *
نوشتید، یعنی تمام فیلد ها، ولی در قسمت سی شارپ از ExecuteScalar استفاده کردید، طبیعیه که فقط id رو نشون بده.
executescalar، برای هر sp که بنویسید، فیلد اول از رکورد اول رو نشون می ده، یعنی یک فیلد فقط!
برای اینکه به تمام فیلد ها دسترسی داشته باشید، از ExecuteReader استفاده کنید
موفق باشید
نقل قول: درخواست راهنمایی در خصوص ارتباط با stored procedure ایجاد شده
conObj = new SqlConnection("Data Source=df;Initial Catalog=ee;User Id=sa;Password=sasasa;");
SqlCommand cmd = new SqlCommand("GetTitlebyID", conObj);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ID", TextBox1.Text);
conObj.Open();
SqlDataReader dr=cmd.ExcuteReader();
while(dr.read())
{
string Title=dr["Title"].ToString();
string Abstract=dr["Abstract"].Tostring();
// ...
//
}
Response.Write(Title);// be jaye title mitavan har moteghayer digari gozasht
dr.Close();
conObj.Close();
نقل قول: درخواست راهنمایی در خصوص ارتباط با stored procedure ایجاد شده
کاربر par4parvaz لطف کردن کد رو گذاشتن، در ادامه و تصحیح این کد، متغیر ها رو بیرون از حلقه تعریف کنید که قابل دسترسی باشن. یعنی string title رو قبل از حلقه تعریف کنید(با یک مقدار اولیه)، و در حلقه مقدار دهیش کنید.
موفق باشید