الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

help me in this problem

مغلق
بدأه Amr_Saafan في 7 ديسمبر 2005 · 1 رد · 445 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

Alsalamo Alaikom,

first,i want to thank everybody help me before or just think to help me,and also Sorry for intrupting you many times, but now i have a new problem.

Look! I have a table that contain an arabic row and english row, i want to call the the records in Company_Name which is english and put it in a dropdown list or listbox. i tried to make for loop and think in it many times but couldn't reach to a solution.

when i postponed this problem to get a solution i bind the dropdownlist ot listbox with the table so it contains english and arabic records now. my aim is to delete the selected row according to the selected value.

this is the code:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
sqlDataAdapter1.Fill(dataSet11);
sqlDataAdapter1.Fill(dataSet11);
ListBox1.DataBind();
}

private void Button1_Click(object sender, System.EventArgs e)
{
if(ListBox1.SelectedValue == null)
{
Label2.Text="No items is selected"; 
}
else 
{ 
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection= sqlConnection1;
cmd1.CommandText="select Company_ID from "
+"Companies where Company_Name='"
+ListBox1.SelectedItem.Text+"'"; 
sqlConnection1.Open();
int compID =int.Parse(cmd1.ExecuteScalar().ToString());
sqlConnection1.Close();
SqlCommand cmd2= new SqlCommand();
cmd2.Connection= sqlConnection1;
cmd2.CommandText="delete from Companies where "
+"Company_ID="+compID+" or Company_ID="+(compID+1);
sqlConnection1.Open();
cmd2.ExecuteNonQuery();
Label2.Text="You have deleted <b>"+ListBox1.SelectedItem.Text
+" Company <b> successfully;)";
sqlConnection1.Close();
}

}

when i build it,no rows was deleted and an error message appears which tells me:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Line 304: SqlCommand cmd1 = new SqlCommand(); Line 305: cmd1.Connection= sqlConnection1; Line 306: cmd1.CommandText="select Company_ID from " Line 307: +"Companies where Company_Name='" Line 308: +ListBox1.SelectedItem.Text+"'";

تم تعديل هذه المشاركة بواسطة Amr_Saafan في 7 ديسمبر 2005 في 14:26

Amr Elsayed

meshmesh40@hotmail.com

http://groups.yahoo.com/group/Developers_For_Ever

#2

بسم الله الرحمن الرحيم

حاول تغير السطر الأول من الكود

try replace code ListBox1.SelectedValue In first line

        private void Button1_Click(object sender, System.EventArgs e)
        {
            if (ListBox1.SelectedItem == null)
            {
                Label2.Text = "No items is selected";
            }
            else
            {
                SqlCommand cmd1 = new SqlCommand();
                cmd1.Connection = sqlConnection1;
                cmd1.CommandText = "select Company_ID from "
                + "Companies where Company_Name='"
                + ListBox1.SelectedItem.Text + "'";
                sqlConnection1.Open();
                int compID = (int)cmd1.ExecuteScalar(); // مادمنا نعرف مسبقاًً أن الناتج Int فالكتاية هكذا أفضل
                sqlConnection1.Close();
                SqlCommand cmd2 = new SqlCommand();
                cmd2.Connection = sqlConnection1;
                cmd2.CommandText = "delete from Companies where "
                + "Company_ID=" + compID + " or Company_ID=" + (compID + 1);
                sqlConnection1.Open();
                cmd2.ExecuteNonQuery();
                Label2.Text = "You have deleted <b>" + ListBox1.SelectedItem.Text
                + " Company <b> successfully;)";
                sqlConnection1.Close();
            }
        }

أتمنى أن تكون قد وصلت لغايتك إذا أستمرت المشكلة عاود السؤال عسى أن ينجدك من هو أكثر من خبره في هذا المجال

أتمنى لك التوفيق

تم تعديل هذه المشاركة بواسطة TarekNajem04 في 7 ديسمبر 2005 في 14:47

لا تفعل إلا ما تستطيع أن تقوله ولا تقول إلا ما تسطيع أن تفعله

اللهم علمنا ما ينفعنا ونفعنا بما علمتنا وهب لنا من لدنك ولياً مرشدا

كلما أدبني الدهر أراني نقص عقلي ... وإذا ما زدت علماً زادني علماً بجهلي

أخوكم في الله أبو المجد

هيا نتعلم بعض المبادء لنطرق باب الإحتراف ************** جديد الدروس ( Application Domains and Assemblies)

التوقيت في دمشق:

<embed src="http://www.clocklink.com/clocks/0006-Gray.swf?TimeZone=R1T" width="200" height="240" wmode="transparent" type="application/x-shockwave-flash">

هذا الموضوع مغلق.

مواضيع مشابهة