INSERT data in SQL but not recorded in Table

junaidsoomro

New member
Joined
Oct 28, 2013
Messages
3
Programming Experience
Beginner
This is my Statment:
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            //if (Operators.ConditionalCompareObjectEqual(this.Session["Order_Dup_Status"], "yes", false))
            {
                this.DB_cn = new SqlConnection();
                this.DB_cn.ConnectionString = "Data Source=172.16.4.8;Initial Catalog=anDb;User ID=sa; Password= ;";
                if (this.DB_cn.State == ConnectionState.Closed)
                {
                    this.DB_cn.Open();
                }
                this.DB_Cmd = new SqlCommand();
                this.DB_Cmd.Connection = this.DB_cn;
                string strins = "INSERT into vendortbl(companybox, remarksbox) VALUES ('" + this.CompanyNameBox.Text + "','" + this.RemarksBox.Text + "')";
                this.DB_Cmd.CommandText = strins;
                //this.DB_Cmd.ExecuteNonQuery();
                if (this.DB_cn.State == ConnectionState.Open)
                {
                    this.DB_cn.Close();
                }
                this.QuerySubmitedLabel.Visible = true;
            }
        }
        catch (NullReferenceException exception1)
        {
            ProjectData.SetProjectError(exception1);
            //Exception exception = exception1;
            NullReferenceException exception = exception1;
            //this.Lbl_Order_Message.Visible = true;
            //this.Lbl_Order_Message.Text = exception.Message.ToString();
            ProjectData.ClearProjectError();
        }

But When I'm submit, Showing Query Successfully Submitted but in the Table its blank

Can someone guide me what is the problem?

Thanks
 
Last edited by a moderator:
By the way, why are you catching a NullReferenceException? There's no reason for a NullReferenceException to be thrown in that code. A SqlException is a reasonable possibility but exactly which reference are you concerned may be null that you can't test for first?
 
Thank you very much...
its mean you want to say I'll go with this

catch (NullReferenceException exception1)
{
ProjectData.SetProjectError(exception1);
Exception exception = exception1;
NullReferenceException exception = exception1;
//this.Lbl_Order_Message.Visible = true;
//this.Lbl_Order_Message.Text = exception.Message.ToString();
ProjectData.ClearProjectError();
}

but its showing error:
Error 21 A local variable named 'exception' is already defined in this scope F:\Projects\SupplierWeb\MainPage.aspx.cs 264 36 SupplierWeb

so I commented //NullReferenceException exception = exception1;

Is this okay?

After this still my data is not saving :( I don't know why..
Is there any option I'll attach my this project and give to you maybe you'll better run this program and check.
thanks.
 
After this still my data is not saving :( I don't know why..

Seriously? You don't know why? It looks like you've read post #3 but not done anything reasonable about it and you've ignored post #2 altogether. Call ExecuteNonQuery and don't catch a NullReferenceException. That's what I said.
 
Thanks You Very Much..

Problem has been solved due to your advice.

Finally i use this:
    protected void QuerySubmit_Click(object sender, EventArgs e)
    {
        try
        {
            this.QuerySubmitedLabel.Visible = true;
            //if (Operators.ConditionalCompareObjectEqual(this.Session["Order_Dup_Status"], "yes", false))
            {
                this.DB_cn = new SqlConnection();
                this.DB_cn.ConnectionString = "Data Source=172.16.4.8;Initial Catalog=SupDb;User ID=sa; Password= ;";
                if (this.DB_cn.State == ConnectionState.Closed)
                {
                    this.DB_cn.Open();
                }
                this.DB_Cmd = new SqlCommand();
                this.DB_Cmd.Connection = this.DB_cn;
                string strins = "INSERT into SupTable(CompanyName, Remarks, Filename) VALUES ('" + this.CompanyNameBox.Text + "','" + this.RemarksBox.Text + "','" + this.StatmentUpload.FileName + "')";
                this.DB_Cmd.CommandText = strins;
                this.DB_Cmd.ExecuteNonQuery();
                if (this.DB_cn.State == ConnectionState.Open)
                {
                    this.DB_cn.Close();
                }
                this.StatmentUpload.SaveAs(@"C:\Inetpub\wwwroot\SupplierWeb\SupDat\" + this.StatmentUpload.FileName);
            }
            this.CompanyNameBox.Text = "";
            this.RemarksBox.Text = "";
        }
        catch (NullReferenceException exception1)
        {
            ProjectData.SetProjectError(exception1);
            Exception exception = exception1;
            ProjectData.ClearProjectError();
        }
    }



But Now I need to fetch this data in other page, I'll try but if any help required can I submit new POST?
thanks

Regards
Junaid
 
Last edited:
I need to fetch this data in other page, I'll try but if any help required can I submit new POST?

Yes you can and yes you should. Please keep each thread to a single topic and each topic to a single thread. If this issue has been resolved then please mark this thread Answered and post your new topic in a new thread.

Also, when you post code snippets, please format them for readability. I fixed the code in your first post but you didn't take the cue and continued to post unformatted code in subsequent posts. Unformatted code is hard to read and the last thing you want to do is make it hard for people to help you.
 
Back
Top Bottom