Click here to Skip to main content
15,889,867 members
Home / Discussions / C#
   

C#

 
Question[SOLVED] SQL Server 2014 & SqlDataSourceEnumerator Pin
MooKowMyke30-May-15 19:14
MooKowMyke30-May-15 19:14 
AnswerRe: SQL Server 2014 & SqlDataSourceEnumerator Pin
MooKowMyke30-May-15 20:37
MooKowMyke30-May-15 20:37 
AnswerRe: SQL Server 2014 & SqlDataSourceEnumerator Pin
Eddy Vluggen31-May-15 0:52
professionalEddy Vluggen31-May-15 0:52 
GeneralRe: SQL Server 2014 & SqlDataSourceEnumerator Pin
MooKowMyke31-May-15 17:20
MooKowMyke31-May-15 17:20 
QuestionHow to change datatype of a variable without changing its value ? Pin
Member 968349129-May-15 22:14
Member 968349129-May-15 22:14 
AnswerRe: How to change datatype of a variable without changing its value ? Pin
OriginalGriff29-May-15 22:59
mveOriginalGriff29-May-15 22:59 
QuestionNot able to insert my data into sql db. Getting that annoying null error! Pin
Norris Chappell29-May-15 7:15
Norris Chappell29-May-15 7:15 
AnswerRe: Not able to insert my data into sql db. Getting that annoying null error! Pin
Richard Deeming29-May-15 7:41
mveRichard Deeming29-May-15 7:41 
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

C#
protected void InsertButton_Click(object sender, EventArgs e)
{
    const string Query = "INSERT INTO StaffTracking (Project, Tower, [Functional Area], ResourceName, CATWResourceName, Org, IndicateifBillingFP, IndicateifBillingTM, DefaultFTE, Active, PersonnelResourceType) VALUES (@Project, @Tower, @FunctionalArea, @ResourceName, @CATWResourceName, @Org, @IndicateifBillingFP, @IndicateifBillingTM, @DefaultFTE, @Active, @PersonnelResourceType)";

    using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString))
    using (SqlCommand command = new SqlCommand(Query, connection))
    {
        connection.Open();

        foreach (GridViewRow row in gvNEW.Rows)
        {
            command.Parameters.AddWithValue("@Project", ((TextBox)row.FindControl("txtProject")).Text);
            command.Parameters.AddWithValue("@Tower", ((TextBox)row.FindControl("txtTower")).Text);
            command.Parameters.AddWithValue("@FunctionalArea", ((TextBox)row.FindControl("txtFunctional Area")).Text);
            command.Parameters.AddWithValue("@ResourceName", ((TextBox)row.FindControl("txtResourceName")).Text);
            command.Parameters.AddWithValue("@CATWResourceName", ((TextBox)row.FindControl("txtCATWResourceName")).Text);
            command.Parameters.AddWithValue("@Org", ((TextBox)row.FindControl("TxtOrg")).Text);
            command.Parameters.AddWithValue("@IndicateifBillingFP", ((TextBox)row.FindControl("txtIndicateifBillingFP")).Text);
            command.Parameters.AddWithValue("@IndicateifBillingTM", ((TextBox)row.FindControl("txtIndicateifBillingTM")).Text);
            command.Parameters.AddWithValue("@DefaultFTE", ((TextBox)row.FindControl("txtDefaultFTE")).Text);
            command.Parameters.AddWithValue("@Active", ((TextBox)row.FindControl("txtActive")).Text);
            command.Parameters.AddWithValue("@PersonnelResourceType", ((TextBox)row.FindControl("txtPersonnelResourceType")).Text);

            command.ExecuteNonQuery();
            command.Parameters.Clear();
        }
    }

    ResetInputForm();
}


The NullReferenceException is most likely due to the fact that your code is removing the column at index 0 on each pass through the loop.
  • On the first row, this will remove the txtName control, which you don't use.
  • On the second row, it will remove the txtProject control. As a result, row.FindControl("txtProject") will return null, and you'll get a NullReferenceException when you try to access its Text property.




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


AnswerRe: Not able to insert my data into sql db. Getting that annoying null error! Pin
Sascha Lefèvre29-May-15 7:48
professionalSascha Lefèvre29-May-15 7:48 
GeneralRe: Not able to insert my data into sql db. Getting that annoying null error! Pin
Norris Chappell29-May-15 8:14
Norris Chappell29-May-15 8:14 
GeneralRe: Not able to insert my data into sql db. Getting that annoying null error! Pin
Sascha Lefèvre29-May-15 12:33
professionalSascha Lefèvre29-May-15 12:33 
GeneralRe: Not able to insert my data into sql db. Getting that annoying null error! Pin
Norris Chappell31-May-15 8:41
Norris Chappell31-May-15 8:41 
GeneralRe: Not able to insert my data into sql db. Getting that annoying null error! Pin
Sascha Lefèvre31-May-15 8:58
professionalSascha Lefèvre31-May-15 8:58 
QuestionLoad specific userControl from DLL Pin
Jassim Rahma29-May-15 5:29
Jassim Rahma29-May-15 5:29 
AnswerRe: Load specific userControl from DLL Pin
Bernhard Hiller29-May-15 5:55
Bernhard Hiller29-May-15 5:55 
Generalvideo streaming mvc3 c#,html5 and jquery Pin
saiguttina29-May-15 0:25
saiguttina29-May-15 0:25 
GeneralMessage Closed Pin
28-May-15 23:39
Member 1172789928-May-15 23:39 
AnswerRe: help with calculator program Pin
V.28-May-15 23:45
professionalV.28-May-15 23:45 
AnswerRe: help with calculator program Pin
OriginalGriff29-May-15 0:08
mveOriginalGriff29-May-15 0:08 
QuestionSharePoint Pin
Armugam Indrani28-May-15 22:41
professionalArmugam Indrani28-May-15 22:41 
AnswerRe: SharePoint Pin
Eddy Vluggen29-May-15 2:50
professionalEddy Vluggen29-May-15 2:50 
QuestionTCP Heartbeat background Thread Pin
MooKowMyke27-May-15 21:50
MooKowMyke27-May-15 21:50 
AnswerRe: TCP Heartbeat background Thread Pin
Eddy Vluggen29-May-15 2:57
professionalEddy Vluggen29-May-15 2:57 
GeneralRe: TCP Heartbeat background Thread Pin
MooKowMyke29-May-15 7:57
MooKowMyke29-May-15 7:57 
GeneralRe: TCP Heartbeat background Thread Pin
Eddy Vluggen29-May-15 11:16
professionalEddy Vluggen29-May-15 11:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.